Question Double Click Handler not working on dynamically created picturebox

lloydz1

New member
Joined
May 12, 2011
Messages
2
Programming Experience
3-5
Hi Guys,
I have a Winform application where i added picturebox's to a flowlayout panel dynamically from code. I have successfully managed to add a handler for doubleclick and click events the problem i have is that when i add both together it only seems to work with the click event and not the double click. Any ideas where I could bee going wrong? Iam using VS2008 .NET 3.5
Thanks

my code looks a bit like this :

Public Class Class1
Dim WithEvents picturebox1 As PictureBox

Private Sub Brochure_Creator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 to 2
picturebox1 = New PictureBox

picturebox1.Image = System.Drawing.Image.FromFile(myPicArray(i))
picturebox1.Name = "PB" & i
picturebox1.Size = New Size(165, 99)
picturebox1.SizeMode = PictureBoxSizeMode.StretchImage
picturebox1.BorderStyle = BorderStyle.None
picturebox1.BackColor = Color.Black


AddHandler picturebox1.DoubleClick, AddressOf Picturebox1_DoubleClick
AddHandler picturebox1.Click, AddressOf Picturebox1_click

FlowLayoutPanel1.Controls.Add(picturebox)
picturebox = Nothing

End Sub

Private Sub Picturebox1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picturebox1.Click
Msgbox("click")
End Sub

Private Sub Picturebox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picturebox1.DoubleClick
Msgbox("doubleclick")

End Sub

End Class
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
You should not be using a Handles clause and AddHandler. For objects created at run time, you should pretty much always use AddHandler. Get rid of the member variable and the Handles clause. Just use a local variable and AddHandler.
 

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
You wont be able to double click if the message box pops up as soon as you click. I got some code like this to work on a textbox:

    Public index As Long = 0
    Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
        For x As Integer = 1 To 10
            index += x
            Console.WriteLine(index)
        Next x
    End Sub
    Private Sub TextBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick
        MsgBox("dub")
    End Sub


When I double click the click code is run once and then the double click code.
 

lloydz1

New member
Joined
May 12, 2011
Messages
2
Programming Experience
3-5
Thanks For you help Guys But I managed to resolve it, I di not show the full code due to confidentiality of client but on the click event of the dynamically created picturebox I chagned the borderstyle of the picture box this seemed to stop the double click event from being called I remove the border change and now works perfectly
 
Top Bottom