Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用动态创建的单选按钮检查在C#中选中的单选按钮_C#_Visual Studio_Radio Button - Fatal编程技术网

使用动态创建的单选按钮检查在C#中选中的单选按钮

使用动态创建的单选按钮检查在C#中选中的单选按钮,c#,visual-studio,radio-button,C#,Visual Studio,Radio Button,我在这个问题上找不到任何解决办法或提示。此代码后面描述的问题 protected void Button1_Click(object sender, EventArgs e) { var radio = this.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked); if(radio != null) { string tag = radio.Tag.ToString

我在这个问题上找不到任何解决办法或提示。此代码后面描述的问题

protected void Button1_Click(object sender, EventArgs e)
{
    var radio = this.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
    if(radio != null)
    {
        string tag = radio.Tag.ToString();
        .....
        // Form2 = new Form2(tag);

    }
}
我必须为在特定路径上找到的每个文件夹创建一个picturebox和radiobutton:

        {
            InitializeComponent();
            string pathtocircuits = "../../tracks";
            string[] allfiles = Directory.GetDirectories(pathtocircuits, "*.*", SearchOption.TopDirectoryOnly);
            int imgx = 387;
            int imgy = 153;
            int radx = 428; 
            int rady = 259;
            String track = "";
            String pici = "";
            String pic = "pictureBox";
            String rad = "radiobutton";
            String radr = "";
            String picr = "";
            
            foreach (String file in allfiles)
            {   
                track = Path.GetFileName(file);
                pici = "../../tracks/" + track + "/p_" + track + ".png";
                picr = pic + element.ToString();
                radr = rad + element.ToString(); 
                PictureBox pb = new PictureBox();
                pb.Location = new System.Drawing.Point(imgx, imgy); ;
                pb.Image = Image.FromFile(pici);
                pb.Width = 100;
                pb.Height = 100;
                pb.SizeMode = PictureBoxSizeMode.StretchImage;
                pb.Name = picr;
                Controls.Add(pb);

                RadioButton rdo = new RadioButton();
                rdo.Name = radr;
                rdo.Text = "";
                rdo.Tag = track;
                rdo.Location = new Point(radx, rady);
                this.Controls.Add(rdo);
                

                element += 1;
                imgx += 110;
                radx += 110;
            }
            
        }
通过这一部分,我可以创建我需要的元素(它可以工作)

我的问题是当我按下按钮到达Form2时。如何检查选中的单选按钮并将其标记值存储在字符串中?

for(int i = 0; i<element; i++)
            {
                if( ??? .Checked == true )
                {
                     globalstring = ??? .Tag;
                }
            }
for(int i=0;i在下面添加方法

添加到For循环:rdo.CheckedChanged+=新事件处理程序(radioButton_CheckedChanged)


单选按钮和其他控件一样存储在其容器的控件集合中。如果直接将其添加到表单中,则可以使用此代码检索它们

protected void Button1_Click(object sender, EventArgs e)
{
    var radio = this.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
    if(radio != null)
    {
        string tag = radio.Tag.ToString();
        .....
        // Form2 = new Form2(tag);

    }
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
var radio=this.Controls.OfType().FirstOrDefault(x=>x.Checked);
如果(收音机!=null)
{
string tag=radio.tag.ToString();
.....
//Form2=新Form2(标签);
}
}

默认情况下,radiobutton添加到同一组中,因此您可以按如下方式获取选中的radiobutton

List<RadioButton> radioButtons = this.Controls.OfType<RadioButton>().ToList();
RadioButton rb = radioButtons
        .Where(r => r.Checked) 
        .Single();
string tag = rb.Tag.ToString();
List radioButtons=this.Controls.OfType().ToList();
RadioButton rb=单选按钮
.其中(r=>r.Checked)
.Single();
string tag=rb.tag.ToString();