控件分类:
基本上常用的控件可以分为六类:
1、文本类控件:在控件上显示文本,如TextBox Label等;
2、选择类控件:主要为用户提供选择的项目,如RadioButton、CheckBox等;
3、分组控件:可以将窗体中其他的控件进行分组管理,如GroupBox Panel等;
4、菜单控件:为系统制作功能菜单,如菜单栏,右键快捷菜单;
5、工具栏控件:提供主菜单中常用的相关工具;
6、状态栏控件:显示应用程序的相关信息
详细笔记:
文本框组件textBox:
1、将ReadOnly属性设置为true,则为只读文本框,用户不可编辑;
2、将UseSystemPassWordChar属性设置为true进行文本加密,默认文本加密为黑色实心原点;
3、将PassWordChar属性输入用户指定符号,比如*号,则文本加密显示该符号;
4、将Multiline属性设置为true,则变为多行文本框
5、在Form窗体设置属性AcceptButton:选择指定的按钮Button,则窗体运行时,回车键触单击发该按钮Button;
6、在Form窗体设置属性cancelButton:选择指定的按钮Button,则窗体运行时,按Esc键触发单击该Button;
7、背景图片BackgroundImageLayout属性为:Tile时,背景图片重复平铺,换成Center或Stretch都不重复平复图片。
菜单和工具栏:
8、下拉式菜单MenuStrip,在文本内容设置时,后面加(&+字母)设置快捷键,ALT+该字母触发快捷键。
9、弹出式菜单ContextMenuStrip,将Form的ContextMenuStrip属性选定对应的弹出菜单,才能让窗体和弹出菜单相互关联,鼠标右键时才会触发。
10、工具栏toolStrip,右键鼠标选择插入标准项,产生标准工具按钮。
11、工具栏的Button默认只能显示图片,我们将DisplayStyle属性更改为ImageAndText,则图文都会显示。
12、状态栏StatusStrip,显示窗体右下角的状态。
选择类控件分为:
13、下拉组合框控件ComboBox,复选框控件CheckBox,单选按钮控件RadioButton;
14、下拉组合框控件ComboBox可以通过在设计器中直接通过编辑项添加选项内容;
也可用代码实现所有内容:
15、ComboBoxName.Items.Add(“string”);添加string选项
16、ComboBoxName.Items.Clear();清除所有项
17、ComboBoxName.SelectedIndex = 0;默认选择第一项
18、设置DropDownStype属性为DropDownList,则为不可编辑式下拉组合框控件
19、单选按钮控件RadioButto默认一个Form只能选中一个,我们通过容器中的面板Panel将Radio分组,不同组都可以单选一个按钮。
我们用这些知识点试着做一个窗体应用:
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 Until5_Windows基础控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text != textBox3.Text)
{
MessageBox.Show("第一次密码和第二次密码不同");
return;
}
string sex = "";
if (radioButton1.Checked)
{
sex = radioButton1.Text;
}else if (radioButton2.Checked)
{
sex = radioButton2.Text;
}
else if(radioButton3.Checked)
{
sex = radioButton3.Text;
}
string age = "";
if (radioButton4.Checked)
{
age = radioButton4.Text;
}
else if (radioButton5.Checked)
{
age = radioButton5.Text;
}
else if (radioButton6.Checked)
{
age = radioButton6.Text;
}
else if (radioButton7.Checked)
{
age = radioButton7.Text;
}
string content = string.Format("欢迎用户‘{0}’登录成功\n详细信息:\n性别:{1}\n年龄阶段:{2}", comboBox1.Text, sex, age);
MessageBox.Show(content,"信息提示系统",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
}
}
总结:
这一章节Windows基础控件的主要内容都是属性设置,因此代码并不复杂,只要频繁的接触这些控件及其属性,掌握这一知识点还是不难的。