Centering Form2 inside Form1

How I have centering Form2 inside Form1. CenterToParent() doesn't work.

Answer this question

Centering Form2 inside Form1

  • Tarana

    Beautiful!

    Thank you very much!

    Good bye!

    Blader


  • ZopoStyle

     Blader wrote:
    Thank you, James.

    Blader,

    Ok, this is the best work around if you don't want to change Form1 into a MDI form.

    Put this coding in the button (or other control) that is creating the instance of Form2:

    Dim frm2 As New Form2
    frm2.Left =
    CInt(Me.Left + ((Me.Width - frm2.Width) / 2))
    frm2.Top =
    CInt(Me.Top + ((Me.Height - frm2.Height) / 2))
    frm2.Show()

    This will make Form2's center of position = Form1's center of position. I don't believe there is a built-in command to the VB Language to do this; so hopefully this work around will be sufficient to you. Now keep in mind that if Form2 is larger than Form1 and the position of Form1 is in the wrong place it could be possible to make it so that Form2 can't be closed because the control box is off the screen. You may want to add some coding that will limit the position Form2 can go left. Such as finding the screen resolution and then subtracting Form2.Width from the user's screen width and then use an if...then statement that will set the maximum left position. Again, this is just a caution to you for possible future problems by using this centering position method.

    I hope this solves the problem for you.

    Thank you,

    James


  • holysmokes99

    Thank you James,

    I've tried to change the"StartPosition" property to: CenterParent but I don't know to make Form2 as the child to Form1.


  • Mikael Håkansson

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim frm2 As New Form2

    frm2.Left = CInt(Me.Left + ((Me.Width - frm2.Width) / 2))

    frm2.Top = CInt(Me.Top + ((Me.Height - frm2.Height) / 2))

    frm2.Show()

    Hide()

    End Sub

    End Class

    Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Form1.Show()

    Me.Close()

    End Sub

    End Class

    I'm sory. Form2 is shown on other place.

    Thank you, James.


  • Leonard Lee

    Thank you, James.
  • john82

    OK. It's very good sample. But I wondered how I can centering form1 into form2.

    Thanks.


  • Jassim Rahma

    Blader wrote:
    How I have centering Form2 inside Form1. CenterToParent() doesn't work.

    Blader,

    1) Use Design View of Form2

    2) Select the Form in the properties window

    3) Change the "StartPosition" property to: CenterParent

    Note: This will only work if Form2 is the child to Form1. To accomplish the relationship I would have Form1 load and show Form2.

    Thank you,

    James


  • Bluehunter

    Blader,

    Easiest way to create this type of relationship is to ensure that Form1 is the starting form. So if you are using a Splash Form then you would want to change to start from Splash to Form1. As I understand, and please anyone correct me, all forms created are child forms to the starting form. Again, this could be completely wrong in understanding.

    The other way to create the child-parent relation is to make Form1 as a MDI form; then it will not matter at all what the starting form is. The disadvantage is that the child forms can not be shown in any area outside the parent form.

    If I find another way to make Form2 child of Form1 I will post it; assuming no one else has already posted it.

    You may want to start a new thread that asks how to make Form2 child of Form1.

    I am sorry that I am not knowledgeable enough to help further; hopefully someone else will respond and indicate how to make Form2 the child form.

    Thank you,

    James


  • Chirag Patel

    James Rea wrote:

    Blader wrote:
    Thank you, James.

    Blader,

    Ok, this is the best work around if you don't want to change Form1 into a MDI form.

    Put this coding in the button (or other control) that is creating the instance of Form2:

    Dim frm2 As New Form2
    frm2.Left =
    CInt(Me.Left + ((Me.Width - frm2.Width) / 2))
    frm2.Top =
    CInt(Me.Top + ((Me.Height - frm2.Height) / 2))
    frm2.Show()

    This will make Form2's center of position = Form1's center of position. I don't believe there is a built-in command to the VB Language to do this; so hopefully this work around will be sufficient to you. Now keep in mind that if Form2 is larger than Form1 and the position of Form1 is in the wrong place it could be possible to make it so that Form2 can't be closed because the control box is off the screen. You may want to add some coding that will limit the position Form2 can go left. Such as finding the screen resolution and then subtracting Form2.Width from the user's screen width and then use an if...then statement that will set the maximum left position. Again, this is just a caution to you for possible future problems by using this centering position method.

    I hope this solves the problem for you.

    Thank you,

    James

    Blader,

    Ohh, it's probably because the Form2 property for the StartPosition needs to be changed to "Manual". I forgot to mention that part. The StartPosition will override the coding; by setting it to manual you are telling VB not to change where your coding is placing it. Sorry that I forgot to specify that.

    Thank you,

    James


  • hr0nix

    Oh u know . . .

    I am so late but I am so thankful to u both.

    it`s very very great . . .

    thanks


  • Sajal Mahajan

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

    Form2.ShowDialog()

    End Sub

    End Class

    Public Class Form2

    Private Sub Form2_Load(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles Me.Load

    Me.CenterToParent()

    End Sub

    End Class



  • Centering Form2 inside Form1