📜  C#| GroupBox类别

📅  最后修改于: 2021-05-29 22:29:04             🧑  作者: Mango

在Windows窗体中,GroupBox是一个包含多个控件的容器,并且这些控件彼此相关。换句话说,GroupBox是带有适当的可选标题的一组控件周围的框架显示。或者使用GroupBox将相关控件分类到一个组中。 GroupBox类用于表示Windows组框,还提供不同类型的属性,方法和事件。它在System.Windows.Forms命名空间下定义。组框的主要用途是保存RadioButton控件的逻辑组。

在C#中,您可以使用两种不同的方式在Windows窗体中创建GroupBox:

1.设计时:这是创建GroupBox的最简单方法,如以下步骤所示:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:接下来,将GroupBox从窗体上的工具箱中拖放。

  • 步骤3:拖放之后,您将转到GroupBox的属性,以根据需要修改GroupBox。

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助GroupBox类提供的语法以编程方式创建GroupBox。以下步骤显示如何动态设置创建GroupBox:

  • 步骤1:使用GroupBox类提供的GroupBox()构造函数创建GroupBox。
    // Creating a GroupBox
    GroupBox box = new GroupBox(); 
    
  • 步骤2:创建GroupBox之后,设置GroupBox类提供的GroupBox的属性。
    // Setting the location of the GroupBox
    box.Location = new Point(179, 145);
    
    // Setting the size of the GroupBox
    box.Size = new Size(329, 94);
    
    // Setting text the GroupBox
    box.Text = "Select Gender";
    
    // Setting the name of the GroupBox
    box.Name = "MyGroupbox";
    
  • 步骤3:最后,将此GroupBox控件添加到表单中,并使用以下语句在GroupBox上添加其他控件:
    // Adding groupbox in the form
    this.Controls.Add(box);
    
    and 
    
    // Adding this control to the GroupBox
    box.Controls.Add(b2);
    

    例子:

    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 WindowsFormsApp45 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting 
            // properties of the GroupBox
            GroupBox box = new GroupBox();
            box.Location = new Point(179, 145);
            box.Size = new Size(329, 94);
            box.Text = "Select Gender";
            box.Name = "MyGroupbox";
      
            // Adding groupbox in the form
            this.Controls.Add(box);
      
            // Creating and setting 
            // properties of the CheckBox
            CheckBox b1 = new CheckBox();
            b1.Location = new Point(40, 42);
            b1.Size = new Size(49, 20);
            b1.Text = "Male";
      
            // Adding this control 
            // to the GroupBox
            gbox.Controls.Add(b1);
      
            // Creating and setting 
            // properties of the CheckBox
            CheckBox b2 = new CheckBox();
            b2.Location = new Point(183, 39);
            b2.Size = new Size(69, 20);
            b2.Text = "Female";
      
            // Adding this control
            // to the GroupBox
            box.Controls.Add(b2);
        }
    }
    }
    

    输出:

建设者

Constructor Description
GroupBox() This Constructors is used to initializes a new instance of the GroupBox class.

特性

Property Description
AutoSize This property is used to get or set a value that indicates whether the control resizes based on its contents.
AutoSizeMode This property indicates how the GroupBox behaves when its AutoSize property is enabled.
BackColor This property is used to get or set the background color for the control.
BorderStyle This property indicates the border style for the control.
DisplayRectangle This property is used to get a rectangle that represents the dimensions of the GroupBox.
Font This property is used to get or set the font of the text displayed by the control.
ForeColor This property is used to get or set the foreground color of the control.
Height This property is used to get or set the height of the control.
Location This property is used to get or set the coordinates of the upper-left corner of the GroupBox control relative to the upper-left corner of its form.
Name This property is used to get or set the name of the control.
TabStop This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the GroupBox.
Size This property is used to get or set the height and width of the control.
Visible This property is used to get or set a value indicating whether the control and all its child controls are displayed.
Width This property is used to get or set the width of the control.