AdorableVB
AdorableVB

Reputation: 1393

move winform in relation to a button from another form vb/c#

I want you to check on this first..

that's what I am working on, problem solved on the button, and now, I need to make a WinForm follow the button wherever and whenever I drag the map/picture. it is something like this, infowindows on google's API. first picture, I made it on html.

enter image description here

and this one.. this is what I am working on now, on winForms, I can't drag form2 with the picture.. enter image description here

this is my current code..

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim loc As Point = PictureBox1.PointToClient(Button1.PointToScreen(Point.Empty))
    Button1.Parent = PictureBox1
    Button1.Location = loc

End Sub

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

Private Sub pictureBox1_LocationChanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim p As Point = button1.PointToScreen(Point.Empty)
    p.Offset(5, 10)
    Form2.Location = p
    Form2.Owner = Me
End Sub

as you can see, that infowindow, I want that to be a form in my winForms. Is it possible that its location can be relative/parent to the button just like from the link above. thanks!

Upvotes: 1

Views: 894

Answers (1)

King King
King King

Reputation: 63327

You can try handling the LocationChanged of your picturebox like this:

//LocationChanged event handler for your pictureBox1
private void pictureBox1_LocationChanged(object sender, EventArgs e){
  //Get the location of button1 in screen coordinates
  Point p = button1.PointToScreen(Point.Empty);
  //Offset it to what you want
  p.Offset(5,10);
  //set the location for your infoWindow form
  infoWindow.Location = p;
}

Note that I use infoWindow as a form, I think it's usable in your case, set the FormBorderStyle to None and add some custom close button ... (You can search for more on this, there are tons of examples). In case you don't know how to register the LocationChanged event handler, here it is:

pictureBox1.LocationChanged += pictureBox1_LocationChanged;

Also note that your infoWindow form must have your main form as its owner:

infoWindow.Owner = yourMainForm;
//or this if the code is placed inside mainForm class
infoWindow.Owner = this;

Update:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim loc As Point = PictureBox1.PointToClient(Button1.PointToScreen(Point.Empty))
  Button1.Parent = PictureBox1
  Button1.Location = loc
  Form2.Owner = Me
End Sub

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

Private Sub pictureBox1_LocationChanged(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox1.LocationChanged

  Dim p As Point = button1.PointToScreen(Point.Empty)
  p.Offset(-Form2.Width/2, -Form2.Height-10)
  Form2.Location = p

End Sub

Upvotes: 4

Related Questions