Home All Groups Group Topic Archive Search About

Source object on right click

Author
22 Jun 2006 8:17 PM
wxnut
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

Author
23 Jun 2006 1:54 AM
Ahmed
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.

Ahmed
wxnut wrote:
Show quoteHide quote
> 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
Author
23 Jun 2006 2:00 AM
Ahmed
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 wrote:
Show quoteHide quote
> 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
Author
23 Jun 2006 1:33 PM
Chris Dunaway
wxnut wrote:
> 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
Author
23 Jun 2006 3:41 PM
Ahmed
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.

Chris Dunaway wrote:
Show quoteHide quote
> wxnut wrote:
> > 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
Author
26 Jun 2006 1:03 PM
Chris Dunaway
Ahmed wrote:
> 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.