| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 988 人关注过本帖
标题:大家好,新人求助
只看楼主 加入收藏
hszhxl
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-11-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
大家好,新人求助
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的……
搜索更多相关主题的帖子: Change Private sender End Sub 
2021-11-14 20:05
apull
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:三体星系
等 级:版主
威 望:216
帖 子:1506
专家分:9241
注 册:2010-3-16
收藏
得分:20 
程序代码:
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编辑过]

2021-11-14 23:58
hszhxl
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-11-14
收藏
得分:0 
程序代码:
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

非常感谢版主连夜精心回复,问题解决
祝本版越来越壮大!!!
不知道如何表达激动心情
2021-11-15 06:41
快速回复:大家好,新人求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048625 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved