954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Centering a form in VB.NET

Just got to playing around, and felt that the VB forum on here could use some posts.

What I was playing around with forms in VB.NET and wanted to hard code centering a form. Here is what I found...and if anyone has an easier or slicker way...let me know.


-in the windows form generated code portion of a form in VB.NET ad the following line:

Me.StartPosition = FormStartPosition.CenterScreen

-Note: to work this must be placed in the Form1 (or whatever form name) initialization code.

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

I've started using this code, since it works no matter what the screen resolution is on the user's monitor. It only does it when you start up the program, but I assume that's what you want. (I always use the prefix frm for the names of forms. In this case the form name is SampApplication

Code:

------------------------------------------------------------------------------
frmSampApplication.Left = (Screen.Width - frmSampApplication.Width) / 2
frmSampApplication.Top = (Screen.Width - frmSampApplication.Height) / 2
------------------------------------------------------------------------------

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

I'm 99% sure that this requires no code - its in the properties window for the form :-).

Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 

I'm not exactly sure you can do it in the properties window, or at least get it to the exact location. It would require a lot more work than needed. If you are talking about the form layout window, you can try to center it in there, but again, it would be very hard to get it to the exact spot, and it wouldn't work on all resolutions.

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

Is there a form layout window in .NET? lol

This is what I am talking about:
[img]http://tekmaven.dizla.com/centerscreen.jpg[/img]

Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 

:o I really need to think about getting .NET

Yea, I suppose that would work. hehe.

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

they have that same property for vb6 also, for those of us using vb6 still :(

big_k105
PFO Founder
Team Colleague
357 posts since May 2003
Reputation Points: 36
Solved Threads: 2
 

I love .NET. I actually live for .NET.

If your a VB6 programmer... it has somewhat of a learning curve to go .NET... but it will be extremely rewarding.

Not only is .NET 100x faster, but its more powerfull too :-).

Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 

Ryan, do you think you'd be able to resize the screenshot you have posted. It's sorta annoying that I keep having to horizontally scroll everytime I visit this page.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

Well bite my tongue. I guess I need to start looking more at the properties window. Hehe...

Guess I was punch drunk or something.

Thanks for the tip on the tip.

Except the screen width code noted above is VB6, and not .NET.

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

Mr. Gates, I got my copy of VB.Net 2003 for free. I don't know how are why. I go online a lot to reseach stuff and I most have filled out a survey or something. Next thing I know, I have a 60 day trial version that doesn't expire. I just wish I knew more about vb.net than I do. I'm learning, slowly but surely! :roll:

I_Byte
Newbie Poster
22 posts since Jul 2003
Reputation Points: 12
Solved Threads: 0
 

I've started using this code, since it works no matter what the screen resolution is on the user's monitor. It only does it when you start up the program, but I assume that's what you want. (I always use the prefix frm for the names of forms. In this case the form name is SampApplication

Code:

------------------------------------------------------------------------------ frmSampApplication.Left = (Screen.Width - frmSampApplication.Width) / 2 frmSampApplication.Top = (Screen.Width - frmSampApplication.Height) / 2 ------------------------------------------------------------------------------

Try this for VB.Net

Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2

linksys1
Newbie Poster
1 post since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

The issue with using primaryscreen is that you might not be on the primary screen and also the screens may be offset with respect to the primary. This code eliminates that and centers the form in the current screen:

Dim currentArea = Screen.FromControl(Me).WorkingArea
Me.Top = currentArea.Top + CInt((currentArea.Height / 2) - (Me.Height / 2))
Me.Left = currentArea.Left + CInt((currentArea.Width / 2) - (Me.Width / 2))

mattwilkinson99
Newbie Poster
1 post since Feb 2012
Reputation Points: 22
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You