📌  相关文章
📜  如何在C#中设置RadioButton的复选框的对齐方式?

📅  最后修改于: 2021-05-30 00:04:57             🧑  作者: Mango

在Windows窗体中,RadioButton控件用于在选项组中选择一个选项。例如,从给定列表中选择您的性别,因此您将仅在“男”或“女”或“跨性别”之类的三个选项中选择一个。在Windows窗体中,允许您使用RadioButton的CheckAlign属性来调整RadioButton复选框的对齐方式。
此属性的值由ContentAlignment枚举指定,它包含9种不同类型的复选框对齐值,即BottomCenter,BottomLeft,BottomRight,MiddleCenter,MiddleLeft,MiddleRight,TopCenter,TopLeft和TopRight。此属性的默认值为MiddleLeft 。您可以通过两种不同的方式设置此属性:

1.设计时:这是设置RadioButton复选框的对齐方式的最简单方法,如以下步骤所示:

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

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式设置RadioButton控件的复选框的对齐方式:

public System.Drawing.ContentAlignment CheckAlign { get; set; }

在这里,ContentAlignment是ContentAlignment的值。如果该值不属于ContentAlignment的值,它将抛出InvalidEnumArgumentException。以下步骤显示了如何动态设置RadioButton复选框的对齐方式:

  • 步骤1:使用RadioButton类提供的RadioButton()构造函数创建单选按钮。
    // Creating radio button
    RadioButton r1 = new RadioButton();
    
  • 步骤2:创建RadioButton后,设置RadioButton类提供的RadioButton的CheckAlign属性。
    // Setting the alignment of the checkbox of the radio button
    r1.CheckAlign = ContentAlignment.BottomCenter;
    
  • 步骤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.BackColor = Color.LightSteelBlue;
            r1.Padding = new Padding(6, 6, 6, 6);
            r1.CheckAlign = ContentAlignment.BottomCenter;
      
            // Adding this label to the form
            this.Controls.Add(r1);
      
            // Creating and setting the 
            // properties of the RadioButton
            RadioButton r2 = new RadioButton();
            r2.Text = "Team Leader";
            r2.Location = new Point(400, 40);
            r2.AutoSize = true;
            r2.BackColor = Color.LightSteelBlue;
            r2.Padding = new Padding(6, 6, 6, 6);
            r2.CheckAlign = ContentAlignment.BottomCenter;
      
            // Adding this label to the form
            this.Controls.Add(r2);
        }
    }
    }
    

    输出: