Vb.net 调用动态创建的特定用户控件
我在一个面板中动态创建了一组UserControls,代码如下Vb.net 调用动态创建的特定用户控件,vb.net,winforms,user-controls,Vb.net,Winforms,User Controls,我在一个面板中动态创建了一组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
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
在每个用户控件内部都有一些对象,如复选框和文本框,我需要将Checked属性设置为True或False,并从主窗体(WinForm)在text属性上写入文本
因此,我想在上面的代码中定义一个名称,然后按名称调用它们,这个概念正确吗,或者我应该用另一种方式来实现它?给控件一个名称:
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")