Source object on right click

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wxnut

    #1

    Source object on right click

    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
  • Ahmed

    #2
    Re: Source object on right click

    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.lef t > picturebox1.lef t and contextmenu.lef t <
    picturebox1.rig ht and contextmenu.top > picturebox1.top and
    contextmenu.top < picturebox1.bot tom then
    ' the right click was over picturebox1
    else if ....

    end if

    I prefer the second way.

    Ahmed
    wxnut wrote:[color=blue]
    > 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[/color]

    Comment

    • Ahmed

      #3
      Re: Source object on right click

      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 NameToolStripMe nuItem_Click(By Val sender As
      System.Object, ByVal e As System.EventArg s) Handles
      NameToolStripMe nuItem.Click
      MsgBox(mouse)
      End Sub


      Private Sub PictureBox1_Mou seEnter(ByVal sender As Object, ByVal e
      As System.EventArg s) Handles PictureBox1.Mou seEnter
      mouse = CType(sender.na me, PictureBox).Nam e
      End Sub


      wxnut wrote:[color=blue]
      > 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[/color]

      Comment

      • Chris Dunaway

        #4
        Re: Source object on right click

        wxnut wrote:[color=blue]
        > 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.[/color]

        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(send er, PictureBox)

        Or use the 'is' operator:

        If sender is PictureBox1 Then
        'do something
        End If

        Comment

        • Ahmed

          #5
          Re: Source object on right click

          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:[color=blue]
          > wxnut wrote:[color=green]
          > > 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.[/color]
          >
          > 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(send er, PictureBox)
          >
          > Or use the 'is' operator:
          >
          > If sender is PictureBox1 Then
          > 'do something
          > End If[/color]

          Comment

          • Chris Dunaway

            #6
            Re: Source object on right click

            Ahmed wrote:[color=blue]
            > 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.
            >[/color]

            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.

            Comment

            Working...