Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am writing some code and I need to create a context menu strip that changes it's items when is over different picture boxes, 28 to be exact.
I have tried using Addhandler but it doesn't work.
I also needed to change their size and location by using the Addhandler witch i did.
So basically if the mouse right button is pressed it changes size (this I was able to do) and if the mouse left button is pressed it should open the context menu strip(this I couldn't do).
Were is a sample of my last try:

VB
Private Sub CCTVs_Handlers()
        AddHandler CCTV1.Click, Sub(obj As Object, args As EventArgs) Drop1()
        AddHandler CCTV1.DoubleClick, Sub(obj As Object, args As EventArgs) CAM1() ' This is to resize the Picturebox , CCTV1 is the Picturebox 
End sub


Private Sub Show_CCTV()
              Me.Text = WText + " - CCTV"
        Panel3.Show()
        Panel3.Location = New Point(76, 3)
        Panel3.Width = 1260
        Panel3.Height = 665

        Dim CCTV1 As New PictureBox
        With CCTV1
            .Location = New Point(0, 0)
            .Width = 90
            .Height = 90
            .BorderStyle = BorderStyle.Fixed3D
            .BackColor = Color.Green
            Panel3.Controls.Add(CCTV1)
            Dim lb As New Label
            With lb
                .Text = "CAM1"
                .Location = New Point(0, 0)
                CCTV1.Controls.Add(lb)
            End With
        End With
End Sub

 Private Sub CAM1()
        If CCTV1.Width = 90 Then
            CCTV1.Width = 400
            CCTV1.Height = 400
            CCTV1.BringToFront()
        Else
            CCTV1.Width = 90
            CCTV1.Height = 90
        End If
    End Sub

    Private Sub Drop1()
               Try
            Dim CCTV1d1 As New ContextMenuStrip
            With CCTV1d1
                .Items.Add("Disable CAM1")
                .Items.Add("Stop Recording")
                .Items.Add("CAM1 Propreties")
                .Items.Add("CAM1 Settings")
                .Items.Add("CCTV Settings")
                .DropShadowEnabled = False
                .BackgroundImageLayout = False
                CCTV1.ContextMenuStrip = CCTV1d1
                CCTV1.Controls.Add(CCTV1d1)

            End With
        Catch ex As Exception
            MsgBox("CCTV1d1 : " & ex.Message & " - " & ex.Source)
        End Try
    End Sub


What I have tried:

I have tried using Addhandler but it doesn't work
I tried to link it to other sub so it could call the context menu but this also didn't work and right now i am out of ideas
Please help
Posted
Updated 8-Jul-16 8:13am

1 solution

If we assume an arbitrary number of boxes could be drawn at runtime, it's not practical to have a unique set of event handlers for each item. No need for 28 different event handlers: just, for example, just a handler for that is aware of which box you're hovering over.

So, you could attach a "mouseIn" event to your image's 'box'. This would set a value - declared at a higher scope than your box or handlers, unique to each box, such that when you click on any particular box the onClick (right/left/double) can know which particular box you clicked.

The event handler can then use the value to determine how to respond.
 
Share this answer
 
Comments
JU571C3 8-Jul-16 20:49pm    
And how do I do that?
How do I set a one handler to do all that?
JU571C3 8-Jul-16 21:07pm    
Never mind I finally figured it out Thanks a lot Balboos (seriously thank you)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900