Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 访问表单';s按名称控制_Vb.net_Winforms - Fatal编程技术网

Vb.net 访问表单';s按名称控制

Vb.net 访问表单';s按名称控制,vb.net,winforms,Vb.net,Winforms,不确定这篇文章的标题是否准确。 我试图通过在循环中“组合”窗体控件的名称来访问windows窗体控件及其属性,但似乎找不到相关文档。使用VB.net。基本上,假设我有以下几点: Dim myDt As New DataTable Dim row As DataRow = myDt.NewRow() row.Item("col01") = Me.label01.Text row.Item("col02") = Me.label02.Text '... row.Item("colN") = Me

不确定这篇文章的标题是否准确。 我试图通过在循环中“组合”窗体控件的名称来访问windows窗体控件及其属性,但似乎找不到相关文档。使用VB.net。基本上,假设我有以下几点:

Dim myDt As New DataTable

Dim row As DataRow = myDt.NewRow()

row.Item("col01") = Me.label01.Text
row.Item("col02") = Me.label02.Text
'...
row.Item("colN") = Me.labelN.Text
我想写一个for循环,而不是N个单独的指令。 虽然它很简单,可以表达作业的左侧,但说到右侧,我感到很困惑:

For i As Integer = 1 to N
    row.Item(String.format("col{0:00}", i)) = ???
    ' ??? <- write "label" & i (zero-padded, like col) and use that string to access Me's control that has such name
Next
对于i作为整数=1到N
row.Item(String.format(“col{0:00}”,i))=???
' ???  您可以使用将
searchAllChildren
选项设置为true的方法

For i As Integer = 1 to N
    Dim ctrl = Me.Controls.Find(string.Format("label{0:00}", i), True)
    if ctrl IsNot Nothing AndAlso ctrl.Length > 0 Then
        row.Item(String.format("col{0:00}", i)) = ctrl(0).Text
    End If
Next
关于如何使用反射来设置使用字符串标识的属性来解决此问题的示例

Dim myLabel As Label = new Label()
Dim prop as PropertyInfo = myLabel.GetType().GetProperty("Text")
prop.SetValue(myLabel, "A Label.Text set with Reflection classes", Nothing)
Dim newText = prop.GetValue(myLabel)
Console.WriteLine(newText)

注意:我试过使用
Me.Controls(“label”&I)
但对我不起作用!可能是因为标签嵌套在其他控件中(例如XtraTabControls中的XtraTabPages等…),最好避免隐藏bug。例外情况是件好事。现在,如果我可以通过名称(也作为字符串传递)访问控件的属性,那就太棒了。而不是
ctrl(0).Text
类似于
ctrl(0).GetProperty(“Text”)
,因为我要获取的属性取决于
I
的值(因此将是这样的排序:
ctrl(0).GetProperty(f(I))
,其中
f
返回一个字符串)。干杯。谢谢@Steve,我们会调查的