Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Click event for a set of code created controls

  1. #1

    Thread Starter
    Junior Member JaGrom's Avatar
    Join Date
    Nov 2006
    Location
    Rome (Italy)
    Posts
    17

    Resolved [RESOLVED] [2005] Click event for a set of code created controls

    In my project I have a set of controls created automatically

    VB Code:
    1. Dim i As Integer
    2. Dim tb As New Textbox
    3. For i = 1 to flatPane.Controls.Count
    4.     tb.text = "Hello world"
    5.     frmDocument.Controls.Add(tb)
    6. Next

    Now I want to make this controls react on Click event but I don't know how to do it. The control has not been created in design mode so I don't know how to create a Sub for this event. How I should write the code?

    Can anyone help me?
    Thanks a lot.
    Last edited by JaGrom; Nov 8th, 2006 at 06:34 PM.

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

    Re: [2005] Click event for a set of code created controls

    You still have to write the method to handle the event at design time. The easiest way is to add a control of the appropriate type at design time and have the IDE create an event handler for the desired event. You then delete the control and the method will be left behind, minus its Handles clause. You can then rename the method if desired. To connect your run-time-created controls' events to that method you use the AddHandler statement. Within the event handler you get a reference to the object that raised the event from the 'sender' argument.
    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

  3. #3

    Thread Starter
    Junior Member JaGrom's Avatar
    Join Date
    Nov 2006
    Location
    Rome (Italy)
    Posts
    17

    Re: [2005] Click event for a set of code created controls

    Ok. It worked, but I want to make any sinle control react in a different manner.

    For example I create a variable number of textbox controls
    For each one I want to display a different text on click_event.

    I was thinking to write a code that use the the index of the clicked item in the control collection.
    VB Code:
    1. frmMain.Controls.IndexOf([B]specified control[/B])
    How do I tell him which control I have clicked?
    Last edited by JaGrom; Nov 9th, 2006 at 04:58 AM.

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

    Re: [2005] Click event for a set of code created controls

    Quote Originally Posted by jmcilhinney
    Within the event handler you get a reference to the object that raised the event from the 'sender' argument.
    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

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

    Re: [2005] Click event for a set of code created controls

    Create a Dictionary(Of TextBox, String):
    VB Code:
    1. Private messagesByTextBox As New Dictionary(Of TextBox, String)
    When you create a TextBox you add an item to that Dictionary with the TextBox as the key and the corresponding message as the value:
    VB Code:
    1. Dim tb As New TextBox
    2. Dim message As String = "Hello World"
    3.  
    4. Me.messagesByTextBox.Add(tb, message)
    In your event handler you cast the sender as type TextBox and pass it to the Dictionary to get its corresponding message:
    VB Code:
    1. Dim tb As TextBox = DirectCast(sender, TextBox)
    2. Dim message As String = Me.messagesByTextBox(tb)
    3.  
    4. MessageBox.Show(message)
    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

  6. #6

    Thread Starter
    Junior Member JaGrom's Avatar
    Join Date
    Nov 2006
    Location
    Rome (Italy)
    Posts
    17

    Re: [2005] Click event for a set of code created controls

    Ok I have it.
    Thanks a lot!

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