📜  如何在C#中的RadioButton中设置文本?

📅  最后修改于: 2021-05-29 14:51:51             🧑  作者: Mango

在Windows窗体中,RadioButton控件用于在选项组中选择一个选项。例如,从给定列表中选择您的性别,因此您将仅在“男”或“女”或“跨性别”之类的三个选项中选择一个。在Windows窗体中,允许您使用RadioButton的Text属性在RadioButton中添加文本,这使您的RadioButton有意义。您可以通过两种不同的方式设置此属性:

1.设计时:这是在RadioButton中添加文本的最简单方法,如以下步骤所示:

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

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式在RadioButton控件中添加文本:

public override string Text { get; set; }

此属性的值是System.String类型。以下步骤显示了如何在RadioButton中动态添加文本:

  • 步骤1:使用RadioButton类提供的RadioButton()构造函数创建单选按钮。
    // Creating radio button
    RadioButton r1 = new RadioButton();
    
  • 步骤2:创建RadioButton后,设置RadioButton类提供的RadioButton的Text属性。
    // Adding text in the radio button
    r1.Text = "Intern";
    
  • 步骤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 WindowsFormsApp23 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        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";
      
            // 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.Location = new Point(286, 40);
            r1.TextAlign = ContentAlignment.MiddleLeft;
      
            // 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.Text = "Team Leader";
            r2.Location = new Point(356, 40);
            r2.TextAlign = ContentAlignment.MiddleLeft;
      
            // 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.Text = "Software Engineer";
            r3.Location = new Point(470, 40);
            r3.TextAlign = ContentAlignment.MiddleLeft;
      
            // Adding this label to the form
            this.Controls.Add(r3);
        }
    }
    }
    

    输出: