分组类控件
分组类控件有容器(Panel)控件、分组框(GroupBox)控件、选项卡(TabControl)控件等。
容器控件
容器控件是由System.Windows.Forms.Panel类提供的。该控件相当于一个容器,主要作用就是将其它控件组合在一起放在一个面板上,使这些控件更容易管理。
编写程序,制作一个“下载资源管理器”。
开发步骤如下:
1、首先在Form1窗体中添加三个Button控件,并修改这些控件的Text属性;然后再添加一个Panel控件,并将该控件的Size属性设置为“307,181”。
2、接着再新建三个窗体,分别 为Regular.cs常规窗体、Download.cs下载窗体和Appearance.cs外观窗体,并且为三个窗体添加相应的控件。这里还需要注意,窗体新建完成之后 ,也需要将它们的Size属性设置为“307,181”。
右键,选择重命名。
3、最后在Form1.cs文件中,添加三个Button控件的Click事件。
代码之前
代码之后:
代码如下:
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Regular myRegular=new Regular();
myRegular.TopLevel = false;
this.panel1.Controls.Add(myRegular);
myRegular.FormBorderStyle= FormBorderStyle.None;
myRegular.BringToFront();
myRegular.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Download myDownload = new Download();
myDownload.TopLevel = false;
this.panel1.Controls.Add(myDownload);
myDownload.FormBorderStyle = FormBorderStyle.None;
myDownload.BringToFront();
myDownload.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Appearance myAppearance = new Appearance();
myAppearance.TopLevel = false;
this.panel1.Controls.Add(myAppearance);
myAppearance.FormBorderStyle = FormBorderStyle.None;
myAppearance.BringToFront();
myAppearance.Show();
}
}
}
运行结果如下:
点击,无反应, 运行失败。
三个窗口重新设计:
再次运行:
分组框控件
分组框控件是由System.Windows.Forms.GroupBox类提供的,作用是为其他控件提供可识别的分组,可在同一页面实现多个单选RadioButton控件。通常,使用分组框来按功能细分窗体。
编写程序,将一个学生的性别和年级进行细分。
在窗体中,首先添加两个GroupBox控件,并命名为“性别”和“年级”;
在“性别”控件中添加两个RadioButton控件,分别为“男”和“女”。在
最后再添加一个Button控件,更名为“提交”。
在代码中,首先添加button1_Click事件,对性别控件进行判断。由于最后输出的是一个字符串,所以用户需要定义一个字符串变量str,来存储该值。接着,使用foreach语句,在groupBox1.Controls集合中,定义一个控件的类outctrl,并判断该控件是否是GroupBox控件;然后再使用if语句来判断GroupBox控件中的哪一个RadioButton控件被选中;最后将outctrl.Text赋给变量str。由于“性别”控件与“年级”控件大致相同,在groupBox2.Controls进行判断即可。
代码之前:
代码之后:
代码如下:
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = "";
foreach(Control outctrl in groupBox1.Controls)
{
if(outctrl is RadioButton)
{
if(((RadioButton)outctrl).Checked)
{
str="您的性别是:"+outctrl.Text;
}
}
}
foreach(Control outctrl in groupBox2.Controls)
{
if(outctrl is RadioButton)
{
if(((RadioButton)outctrl).Checked)
{
str += "\n您所在班级:" + outctrl.Text;
}
}
}
MessageBox.Show(str, "选择结果");
}
}
}
运行结果如下:
选项卡控件
选项卡控件是一个选项卡控件,在实际 编程中经常用到,该控件的作用是将相关的组件组合到一系列选项卡页面上。
1、添加和删除TabControl控件中的选项卡
有两种方法:
第一,当鼠标单击TabControl控件右上角带三角形的小按钮时就会打开一个小窗口,该窗口可以方便地在设计期间添加和删除TabPages集合。
TabControl控件的Multiline属性是用来设置是否显示多行选项卡。如果属性值是false时,就表示有多个选项卡不能一次显示出来,需要提供箭头查看剩余的选项卡。
第二,选中TabControl控件,在属性面板中单击TabPages集合的...按钮,会弹出“TabPage集合编辑器”对话框,并在该对话框中添加和移除TabPages集合。
2、以编程方式添加和删除选项卡
默认情况下,TabControl控件包含两个TabPage控件,可以使用TabPage属性的Add方法和Remove方法实现选项卡的添加和删除。
编写程序,实现选项卡的添加和删除
添加一个TabControl控件
添加两个Button控件,更名为“添加”和“删除”。
两个Button控件发生Click事件
代码如下:
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//获取选择条中选项卡的数目
string name = "新增选项卡" + (tabControl1.TabCount + 1).ToString();
//实例化TabPage控件
TabPage tab = new TabPage(name);
//使用TabControl控件的TabPages属性的Add方法添加新选项卡
tabControl1.TabPages.Add(tab);
}
private void button2_Click(object sender, EventArgs e)
{
//获取当前选项卡页的索引值,并进行判断
if(tabControl1.SelectedIndex==0)
{
MessageBox.Show("请选择要删除的选项卡");
}
else
{
//使用Remove方法删除指定选项卡
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
}
}
}
运行结果:
点击添加:
点击添加:
点击删除:
点击“新增选择卡3”,再次点击删除:
点击tabPage1,再次删除。还是不行。tabpage2是可以的。