C#需要动态创建单选按钮并判断用户在Winform中选择了哪个值

发布于 2024-09-11 13:19:51 字数 340 浏览 0 评论 0原文

我需要基于动态列表动态创建单选按钮。场景就像我在 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);                  
}

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

I need to dynamically create radio buttons based on dynamic list. Scenario is like I have list of files shown as Radio button in WinForm. A user clicks on radio button to select file and move forward.
I tried doing following as an example

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);                  
}

The problem is how would I check which value got selected by user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光病人 2024-09-18 13:19:51

一种简单的方法是使用 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;
}

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

A simple way to do it is by using the RadioButtons CheckChanged event to set a variable that specifies the file that they have chosen by using the RadioButtons text or Tag property which you could set to be the file itself?

e.g.

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;
}

It's certainly not the most elegant way but it would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文