注册 登录
编程论坛 VB.NET论坛

大家好,新人求助

hszhxl 发布于 2021-11-14 20:05, 1002 次点击
Public Class Form1
    Dim KJ_A As New ArrayList
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KJ_A.Add(TextBox1)
        KJ_A.Add(TextBox2)
        KJ_A.Add(TextBox3)
        KJ_A.Add(TextBox4)
        KJ_A.Add(TextBox5)
    End Sub
    Sub Change()
        TextBox6.Text = ""
        For i = 0 To 4
            TextBox6.Text = TextBox6.Text & KJ_A(i).text & ","
        Next
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Change()
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        Change()
    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        Change()
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
        Change()
    End Sub

    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
        Change()
    End Sub
End Class

请教各位大侠:这种控件数组,有没有办法把所有的Changed事件合并成一个公共的Changed事件?这只是一个样例,实际中这个数组中是有50多个Textbox的……
2 回复
#2
apull2021-11-14 23:58
程序代码:

Public Class Form1
    Dim KJ_A As New ArrayList
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim h, w As Integer
        For i = 1 To 5
            Dim t As TextBox = New TextBox()  '动态生成控件
            KJ_A.Add(t)

            AddHandler t.TextChanged, AddressOf TextChange '动态绑定事件
            h = t.Height
            w = t.Width
            t.Left = 10
            t.Text = "text" & i
            t.Top = (h + 10) * i '防止重叠
            Me.Controls.Add(t)  '添加到窗口

        Next

    End Sub

    Sub Change()
        txtBoxOut.Text = ""
        For Each txt In KJ_A
            txtBoxOut.Text = txtBoxOut.Text & txt.text & vbCrLf
        Next
    End Sub

    Private Sub TextChange(sender As Object, e As EventArgs)
        Change()
    End Sub

End Class



[此贴子已经被作者于2021-11-15 00:00编辑过]

#3
hszhxl2021-11-15 06:41
程序代码:
Public Class Form1
    Dim KJ_A As New ArrayList

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Dim h, w As Integer '如仅将已有控件添加至数组,无需此声明
        For i = 1 To 5
            'Dim t As TextBox = New TextBox()  '动态生成新控件
            Dim t As TextBox = Me.Controls("Textbox" & i)  '指定已有控件
            KJ_A.Add(t)
            AddHandler t.TextChanged, AddressOf TextChange '动态绑定事件,关键指令!!!!!!!!!!!!

            'h = t.Height '如仅将已有控件添加至数组,无需以下
            'w = t.Width
            't.Left = 10
            't.Text = "text" & i
            't.Top = (h + 10) * i '防止重叠
            'Me.Controls.Add(t)  '添加到窗口
        Next
    End Sub

    Sub TextChange() '公共事件
        TextBoxOut.Text = ""
        For Each txt In KJ_A
            TextBoxOut.Text = TextBoxOut.Text & txt.text & ","
        Next
    End Sub

End Class

非常感谢版主连夜精心回复,问题解决
祝本版越来越壮大!!!
不知道如何表达激动心情
1