GroupBox 分组框控件

GroupBox 控件是由System.Windows.Forms.GroupBox类提供的,作用是为其他控件提供可识别的分组

 

可在同一页面,实现多个单选的RadioButton

 

通常,使用分组框按功能细分窗体,例如,一个学生在璇姐班级和系别时,为了细分窗体,可用两个GroupBox控件来设置,用Text属性来达到分组提示的目的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GroupBox_分组框控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "GroupBox 分组控件";
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string mystr = "";   //定义一个字符串来接受循环遍历后的结果
            foreach (Control outcon in groupBox1.Controls)   //循环遍历control(控件) 在分组框控件中
            {
                if (outcon is RadioButton)   //判断 是不是radiobutton 控件(有可能还要加checkbutton)
                {
                    if (((RadioButton)outcon).Checked)   //判断这个radiobutton是否被选中(checked)前面的(radiobutton)是将outcon这个控件强制转换为radiobutton
                    {
                        mystr = "您的种族是:" + outcon.Text;   //用定义好的变量来接收控件上的txet
                    }
                }
            }
            foreach (Control outcontrol in groupBox2.Controls)
            {

                if (outcontrol is RadioButton)
                {
                    if (((RadioButton)outcontrol).Checked)
                    {
                        mystr="您的所属:"+outcontrol.Text+"\t"+mystr;
                    }
                }
            }
            MessageBox.Show(mystr);
        }
    }
}

  

posted @ 2018-03-22 15:49  WhiteSpace  阅读(7619)  评论(0编辑  收藏  举报