Single contextmenustrip with multiple pictureboxes

07-09-2007, 04:20 PM
I looked at this posting:

But with that posting it creates multiple contextmenu, one for each picturebox. I would like to know if it would be possible to use one contextmenustrip for multiple picturebox. What I have is 6 picturebox and one contextmenustrip. When one of the picturebox is right clicked the contextmenustrip comes up and you can select and one of the menu items. What does not work is the changing of the picture of the picturebox. I have tried using "focused" for the picturebox but it does not work, example below:

Private Sub CMGToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMGToolStripMenuItem.Click
If Slot1.Focused = True Then Slot1.BackgroundImage = imgadit.Images(1)
End Sub

How can I tell what picturebox is currently showing the contextmenustrip, so that I can change the picture accordingly.

Thanks for helping out.

07-14-2007, 12:19 PM
First of all, you need to find out which picture box was 'right-clicked'.

in the MouseDown event, cast the sender object to a picturebox and assign it to a global variable or something.

Dim currentPicBox As PictureBox

Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown, PictureBox2.MouseDown

If e.Button = Windows.Forms.MouseButtons.Right Then
currentPicBox = CType(sender, PictureBox)
End If

End Sub

Note that I have used a single function for handling the events of 2 picboxes.
You can use a single function for handling right click events of all 6 picboxes.

Next, inside the click event of the context menu item,
simply check the global picbox object and do something accordingly...

Private Sub SomeToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SomeToolStripMenuItem.Click

If currentPicBox Is PictureBox1 Then
' picturebox1 is right-clicked.. do something
ElseIf currentPicBox Is PictureBox2 Then
' picturebox2 is right-clicked.. do something
End If

End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum