0
votes

I have a MDI(Parent) Form and 2 other Forms. On form1 there is a button, location is (X:100, Y:200). I required when user click on the button through Form1, another form "Form2" should open. And location of Form2 should be right middle of the button(Sender).

I write this code

   Private Sub UpdateLocation(ByVal element As Control, ByVal frm As Form)
        Dim p As New Point(element.Location)
        p = element.PointToScreen(p)
        p.X = p.X + element.Width + 10
        p.Y = p.Y / 2

        Debug.Print(p.ToString)
        frm.Location = p
        frm.BringToFront()
    End Sub

but Form2 location is not as expected. In am calling the above function on button click event

    UpdateLocation(form1.button1, form2)

Any help would be appreciable. Thanks in advance.

Output of the above code is:

Button1_Click

After a few tests I realized that point.x (location.x) is working, but not the point.y (location.y)

You can download the piece of code from here. you can check the actual issues with it. VS2010 is required.

3
posting some screenshot would be helpful to solve your issue. - Rajaprabhu Aravindasamy
[Button1_Click] goo.gl/yfbLS [Button2_Click] goo.gl/27wUM [Button3_Click] goo.gl/yfbLS - Kartik Goyal

3 Answers

0
votes

Try this,

Private Sub UpdateLocation(ByVal element As Control, ByVal frm As Form)

        Dim p As New Point(element.Location)
        p = element.parent.PointToScreen(p)
        p.X = p.X + element.Width + 10
        p.Y = p.Y + (element.height/2)

        Debug.Print(p.ToString)
        frm.Location = p
        frm.BringToFront()

 End Sub
0
votes

I think you need to account for the height of the button and the form if you want it in the middle:

Dim p As Point = Me.MdiParent.PointToScreen(element.Location)
p.X += element.Width + 10
p.Y += (element.Height / 2) - (frm.Height / 2)
0
votes

"Solved", Just to reconfirm

Private Sub UpdateLocation(ByVal element As Control, ByVal frm As Form)
        If IsNothing(element) Then
            Exit Sub
        End If
        Dim p As New Point(element.Location)
        p = element.Parent.PointToScreen(p)
        p.X = p.X + element.Width + 5
        p.Y += (element.Height / 2) - (frm.Height / 2)

        frm.Location = p
        frm.BringToFront()
End Sub

this code is working 100% fine with all my cases.