StartPosition

  • Thread starter Thread starter Stephen Costanzo
  • Start date Start date
S

Stephen Costanzo

I have a blank form and my Sub New is as follows:

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

msConnectionString = msConnectionString & ";Workstation ID=" &
Environment.MachineName

BuildForm()

End Sub

In the Build Form sub, I read from the database and get information used to
add controls, change the form size, etc. In the Build Form sub, one of the
options is to change the start position from WindowsDefaultLocation to
either CenterScreen or Manual (and provide the start point).

I have suspened the layout of the form before all of the code in the Build
Form sub and then Resumed it (me.resumelayout(True)).

When the code changes the start position to CenterScreen, the form (at the
end) does not recenter on the screen based upon the new form size. Am I
missing something obvious?

Thank you
 
Stephen Costanzo said:
When the code changes the start position to CenterScreen, the form (at the
end) does not recenter on the screen based upon the new form size. Am I
missing something obvious?

Are you setting the form's size before setting the start position?
 
Yes I am.

So the flow again is:

Public Sub New()

MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
msConnectionString = msConnectionString & ";Workstation ID=" & Environment.MachineName
BuildForm()
End Sub

....

Public Sub BuildForm()
....
Me.SuspendLayout()
....
Me.Width = miMaxWidth + miControlGap * 2 + panPosition.Width
Me.Height = iY + miBaseControlHeight * 4 + panCommandPanel.Height
Me.StartPosition = FormStartPosition.CenterScreen
....
Me.Refresh
Me.ResumeLayout(True)
End Sub

Therefore the functionality in the BuildForm is overriding the functionality in the InitalizeComponent code. The screen is centered based upon the initial size of the form not the current size of the form. Therefore if I made the form 100x100 and the size turned out to be 400x380 it is centered as if it were the original.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top