none
Centering Maximized Screen and objects RRS feed

  • Question

  •  

    Hi,

    I created a program in VB.net. During runtime, if the user clicks on the maximize button - I have set the properties so that the form will get larger - but not as big as the total screen size. When the original form loads it is in the center of the screen, but when the user maximuizes it - it jumps to the top left hand corner of the screen + the objects on the form itself also align to the top left. How or what properties do I set to insure that both the form and the objects on the form open in the center of the screen. Also is there a way to make the opject adjust in size to match the larger maximizes form.

    Thanks,

    Micky

    Thursday, September 13, 2007 4:54 PM

Answers

  • In the designer for the form set the Anchor property (within the Layout subgroup) of all the controls to the value of None. (If you click the arrow at the right you can open up the graphical interface and simply unselect all the items).  You should be able to group select all your controls and do this changeonce for all of them.

     

    Thursday, September 13, 2007 7:05 PM
  • Use the MaximizedBounds property.  For example:

    Public Class Form1
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MaximumSize = New Size(800, 600)
        Dim x As Integer = (Screen.PrimaryScreen.WorkingArea.Width - Me.MaximumSize.Width) \ 2
        Dim y As Integer = (Screen.PrimaryScreen.WorkingArea.Height - Me.MaximumSize.Height) \ 2
        Me.MaximizedBounds = New Rectangle(New Point(x, y), Me.MaximumSize)
      End Sub
    End Class
    Friday, September 14, 2007 2:01 PM
    Moderator
  • I can think of two ways offhand.  If it is the initial display of the form that you are concerned with then you can set the form's StartPosition property to "CenterScreen"

     

    If you want to do it manually then you could try this

     

    Code Snippet

    Private Sub Form1_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd

    Me.Location = New Point(My.Computer.Screen.WorkingArea.Width / 2 - Me.Width / 2, My.Computer.Screen.WorkingArea.Height / 2 - Me.Height / 2)

    End Sub

     

     

     

    it automatically centers the form whenever the form resizes --  best of all it does it in relation to the visible portionof the scereen (i.e. not the taskbar)
    Thursday, September 13, 2007 9:08 PM

All replies

  • In the designer for the form set the Anchor property (within the Layout subgroup) of all the controls to the value of None. (If you click the arrow at the right you can open up the graphical interface and simply unselect all the items).  You should be able to group select all your controls and do this changeonce for all of them.

     

    Thursday, September 13, 2007 7:05 PM
  • YES - That worked perfectly!!! Thank you

     

    Do you know how to make the maximized form open in the center of the screen - opposed to the top left? Thanks Again, Micky

    Thursday, September 13, 2007 7:46 PM
  • I can think of two ways offhand.  If it is the initial display of the form that you are concerned with then you can set the form's StartPosition property to "CenterScreen"

     

    If you want to do it manually then you could try this

     

    Code Snippet

    Private Sub Form1_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd

    Me.Location = New Point(My.Computer.Screen.WorkingArea.Width / 2 - Me.Width / 2, My.Computer.Screen.WorkingArea.Height / 2 - Me.Height / 2)

    End Sub

     

     

     

    it automatically centers the form whenever the form resizes --  best of all it does it in relation to the visible portionof the scereen (i.e. not the taskbar)
    Thursday, September 13, 2007 9:08 PM
  • Use the MaximizedBounds property.  For example:

    Public Class Form1
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MaximumSize = New Size(800, 600)
        Dim x As Integer = (Screen.PrimaryScreen.WorkingArea.Width - Me.MaximumSize.Width) \ 2
        Dim y As Integer = (Screen.PrimaryScreen.WorkingArea.Height - Me.MaximumSize.Height) \ 2
        Me.MaximizedBounds = New Rectangle(New Point(x, y), Me.MaximumSize)
      End Sub
    End Class
    Friday, September 14, 2007 2:01 PM
    Moderator