Dynamic Controls interaction

raycopper

New member
Joined
Dec 30, 2007
Messages
3
Programming Experience
5-10
I have a run time made button, and a run time made textbox. How do I change the text of the textbox in the button handler please?

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim myBtn As New Button

        With myBtn
            .Height = 30
            .Width = 30
            .Top = 100
            .Left = 100
            .Text = "OK"
        End With
        AddHandler myBtn.Click, AddressOf myEvent
        Me.Controls.Add(myBtn)

        Dim myTxt As New TextBox

        With myTxt
            .Height = 30
            .Width = 200
            .Top = 100
            .Left = 140
            .Text = ""
        End With
        Me.Controls.Add(myTxt)

    End Sub


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

        ' How to change myTxt.Text here?

    End Sub

End Class
 
Answered myself

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myBtn As New Button

        With myBtn
            .Height = 30
            .Width = 30
            .Top = 100
            .Left = 100
            .Text = "OK"
            .Name = "myBtn"
        End With
        AddHandler myBtn.Click, AddressOf myEvent
        Me.Controls.Add(myBtn)

        Dim myTxt As New TextBox

        With myTxt
            .Height = 30
            .Width = 200
            .Top = 100
            .Left = 140
            .Text = ""
            .Name = "myTxt"
        End With
        Me.Controls.Add(myTxt)

    End Sub


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

        ' How to change myTxt.Text here?
        For Each c As Control In Me.Controls
            If c.Name = "myTxt" Then
                c.Text = "Clicked"
            End If
        Next

    End Sub

End Class
 
Back
Top