Center form on screen or on parent

后端 未结 4 1303
生来不讨喜
生来不讨喜 2020-12-28 10:07

Since built in functionality for positioning forms in VB.NET are not always suitable to use I try to make my sub to do that.

But I missed something...



        
4条回答
  •  [愿得一人]
    2020-12-28 10:34

    I know this is an old post and this doesn't directly answer the question, but for anyone else who stumbles upon this thread, centering a form can be done simply without requiring you to write your own procedure.

    The System.Windows.Forms.Form.CenterToScreen() and System.Windows.Forms.Form.CenterToParent() allow you to center the a form in reference to the screen or in reference to the parent form depending on which one you need.

    One thing to note is that these procedures MUST be called before the form is loaded. It is best to call them in the form_load event handler.

    EXAMPLE CODE:

      Private Sub Settings_Load(sender As Object, e As EventArgs) Handles Me.Load
    
        Me.CenterToScreen()
    
        'or you can use 
    
        Me.CenterToParent()
    
    End Sub
    

提交回复
热议问题