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);
}
}
}
