1

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!

Community
  • 1
  • 1
AdorableVB
  • 1,383
  • 1
  • 13
  • 44
  • I don't really understand what you mean, do you mean you want your `infowindow` to move together with your `button` or your `pictureBox`? – King King Nov 27 '13 at 08:03
  • just like what you did, first was the button to move with the picture, right? now, I want it to move exactly how the button does, but the catch is, the winForm must be in relative to the button, like the picture above. as a result, I move the pic, button moves along, winForm moves along too. – AdorableVB Nov 27 '13 at 08:20
  • What is the type of `infoWindow`? A `Form`? or a `ToolStripControlHost`? ... – King King Nov 27 '13 at 08:22
  • I don't really know what type of form that is likely to be used, but I need it to be able to handle/contain an **activeX** control(cctv camera) + some buttons perhaps. that's the important thing, then you can choose what's better for use. – AdorableVB Nov 27 '13 at 08:25
  • @KingKing I have yet another question.. http://stackoverflow.com/questions/20256176/how-to-use-extract-identity-of-a-control-inside-an-activex-control-gmaps-net-vb – AdorableVB Nov 28 '13 at 01:27

1 Answers1

4

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
King King
  • 61,710
  • 16
  • 105
  • 130
  • I have added that code, but not the locationChanged thingy.. also, I have updated my question. that's not working for me.. – AdorableVB Nov 27 '13 at 08:39
  • @AdorableVB do you mean the code I posted works but the Location of your `infoWindow` is not what you want (as shown in the updated pic)? – King King Nov 27 '13 at 08:40
  • nope, code doesn't work either, I just put the form on top, to make it look like what I wanted for it to appear. – AdorableVB Nov 27 '13 at 08:42
  • @AdorableVB I see you actual code now, in fact it does not register `pictureBox1.LocationChanged` event handler. wait for a moment while I update the code in VB.NET for you. – King King Nov 27 '13 at 08:43
  • works now! but only after I dragged the picture, can I put its code to 'button1_Click'? EDIT: sorry wasn't thinking. I cannot right? by then it cannot be dragged. anyway, thanks! – AdorableVB Nov 27 '13 at 08:50
  • Good, also I'd think about having single control for both info window and button – Sergey Berezovskiy Nov 27 '13 at 08:52
  • @lazyberezovsky yes, it's OK, however looks like the OP's happy about his approach :) – King King Nov 27 '13 at 10:23