VB.NET _Click sub for dynamically created buttons?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

tim8w

Thread Starter
Joined
Nov 9, 2004
Messages
35
I have a form where I have added buttons to by calling MyMsgBox.Controls.Add(NewButton).

How do I write the Click sub?

Calling Routine:

Code:
        Dim MyMsgBox As New dlgMessageBox
        Dim NewButton As New Button

        NewButton.Text = "New"
        MyMsgBox.Controls.Add(NewButton)

dlgMessageBoxRoutine:


Code:
Public Class dlgMessageBox

    Public Function ShowForm() As Integer
        Me.ShowDialog()
     End Function

    Private Sub cmdButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButtonCancel.Click
        Me.Close()
    End Sub

End Class

How do I write the _Click routine for the new button I added?
 

tim8w

Thread Starter
Joined
Nov 9, 2004
Messages
35
With help from Athiest in another forum, here's the solution:

Calling routine:

Code:
        Dim MyMsgBox As New dlgMessageBox
        Dim NewButton As New Button
        Dim ReplaceButton As New Button
        Dim AppendButton As New Button
        Dim iButtonClicked As Integer

        NewButton.Text = "New"
        NewButton.Tag = 1
        MyMsgBox.Controls.Add(NewButton)
        ReplaceButton .Text = "Replace"
        ReplaceButton .Tag = 2
        MyMsgBox.Controls.Add(ReplaceButton )
        AppendButton .Text = "Append"
        AppendButton .Tag = 3
        MyMsgBox.Controls.Add(AppendButton )


        iButtonClicked  = MyMsgBox.ShowForm()

dlgMessageBoxRoutine:

Code:
    Public Function ShowForm() As Integer
        Dim iIndex As Integer

        For iIndex = iLastOriginalControl To Me.Controls.Count - 1
            AddHandler Me.Controls.Item(iIndex).Click, AddressOf ButtonClicked
        Next iIndex
        Me.ShowDialog()
        ShowForm = iButtonClicked
    End Function

    Private Sub ButtonClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

        iButtonClicked = sender.Tag
        Me.Close()
    End Sub
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top