📜  如何在C#中设置RadioButton的可见性?

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

在Windows窗体中,RadioButton控件用于在选项组中选择一个选项。例如,从给定列表中选择您的性别,因此您将仅在“男”或“女”或“跨性别”之类的三个选项中选择一个。在Windows窗体中,允许您使用RadioButton的Visible属性调整RadioButton的可见性。
如果此属性的值设置为true,则RadioButton在窗体上可见;如果此属性的值设置为false,则RadioButton在窗体上不可见。此属性的默认值为true。您可以通过两种不同的方式设置此属性:

1.设计时:这是设置RadioButton可见性的最简单方法,如以下步骤所示:

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

    输出:

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

public bool Visible { get; set; }

此属性的值是System.Boolean类型。以下步骤显示了如何动态设置RadioButton的可见性:

  • 步骤1:使用RadioButton类提供的RadioButton()构造函数创建单选按钮。
    // Creating radio button
    RadioButton r1 = new RadioButton();
    
  • 步骤2:创建RadioButton后,设置RadioButton类提供的RadioButton的Visible属性。
    // Setting the visibility of the radio button
    r1.Visible = true;
    
  • 步骤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.Visible = true;
      
            // 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.Visible = true;
      
            // 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.Visible = false;
      
            // Adding this label to the form
            this.Controls.Add(r3);
        }
    }
    }
    

    输出:

    在设置单选按钮的可见性之前:

    设置单选按钮的可见性后: