Source object on right click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In VB 2005 I have six seperately named picture boxes (arranged in a table
layout control) in which I plot different X/Y data lines. I would like to be
able to right-click on a specific picture box (say, plot #3) and select a
different line color for that one plot.

How can I figure out which of the six plots is the one in question when
right-clicking on it? I can easily change the line color using the context
menu control, but I don't know which plot to apply to color change to. The
'sender' object does not give me any info from which picture box the click
event originated.

thanks
 
I can think of two ways to do this:

To have a context menu for each picturebox and use the tag property to
write the name of the corresponding picturebox.

or

have one shared context menu among them. Handle the click event of that
menu item. cast the sender to a menu item and menuitem.owner is the
context menu. Now, you can get the location of the context menu and
compare it to the location of the picturebox. ie

if contextmenu.left > picturebox1.left and contextmenu.left <
picturebox1.right and contextmenu.top> picturebox1.top and
contextmenu.top < picturebox1.bottom then
' the right click was over picturebox1
else if ....

end if

I prefer the second way.
 
Just tought of a third way,

You can handle the onenter event for each picture box. and once the
mouse enters the picture box you set a variable to the name of the
picture box ie


Private mouse As String

Private Sub NameToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
NameToolStripMenuItem.Click
MsgBox(mouse)
End Sub


Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e
As System.EventArgs) Handles PictureBox1.MouseEnter
mouse = CType(sender.name, PictureBox).Name
End Sub
 
wxnut said:
How can I figure out which of the six plots is the one in question when
right-clicking on it? I can easily change the line color using the context
menu control, but I don't know which plot to apply to color change to. The
'sender' object does not give me any info from which picture box the click
event originated.

Do all 6 picture boxes share the same click method? If so, then the
sender is the picture box that was clicked. You just need to check
which one it is by casting the sender to PictureBox:

Dim pb As PictureBox = DirectCast(sender, PictureBox)

Or use the 'is' operator:

If sender is PictureBox1 Then
'do something
End If
 
Your code won't work. The sender is not the picturebox in this case
because he is using a contextmenu. The sender would be a menuitem.
 
Ahmed said:
Your code won't work. The sender is not the picturebox in this case
because he is using a contextmenu. The sender would be a menuitem.

The ContextMenu class has a SourceControl property which tells which
control the menu was invoked against. That should tell which picture
box the menu was clicked on.
 
Back
Top