如何引用动态创建的控件 [英] how to refer a dynamically created control
本文介绍了如何引用动态创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我动态创建了一组单选按钮.代码如下:
I have dynamically created a set of radiobuttons.The code is as below:
Dim y As Integer = 10
Dim sName As String = "test"
For x As Integer = 1 To y
myRB = New RadioButton()
myRB.Location = New Point(10, y)
myRB.Text = "Candy " & x
myRB.Name = x
Panel1.Controls.Add(myRB)
y += 20
Next
我想记录用户选择的内容,但是我应该如何引用这些控件?
请帮助
i want to record what the user select,but how should i refer these control?
please help
推荐答案
然后在您在RadioButtons列表或数组中创建它们(推荐)时使用它们,也可以通过Panel1.Controls数组找到它们并进行迭代:
Either store then when you create them in a list or array of RadioButtons (recommended) or you can find them via the Panel1.Controls array and iterate through:
For Each c As Control In panel1.Controls
Dim rb As RadioButton = TryCast(c, RadioButton)
If rb IsNot Nothing Then
If rb.Checked Then
' Handle checked button
Exit For
End If
End If
Next
如果选择第二个选项,则将一个独特的值添加到Tag属性中,否则您将不得不检查Text以确定它是哪个.
If you chose the second option, I would put a distinctive value into the Tag property, or you will have to check the Text to tell which one it is.
这篇关于如何引用动态创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文