Results 1 to 13 of 13

Thread: Event Handler For Dynamic Controls

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Event Handler For Dynamic Controls

    Below I create an array of labels. I would like to add an click event to my labels. Can someone point me in a direction?

    Thanks

    Code:
    Public Class frmMain
    
       
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            
    
            Dim lblQty(196) As Label
    
            Dim x As Integer = 115
            Dim y As Integer = 30
    
            For intLoopRows As Integer = 0 To 27
    
                y += 37
    
    
                For intLoopCols As Integer = 0 To 6
                    x += 50
    
                    lblQty(intLoopRows) = New Label
                    lblQty(intLoopRows).Text = "0"
    
                    lblQty(intLoopRows).SetBounds(x, y, 12, 12)
                    Me.Controls.Add(lblQty(intLoopRows))
    
                Next
                x = 115
    
    
            Next
    
        End Sub
    
       
    End Class

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Event Handler For Dynamic Controls

    Have a search for AddHandler.
    Last edited by dee-u; Oct 20th, 2009 at 10:55 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Re: Event Handler For Dynamic Controls

    I did. Thanks. I changed my code (see below) and it does not work.....I am probably missing something very obvious to you more experienced programmers. Thanks for any help.

    Code:
    Public Class frmMain
    
       
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim lblQty(196) As Label
    
            For intCreateArray As Integer = 0 To 196
    
                lblQty(intCreateArray) = New Label
                AddHandler lblQty(intCreateArray).Click, AddressOf labelclicked
    
    
            Next
    
    
            Dim x As Integer = 115
            Dim y As Integer = 30
    
            For intLoopRows As Integer = 0 To 27
    
                y += 37
    
    
                For intLoopCols As Integer = 0 To 6
                    x += 50
    
                    lblQty(intLoopRows) = New Label
                    lblQty(intLoopRows).Text = "0"
    
                    lblQty(intLoopRows).SetBounds(x, y, 12, 12)
                    Me.Controls.Add(lblQty(intLoopRows))
    
                Next
                x = 115
    
    
            Next
    
        End Sub
    
        Private Sub labelclicked(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.Text = "99"
    
    
        End Sub
       
    End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Re: Event Handler For Dynamic Controls

    Still trying with no luck.....please help if you can.

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Event Handler For Dynamic Controls

    You need to put the addhandler in your inner loop.
    VB.Net Code:
    1. For intLoopCols As Integer = 0 To 6
    2.                 x += 50
    3.  
    4.                 lblQty(intLoopRows) = New Label
    5.                 lblQty(intLoopRows).Text = "0"
    6.  
    7.                 lblQty(intLoopRows).SetBounds(x, y, 12, 12)
    8.                 AddHandler lblQty(intLoopRows).Click, AddressOf labelclicked
    9.                 Me.Controls.Add(lblQty(intLoopRows))
    10.  
    11.             Next
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Re: Event Handler For Dynamic Controls

    dee-U,

    Thank you. Is there a way for me to know which lbl was clicked in my event handler?

    Thanks

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Event Handler For Dynamic Controls

    Use the Sender object that is passed in to the event handler
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Event Handler For Dynamic Controls

    Quote Originally Posted by bgreer5050 View Post
    dee-U,

    Thank you. Is there a way for me to know which lbl was clicked in my event handler?

    Thanks
    As is the case with all event handlers, the 'sender' parameter is a reference to the object that raised the event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Re: Event Handler For Dynamic Controls

    Ok. The labels on the form are an array of labels. How do I know which one was clicked?

    messagebox.show(sender.???????)

    Thanks Again

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Event Handler For Dynamic Controls

    Well because Sender is of type Object, you need to cast it to the relevant type first before you can access properties on it. Something like this:

    vb Code:
    1. MessageBox.Show(DirectCast(Sender, Control).Name)

    or, if you know it will ALWAYS be a label that is the sender:

    vb Code:
    1. MessageBox.Show(DirectCast(Sender, Label).Name)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Re: Event Handler For Dynamic Controls

    I am still missing something. I am getting a blank messagebox.

    Here is the code as it stands now:

    Code:
    Public Class frmMain
    
        Dim lblQty(196) As Label
    
    
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Protected Friend Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
            'Dim lblQty(196) As Label
    
            For intCreateArray As Integer = 0 To 196
    
                lblQty(intCreateArray) = New Label
    
    
    
    
            Next
    
    
            Dim x As Integer = 115
            Dim y As Integer = 30
    
            For intLoopRows As Integer = 0 To 27
    
                y += 37
    
    
                For intLoopCols As Integer = 0 To 6
                    x += 50
    
                    lblQty(intLoopRows) = New Label
    
    
    
                    'AddHandler lblQty(intLoopRows).Click, AddressOf labelclicked
                    lblQty(intLoopRows).Text = "0"
    
                    lblQty(intLoopRows).SetBounds(x, y, 12, 12)
                    Me.Controls.Add(lblQty(intLoopRows))
                    AddHandler lblQty(intLoopRows).Click, AddressOf labelclicked
    
    
    
                Next
                x = 115
    
    
            Next
    
        End Sub
    
        Private Sub labelclicked(ByVal sender As Object, ByVal e As System.EventArgs)
    
            MessageBox.Show((DirectCast(sender, Control).Name).ToString)
            MessageBox.Show(DirectCast(sender, Label).Name)
    
    
    
            'Dim test As Label = New Label
            'test = CType(sender, Label)
            ''MessageBox.Show(sender.GetType.ToString)
    
    
    
    
    
    
    
    
    
    
    
    
        End Sub
    
    End Class

  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Event Handler For Dynamic Controls

    Well thats because you are using my example code without even thinking about what it does... In my example I am showing the Name property of the label but in your code you are never setting the Name property of the labels you create so of course it will be blank when you read that property. You said you wanted to identify which label was clicked so I assumed you had some way of identifying each label (ie, the Name property). If you just wanted to get the label's text then you could do this:
    vb Code:
    1. MessageBox.Show(DirectCast(sender, Label).Text)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Re: Event Handler For Dynamic Controls

    Thanks Chris.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width