【问题标题】:Check which Radio Button is checked in C#, with dynamically created radiobuttons使用动态创建的单选按钮检查在 C# 中选中了哪个单选按钮
【发布时间】:2021-02-22 17:35:06
【问题描述】:

我找不到有关此问题的任何解决方案或提示。此代码后描述的问题。

我必须为在特定路径上找到的每个文件夹创建一个图片框和单选按钮:

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

如果我尝试使用创建的单选按钮的名称而不是 ???它给了我一个像'元素???没有 Checked 或 Tag 属性'

【问题讨论】:

    标签: c# visual-studio radio-button


    【解决方案1】:

    在下方添加方法

    添加到 For 循环:rdo.CheckedChanged += new EventHandler(radioButton_CheckedChanged);

           private void radioButton_CheckedChanged(object sender, EventArgs e)
            {
                RadioButton button = sender as RadioButton;
    
                string name = button.Text;
    
            }
    

    【讨论】:

      【解决方案2】:

      RadioButtons 与任何其他控件一样存储在其容器的 Controls 集合中。如果您将它们直接添加到表单中,则可以使用此代码检索它们。

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

      【讨论】:

        【解决方案3】:

        单选按钮默认添加到同一个组中,因此您可以按如下方式获取选中的单选按钮。

        List<RadioButton> radioButtons = this.Controls.OfType<RadioButton>().ToList();
        RadioButton rb = radioButtons
                .Where(r => r.Checked) 
                .Single();
        string tag = rb.Tag.ToString();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-12
          • 1970-01-01
          • 2019-01-13
          • 2016-02-01
          • 2012-04-17
          • 1970-01-01
          • 1970-01-01
          • 2022-09-22
          相关资源
          最近更新 更多