动态控件嵌套动态Radiobutton 取值问题 (winform)
For 1 To 3
Dim tp As TabPage = New TabPage()
Dim rd1 As RadioButton = New RadioButton()
Dim rd2 As RadioButton = New RadioButton()
rd1.Text = "Model1 "
rd2.Text = "Model2 "
rd1.Location = New Point(10, 10)
rd2.Location = New Point(10, 30)
TabControl1.TabPages.Add( "File " & (numforNB + 1).ToString())
TabControl1.TabPages(numforNB).Controls.Add(rd1)
TabControl1.TabPages(numforNB).Controls.Add(rd2)
Next
问题1;动态控件在FORM_LOAD生成,要将每一个TabPages下选择的RadioButton的状态 存到数据库里,怎么拿到每一个TabPages下RadioButton checked 属性?
问题2:VB.NET里怎么写动态RadioButton的Click事件?rd1.Click是没有的.
[解决办法]
你应该在VB.NET问,这里一般都用C#,对VB语法不熟,我只能用C#语法回答你了
问题1:枚举TablPage下的所有控件,对每个控件进行判断
foreach(Control ct in TableControl1.TabPages(numforNB).Controls)
{
if(ct.GetType() == typeof(RadioButton))
{
MessageBox.Show(((RadioButton)ct).Checked.ToString());
}
}
问题2:
rd1.Click += new EventHandler(rd_Click);
然后创建一个处理事件的方法
private void rd_Click(object sender,EventArgs e)
{
MessageBox.Show( "Click ");
}
[解决办法]
问题2应该这样吧:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rd1 As RadioButton = New RadioButton()
AddHandler rd1.Click, AddressOf rdClick
End Sub
Private Sub rdClick(ByVal send As Object, ByVal e As System.EventArgs)
……
End Sub