📜  如何在C#中设置RadioButton的名称?

📅  最后修改于: 2021-05-29 18:24:31             🧑  作者: Mango

在Windows窗体中,RadioButton控件用于在选项组中选择一个选项。例如,从给定列表中选择您的性别,因此您将在“男性”或“女性”或“变性者”三个选项中仅选择一个选项。在Windows窗体中,允许您使用RadioButton控件的Name属性为RadioButton命名。您可以通过两种不同的方式设置此属性:

1.设计时:这是将名称设置为RadioButton的最简单方法,如以下屏幕截图所示:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:从工具箱中拖动RadioButton控件,并将其放在Windows窗体上。您可以根据需要将RadioButton控件放置在Windows窗体上的任何位置。
  • 步骤3:拖放之后,您将转到RadioButton控件的属性,以将名称设置为RadioButton。

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式将名称设置为RadioButton:

public string Name { get; set; }

此属性的值是System.String类型,并且此字符串表示RadioButton的名称。以下步骤显示了如何动态设置RadioButton的名称:

  • 步骤1:使用RadioButton类提供的RadioButton()构造函数创建单选按钮。
    // Creating radio button
    RadioButton r1 = new RadioButton();
    
  • 步骤2:创建RadioButton之后,设置RadioButton类提供的RadioButton的Name属性。
    // Setting the name of the radio button
    r1.Name = "My_Radio_1";
    
  • 步骤3:最后使用Add()方法将此RadioButton控件添加到表单中。
    // Add this radio button to the form
    this.Controls.Add(r1);
    

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp24 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void RadioButton2_CheckedChanged(object sender,
                                                  EventArgs e)
        {
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting label
            Label l = new Label();
            l.AutoSize = true;
            l.Location = new Point(176, 40);
            l.Text = "Select Post:";
            l.Font = new Font("Berlin Sans FB", 12);
      
            // Adding this label to the form
            this.Controls.Add(l);
      
            // Creating and setting the 
            // properties of the RadioButton
            RadioButton r1 = new RadioButton();
            r1.AutoSize = true;
            r1.Text = "Intern";
            r1.Name = "My_Radio_1";
            r1.Location = new Point(286, 40);
            r1.Font = new Font("Berlin Sans FB", 12);
      
            // Adding this label to the form
            this.Controls.Add(r1);
      
            // Creating and setting the
            // properties of the RadioButton
            RadioButton r2 = new RadioButton();
            r2.AutoSize = true;
            r2.Name = "My_Radio_2";
            r2.Text = "Team Leader";
            r2.Location = new Point(356, 40);
            r2.Font = new Font("Berlin Sans FB", 12);
            // Adding this label to the form
            this.Controls.Add(r2);
      
            // Creating and setting the
            // properties of the RadioButton
            RadioButton r3 = new RadioButton();
            r3.AutoSize = true;
            r3.Name = "My_Radio_3";
            r3.Text = "Software Engineer";
            r3.Location = new Point(480, 40);
            r3.Font = new Font("Berlin Sans FB", 12);
      
            // Adding this label to the form
            this.Controls.Add(r3);
        }
    }
    }
    

    输出: