Event porcedures for dynamically created buttons

Posted on 2005-04-21
Last Modified: 2010-04-23
I am writing a program the when the use selects a report from a list  control array is created then added to another forms control collection. The controls are used for collecting the parameters needed for the report. If the parameter is multivalued I have created a text box, two buttons (add/remove) and a list box. How do I create the click event procedure for these buttons or is there a better way to do this. Some of the code is below.
 If newParameterFieldDefinition.EnableAllowMultipleValue Then
                I = I + 3
                ReDim ctlNewList(I)
                ctlNewList(I - 4) = New System.Windows.Forms.Label
                ctlNewList(I - 4).Text = newParameterFieldDefinition.PromptText
                ctlNewList(I - 3) = New System.Windows.Forms.TextBox
                ctlNewList(I - 2) = New System.Windows.Forms.Button
                ctlNewList(I - 2).Text = "ADD >>"
                ctlNewList(I - 1) = New System.Windows.Forms.Button
                ctlNewList(I - 1).Text = "<< REMOVE"
                ctlNewList(I) = New System.Windows.Forms.ListBox
                ctlNewList(I).Tag = newParameterFieldDefinition.Name
                frmParameters.Controls.Add(ctlNewList(I - 4))
                frmParameters.Controls.Add(ctlNewList(I - 3))
                 frmParameters.Controls.Add(ctlNewList(I - 2))
                frmParameters.Controls.Add(ctlNewList(I - 1))
Question by:Iron01
    LVL 12

    Expert Comment

    first of all... is I  just a count for the number of added controls in your form?

    ... and im a little slow here, the add/remove buttons that you need the handlers for (you use addhandler/removehandler for that by the way, im sure you'll get some code posted up soon) ...  what are they adding/removing  ?
    LVL 5

    Expert Comment

    You use the addhandler function.

    Read about it in your help file.

    addhandler control_to_handle, function_to_call

    LVL 28

    Accepted Solution

    Here is an exampl how to add dynamically buttons and then add handlers to them for the click event

    Private ButtonArray(9) As Button

    Public Sub AddButtons()
        For x As Integer = 0 To 9
            ButtonArray(x) = New Button
            With ButtonArray(x)
                .Tag = x
                .Text = "Button " & x
                .Size = New Size(60,20)
                .Location = New Point(20, (x * 20) + 4)
                .Parent = Me
                .Visible = True
                AddHandler .Click, AddressOf ButtonHandler
            End With
    End Sub

    'Then, create a ButtonHandler to handle the button clicks

    Public Sub ButtonHandler(ByVal sender As System.Object, ByVal e As
        MsgBox("Button number " & CType(sender,Button).Tag & " was clicked")
    End Sub

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now