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

如何优化代码

yuk_yu 发布于 2016-08-12 12:01, 2298 次点击


我有一个label, 从label1到label187, 我想让1-187参与i的循环,如何可以简化?
               
                for i = 6  to 187
                Me.Label6.Text = Ds.Tables(0).Rows(I).Item(2).ToString
                Me.Label7.Text = Ds.Tables(0).Rows(I).Item(3).ToString
                Me.Label8.Text = Ds.Tables(0).Rows(I).Item(4).ToString
                next
4 回复
#2
yuk_yu2016-08-12 15:27
回复 楼主 yuk_yu
程序代码:
Dim lbl() As ArrayList
        Dim i As Integer
        For Each a As Control In Me.Controls
            If TypeOf (a) Is Label Then
                MsgBox(a.Name)
                i = i + 1
                ReDim Preserve lbl(i)
                lbl(i) = ???
            End If
        Next


不太会,请大家指教!
#3
yuk_yu2016-08-12 16:13
另外我有十个Groupbox, 每个里面有5个Label, 如果将所有Label加入数组?
#4
不说也罢2016-08-14 17:36
这涉及到窗体控件枚举的问题。写个例子,你可以根据这个思路给所有的Label赋值,相信对你来讲不是问题。
程序代码:


        Dim j As Integer = 0
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is System.Windows.Forms.GroupBox Then'遍历GroupBox
                For Each lbl As Control In ctl.Controls'如果GroupBox里只有Label控件,就不需要判断。如果有其它控件,需要象上面IF判断一下
                    j = lbl.Text.Replace("Label", "")
                    lbl.Text = j.ToString
                Next
            End If
        Next



[此贴子已经被作者于2016-8-14 17:39编辑过]

#5
yuk_yu2016-08-14 18:30
回复 4楼 不说也罢
谢谢版主,原来要逐层判断啊!再次感谢
1