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

For each Next 循环问题

Mountisky 发布于 2017-10-22 14:07, 1589 次点击
程序代码:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim ctl1 As Control
        Dim i As Integer = 4
        Dim a(i) As String
        For Each ctl1 In Me.Controls
            If Mid(ctl1.Name, 1, 2) = "Bu" Then
                For i = 1 To 4
                    a(i) = ctl1.Name
                    ' i = i + 1
                    TextBox1.Text = TextBox1.Text & vbCrLf & "a" & i & "=" & a(i)
                Next
            End If
        Next ctl1
    End Sub


我希望输出的结果是
a1=button1,
a2=button2,
a3=button3…
实际的输出结果是这个样子的,我不知道到底哪里出了问题,
我希望遍历集合,把符合条件的,一个一个的输出到一个数组里面,
只有本站会员才能查看附件,请 登录

3 回复
#2
xyxcc1772017-10-22 19:37
在net中,最后添加上去的控件的index的值是0
#3
wmf20142017-10-23 11:41
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim ctl1 As Control
        Dim i As Integer = 4
        Dim a(i) As String
        i=1
        For Each ctl1 In Me.Controls
            If Mid(ctl1.Name, 1, 2) = "Bu" Then
               if i<5 then
                    a(i) = ctl1.Name
                    TextBox1.Text = TextBox1.Text & vbCrLf & "a" & i & "=" & a(i)
                    i = i + 1
                end if
            End If
        Next ctl1
    End Sub
#4
紫金山2017-10-26 09:21
回复 3楼 wmf2014
谢谢,万谢,问题基本解决,我研究了下,好像是遍历先把最后一个找出来进行赋值。顺序刚好是反的。
1