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.

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
------------------------------------------------------------------------------

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

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.

:o I really need to think about getting .NET

Yea, I suppose that would work. hehe.

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

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 :-).

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.

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.

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'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

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))

commented: :) +12

In VB.Net Screen.FromControl(Me).WorkingArea is not valid instruction as for as screen is concerned in asp.net vb code. The following code is valid code in in asp.net vb code:
Dim currentArea = System.Windows.Forms.Screen.FromControl(Me).WorkingArea.
However error is encountered at 'Me'with a message that frm can not be converted to systems.windows.forms.controls. please advise furthe course of action

Me.Top, Me.Height, Me.Left, Me.Width are not valid instructions in asp.net with VB code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.