Solved

EVENT HANDLER PANEL INSIDE FLOWLAYOUT PANEL DOES NOT FIRED

Posted on 2011-02-13
4
381 Views
Last Modified: 2012-05-11
Hi All,

Please see below code, why it does not work ?

 pnl.Controls.Add(lblType)
                pnl.Controls.Add(lblPrice)
                pnl.Controls.Add(pb)

                lblType.Dock = DockStyle.Bottom
                lblPrice.Dock = DockStyle.Bottom

                flpProfileBan.Controls.Add(pnl)

                AddHandler pnl.MouseClick, AddressOf Tyre_Profile_Click


    Private Sub Tyre_Profile_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        Dim pnl As Panel = DirectCast(sender, Panel)

        For Each ctl As Control In pnl.Controls
            If TypeOf ctl Is Label Then
                If ctl.Name = "lblPrice" Then
                    strCurrentProfileType = ctl.Text
                End If
            End If
        Next

    End Sub

Thank you.
0
Comment
Question by:emi_sastra
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34885486
It doesn't work because the Click NEVER reaches the Panel and therefore the event never gets fired.

Instead, you have a PictureBox and a Label that COMPLETELY cover the surface of the Panel.

If you want a "unified" click over the whole panel then wire up the Click() event for both the PictureBox and the Label instead.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34885600
Yes, it is.

AddHandler pb.MouseClick, AddressOf Tyre_Profile_Click

Now, how could I get the lbltype.text ? then sender not is picturebox.

 Private Sub Tyre_Profile_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        Dim pnl As Panel = DirectCast(sender, Panel)

        For Each ctl As Control In pnl.Controls
            If TypeOf ctl Is Label Then
                If ctl.Name = "lblPrice" Then
                    strCurrentProfileType = ctl.Text
                End If
            End If
        Next

    End Sub

Thank you.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34885659
You could wire up both of the labels to the Tyre_Profile_Click() handlers as well:

    AddHandler pnl.MouseClick, AddressOf Tyre_Profile_Click
    AddHandler lblType.MouseClick, AddressOf Tyre_Profile_Click
    AddHandler lblPrice.MouseClick, AddressOf Tyre_Profile_Click

Now cast the sender parameter to a control, but then cast the PARENT to a Panel:

    Dim pnl As Panel = DirectCast(DirectCast(sender, Control).Parent, Panel)

So all three internal controls will fire the handler and we simply cast the Parent() control to the Panel so we can iterate and find the Label that you are looking for.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 34885672
Yes, it works.

Thank you very much for your help.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question