我在Panel中动态创建了一组UserControls,代码如下
Dim X As Integer = 4
Dim Y As Integer = 0
For XRule As Integer = 0 To ArrayRuleNames.Length - 1
Dim MyRule As New RuleControl
Dim Location As Point
Location.X = X
Location.Y = Y
With MyRule
.RuleNameGpb.Text = ArrayRuleNames(XRule)
.RuleNumberTxt.Text = ArrayRuleNumbers(XRule)
.RuleNumberTxt.Tag = XRule
.SendBtn.Text = "Read"
.Parent = Me
.Location = Location
.Visible = True
End With
Panel.Controls.Add(MyRule)
Y += 80
Next
每个用户控件内部都有一些对象,如Checkboxes和TextBoxes,我需要设置为True或False Checked属性,并在主窗体(WinForm)的Text属性上写入文本。
所以,我正在考虑在上面的代码中定义一个名称,然后通过名称调用它们,这个概念是正确的还是我应该以不同的方式进行呢?
答案 0 :(得分:1)
为控件命名:
With MyRule
.Name = "MyRule1"
.RuleNameGpb.Text = ArrayRuleNames(XRule)
.RuleNumberTxt.Text = ArrayRuleNumbers(XRule)
然后你可以参考它:
If Panel.Controls.ContainsKey("MyRule1") Then
Dim MyRule As RuleControl = Panel.Controls("MyRule1")