WinForm窗体应用程序学习笔记(二)
在学习笔记(一)中学习了WinForm项目的文件结构、控件的常用属性与事件还创建了第一个WinForm小程序。这次的学习笔记主要学习WinForm常用控件。
友情链接:
WinForm常用控件
1 . Button按钮、TextBox文本框、Label文本标签
这次设计一个登陆网站时,提示用户输入登陆信息,并且点击按钮进行登陆操作的小程序。默认登陆账号为"admin",密码"adminadmin",若账号密码都一样,则可以提示“登陆成功!”;否则提示“用户名或密码错误!”。成品如下图所示:
可以看到有两个熟悉的控件,一个时Button控件(用来响应鼠标的点击)、另一个是Label文本标签(用来显示信息),最后我们需要一个TextBox控件用来接收输入的信息。
偷个懒省个事儿,直接在昨天第一个小程序中创建一个窗体“Form2”,然后将Application.Run()方法中的参数修改为Form2窗体对象。这样点击F5运行时,出现的就是新建的Form2窗体程序了。下图展示了具体操作步骤:
紧接着, 拖动三个控件到到Form1.cs[设计]界面,形成良好的命名习惯,其中两个Button,登陆的命名为“BtnLogin”、重置TextBox的按钮命名为“BtnClear”;两个Lable标签分别命名为“LabId”、“LabPassword”;两个TextBox控件分别命名为“TextId”、“TextPassword”。这里不再给出拖拽和更改文件名的Gif图,直接展示拖拽工具完成的样子……
然后编写与用户交互的逻辑的代码,点击【登陆】那个按钮,然后在右下角的属性上面点击【闪电】模样的按钮,双击“Click”,这时候会在Form2.cs文件下自动生成一个BtnLogin_Click的方法。同样的方法双击【重置】,自动生成BtnClear_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 first
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void BtnLogin_Click(object sender, EventArgs e)
{
//判断输入是否为空
if (!String.IsNullOrEmpty(TextId.Text) && !String.IsNullOrEmpty(TextPassword.Text))
{
if (TextId.Text == "admin" && TextPassword.Text == "adminadmin")
{
MessageBox.Show("登陆成功!");
}
else
{
MessageBox.Show("用户名或密码错误!");
TextPassword.Text = "";
// TextId.Text = "";
}
}
}
private void BtnClear_Click(object sender, EventArgs e)
{
TextPassword.Text = "";
TextId.Text = "";
}
}
}
至此,这个小程序已经完成了!
2 . CheckBox复选框、RadioButton单选按钮
当用户注册信息时,经常会选择兴趣爱好什么的。所以WinForm提供了两个控件,分别是CheckBox复选框,另一个是RadioButton单选按钮。其中:
- CheckBox复选框:用于多项选择,他只有选中和未选中两种状态。当被选中时,其Checked属性为true、反之为false。一个窗体可以包含多个CheckBox控件,并且这些控件可以同时被选中。
- RadioButton单选按钮:包括选中和未选种两种状态,当被选中时,其Checked值为true;反之为false。若一个窗体包含多个RadioButton控件位于同一组,此时只有一个控件被选中。
这次我们设计一个用户注册界面,成品如下:
新建一个窗体Form3,然后在[设计]页面完成以下注册设计(同样为每个控件重命名,其中按钮【注册】:BtnRegis、按钮【重置】:BtnReset,单选框【男】:rdMale,单选框【女】:rdFemale、密码TextBox:Password、重复密码TextBox:RepeatPassword,其他不再贴出):
然后打开代码窗口完善一下代码:
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 first
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
//在主窗体初始化时,设置单选框的默认值
private void Form3_Load(object sender,EventArgs e)
{
}
private void BtnRegis_Click(object sender, EventArgs e)
{
bool flag = false;
foreach(Control item in this.Controls)
{
if(string.IsNullOrEmpty(item.Text))
{
flag = true;
break;
}
}
if (flag && rdMale.Checked==false && rdFemale.Checked==false)
{
MessageBox.Show("请确定已填写全部信息!");
}else if (PassWord.Text!=RepeatPassword.Text)
{
MessageBox.Show("两次密码输入不一致!");
}else{
MessageBox.Show("注册成功!");
}
}
private void Btnreset_Click(object sender, EventArgs e)
{
//遍历窗体中所有控件
foreach(Control item in this.Controls)
{
if(item is TextBox)
{
item.Text = "";
}
if(item is RadioButton)
{
RadioButton r = (RadioButton)item;
r.Checked = false;
// rdMale.Checked = true;
}
if(item is CheckBox)
{
CheckBox c = (CheckBox)item;
c.Checked = false;
}
}
}
}
}
注册页面完成!
部分内容参考自《C#程序设计基础入门教程》——传智播客高教产品研发部