【问题标题】:C# need to dynamically create radio buttons and determine which value user selected in WinformC#需要动态创建单选按钮并确定用户在Winform中选择了哪个值
【发布时间】:2013-09-15 11:09:12
【问题描述】:

我需要根据动态列表动态创建单选按钮。场景就像我在 WinForm 中显示为单选按钮的文件列表。用户单击单选按钮以选择文件并继续前进。 我试着做下面的例子

for (int i = 0; i < 10; i++)  
{     
    ii = new RadioButton();  
    ii.Text = i.ToString();  
    ii.Location = new Point(20, tt);  
    tt = tt + 20;  
    panel1.Controls.Add(ii);                  
}

问题是如何检查用户选择了哪个值?

【问题讨论】:

    标签: c# radio-button


    【解决方案1】:

    一个简单的方法是使用RadioButtons CheckChanged 事件来设置一个变量,该变量通过使用RadioButtons 文本或Tag 属性来指定他们选择的文件,您可以将其设置为文件本身?

    例如

    private File f = null;
    
    for (int i = 0; i < 10; i++)
    {
        ii = new RadioButton();
        ii.Text = i.ToString();
        ii.Location = new Point(20, tt);
        ii.Tag = fileArray[i]; // Assuming you have your files in an array or similar
        ii.CheckedChanged += new System.EventHandler(this.Radio_CheckedChanged);
        tt = tt + 20;
        panel1.Controls.Add(ii);
    }
    
    private void Radio_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton r = (RadioButton)sender;
        f = (File)r.Tag;
    }
    

    这当然不是最优雅的方式,但它会起作用。

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2011-07-12
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多