Vb.net 标签名称的变量值
我正在为我的计算机课程做一个项目。我真的需要一些帮助 我希望能够得到这样一行基本的代码Vb.net 标签名称的变量值,vb.net,Vb.net,我正在为我的计算机课程做一个项目。我真的需要一些帮助 我希望能够得到这样一行基本的代码 lblSectionsAttempted.ForeColor = Color.Green 基本上把它变成这个。。。但这不起作用: lblSectionsAttempted(TempInc).ForeColor = Color.Green TempInc是一个变量,每次循环完成时,该变量将递增1 我的表格上有14个“lblsection未遂”标签。我希望能够根据变量TempInc的值来改变每个标签的
lblSectionsAttempted.ForeColor = Color.Green
基本上把它变成这个。。。但这不起作用:
lblSectionsAttempted(TempInc).ForeColor = Color.Green
TempInc是一个变量,每次循环完成时,该变量将递增1
我的表格上有14个“lblsection未遂”标签。我希望能够根据变量TempInc的值来改变每个标签的前颜色。。。例如:
所以当TempInc=1时,我希望lblsectionsattended1.ForeColor改变
If TempInc = 1 Then
lblSectionsAttempted1.ForeColor = Color.Green
Else if TempInc = 2 Then
lblSectionsAttempted2.ForeColor = Color.Green
然后,当TempInc=2时,我希望lblsectionsatted2.ForeColor改变
If TempInc = 1 Then
lblSectionsAttempted1.ForeColor = Color.Green
Else if TempInc = 2 Then
lblSectionsAttempted2.ForeColor = Color.Green
等等等等
然而,拥有大量的if语句并不理想。请有人告诉我如何重新编写这行代码,使变量的值影响标签的更改
lblSectionsAttempted(TempInc).ForeColor = Color.Green
假设您的标签名为LBLSectionSatTested1到LBLSectionSatTested14,那么我将创建一个控制数组,并使用该数组循环并更新标签的前景色:
' Create the control array
Dim oLabelArray(13) As Label
oLabelArray(0) = Me.lblSectionAttempted1
oLabelArray(1) = Me.lblSectionAttempted2
oLabelArray(2) = Me.lblSectionAttempted3
oLabelArray(3) = Me.lblSectionAttempted4
oLabelArray(4) = Me.lblSectionAttempted5
oLabelArray(5) = Me.lblSectionAttempted6
oLabelArray(6) = Me.lblSectionAttempted7
oLabelArray(7) = Me.lblSectionAttempted8
oLabelArray(8) = Me.lblSectionAttempted9
oLabelArray(9) = Me.lblSectionAttempted10
oLabelArray(10) = Me.lblSectionAttempted11
oLabelArray(11) = Me.lblSectionAttempted12
oLabelArray(12) = Me.lblSectionAttempted13
oLabelArray(13) = Me.lblSectionAttempted14
' Now if you are updating all controls to the color green you can do this:
For TempInc As Int32 = 0 To oLabelArray.Count - 1
oLabelArray(TempInc).ForeColor = Color.Green
Next
您可以使用控件。查找()
将名称与计数器连接起来,然后使用
FindControl
获取该控件,将其转换为标签
并更改前景色。这是WPF、WinForms还是ASP.NET?更改后的颜色将始终为绿色,还是根据TempInc的值而不同?根据TempInc的值,没有红色或绿色@Supersnake@Tim我完全不知道,我只知道我正在使用Visual Studio Express 2012,它是VB.net代码。。。?