vb.net 在 VB 中的标签名称中实现变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14126437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing a Variable in a Label Name in VB?
提问by Greg Stratford
I currently have a huge IF statement and want to minimize it as much as possible.
我目前有一个巨大的 IF 语句,并希望尽可能地减少它。
I have a datagrid that i am populating from a SQL query and then from this datagrid i am passing the values into seperate labels.
我有一个从 SQL 查询填充的数据网格,然后从这个数据网格我将值传递到单独的标签中。
I am able to create a For Each Loop in which i cycle through the variables looking until a counter reaches 7. However the problem arises when i need to incremente the Label name values by one. Each time, so essentially i need to add a counter variable into the Label name.
我能够创建一个 For Each 循环,在其中我循环查看变量,直到计数器达到 7。但是,当我需要将标签名称值增加 1 时就会出现问题。每次,基本上我都需要在标签名称中添加一个计数器变量。
The code that i need to minimize is:
我需要最小化的代码是:
result73 = DataGridView1.Rows(0).Cells(0).Value.ToString
result74 = DataGridView1.Rows(0).Cells(1).Value.ToString
result75 = DataGridView1.Rows(1).Cells(0).Value.ToString
result76 = DataGridView1.Rows(1).Cells(1).Value.ToString
result77 = DataGridView1.Rows(2).Cells(0).Value.ToString
result78 = DataGridView1.Rows(2).Cells(1).Value.ToString
result79 = DataGridView1.Rows(3).Cells(0).Value.ToString
result80 = DataGridView1.Rows(3).Cells(1).Value.ToString
result81 = DataGridView1.Rows(4).Cells(0).Value.ToString
result82 = DataGridView1.Rows(4).Cells(1).Value.ToString
result83 = DataGridView1.Rows(5).Cells(0).Value.ToString
result84 = DataGridView1.Rows(5).Cells(1).Value.ToString
result85 = DataGridView1.Rows(6).Cells(0).Value.ToString
result86 = DataGridView1.Rows(6).Cells(1).Value.ToString
result87 = DataGridView1.Rows(7).Cells(0).Value.ToString
result88 = DataGridView1.Rows(7).Cells(1).Value.ToString
If result73 = "Monday" Then
DaySalesLbl1.Text = result74
ElseIf result73 = "Tuesday" Then
DaySalesLbl2.Text = result74
ElseIf result73 = "Wednesday" Then
DaySalesLbl3.Text = result74
ElseIf result73 = "Thursday" Then
DaySalesLbl4.Text = result74
ElseIf result73 = "Friday" Then
DaySalesLbl5.Text = result74
ElseIf result73 = "Saturday" Then
DaySalesLbl6.Text = result74
ElseIf result73 = "Sunday" Then
DaySalesLbl7.Text = result74
End If
This If Statement goes on for each variable that is declared above.
对于上面声明的每个变量,此 If 语句继续执行。
The Loop I have created looks something like this:
我创建的循环看起来像这样:
Dim cou As Integer
Dim n As Integer
cou = 0
n = 1
Do Until result74 <> ""
If result73 = cou Then
DaySalesLbl +n = result74
End If
cou = cou + 1
n = n + 1
Loop
But the section DaySalesLbl +n = result74brings an Error because it looks for a generated method.
但是该部分DaySalesLbl +n = result74带来了一个错误,因为它寻找一个生成的方法。
采纳答案by Steven Doggart
If, as I suspect, this is a WinForm project, then you can access the controls by name using the Form.Controlscollection, like this:
如果,正如我怀疑的那样,这是一个 WinForm 项目,那么您可以使用Form.Controls集合按名称访问控件,如下所示:
Dim cou As Integer
Dim n As Integer
cou = 0
n = 1
Do Until result74 <> ""
If result73 = cou Then
Dim l As Label = CType(Me.Controls("DaySalesLbl" & n), Label)
l.Text = result74
End If
cou = cou + 1
n = n + 1
Loop
The Controlscollection contains all the controls that are direct children of the form. You can access them by index (e.g. Me.Controls(0)), or by name (e.g. Me.Controls("DaySalesLbl6")). However, the collection stores the list as the base Controltype, so you have to cast it to a the specific type before accessing the specific properties. That is why the CType(..., Label)is there in the example.
该Controls集合包含作为窗体的直接子级的所有控件。您可以通过索引(例如Me.Controls(0))或名称(例如Me.Controls("DaySalesLbl6"))访问它们。但是,集合将列表存储为基本Control类型,因此您必须在访问特定属性之前将其转换为特定类型。这就是CType(..., Label)示例中存在 的原因。
回答by John Bustos
I would write code as follows:
我会写代码如下:
For i as Integer = 0 to 7
Select Case DataGridView1.Rows(i).Cells(1).Value.ToString
Case "Monday"
Me.Controls("DaySalesLbl" & i+1).Text = DataGridView1.Rows(i).Cells(2).Value.ToString
Case "Tuesday"
Me.Controls("DaySalesLbl" & i+2).Text = DataGridView1.Rows(i).Cells(2).Value.ToString
... etc ...
I did this without the IDE, so I hope I didn't mess anything up, but I hope this sets you in the right direction and helps.
我在没有 IDE 的情况下做到了这一点,所以我希望我没有搞砸任何事情,但我希望这能让您朝着正确的方向前进并有所帮助。

