1、熟悉C#中的WinForm程序设计,理解窗体和控件的属性、方法和事件。
2、设计一个计算1~10之间的加法运算的小程序,了解Form(窗体对象)、Label(标签对象)、TextBox(文本框对象)、Button(按钮对象)的属性设置,事件方法内容的编写,掌握WinForm程序开发的基本流程。
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 Demo_Math
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Next_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txt_a.Text);
int b = Convert.ToInt32(txt_b.Text);
int x = Convert.ToInt32(txt_c.Text);
if (x == a+b)
{
lbl_Msg.Text = "你答对了,真棒!";
}
else
{
lbl_Msg.Text = "你答错了,加油!";
txt_c.Focus();
}
}
}
}
3、改进上例程序,让程序拥有随机出题的功能。
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 Demo_MathRandom
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Next_Click(object sender, EventArgs e)
{
int a, b;
Random r = new Random();
a = r.Next(1, 10);
b = r.Next(1, 10);
txt_a.Text = Convert.ToString(a);
txt_b.Text = Convert.ToString(b);
txt_c.Text = "";
lbl_Msg.Text = "";
if (txt_c.CanFocus)
txt_c.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txt_a.Text);
int b = Convert.ToInt32(txt_b.Text);
int x = Convert.ToInt32(txt_c.Text);
if (x == a+b)
{
lbl_Msg.Text = "你答对了,真棒!";
}
else
{
lbl_Msg.Text = "你答错了,加油!";
txt_c.Focus();
}
}
private void Form1_Load(object sender, EventArgs e)
{
btn_Next_Click(sender, e);
}
}
}
4、设计一个登录窗口,要求如下:
a.窗体运行时要处于屏幕的中央,并且不能够被最大化和最小化,也不能够改变大小
b.用户名和密码的长度限制在8位以内
c.点击“登录”按钮或按下“Enter”键后开始登录验证
d.点击“取消”按钮或按下“Esc”键后退出
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 Demo_LoginForm
{
public partial class LoginForm : Form
{
private string _UserName = "admin";
private string _UserPwd = "123456";
public LoginForm()
{
InitializeComponent();
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
txt_UserName.Text = "";
txt_UserPwd.Text = "";
this.Close();
}
private void btn_Login_Click(object sender, EventArgs e)
{
string userName = txt_UserName.Text;
string userPwd = txt_UserPwd.Text;
if ((userName==_UserName) && (userPwd ==_UserPwd))
{
MessageBox.Show(this, "用户名和密码验证通过,登录成功!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(this, "用户名和密码验证失败,请检查后重试!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
txt_UserName.Focus();//控件获取焦点
}
}
private void txt_UserName_TextChanged(object sender, EventArgs e)
{
btn_Login.Enabled = (txt_UserName.Text != "") && (txt_UserPwd.Text != "");
}
private void txt_UserPwd_KeyPress(object sender, KeyPressEventArgs e)
{
//此事件与KeyDown事件只需其一即可,不可两者均写
//如果在窗体的【杂项】属性中设置了【AcceptButton】和【CancelButton】属性,那么就不需要此事件
if (e.KeyChar == 13) //回车键 或者在KeyDown事件中写成 if (e.KeyCode == Keys.Enter)
btn_Login_Click(sender, e);
else if (e.KeyChar == 27) //ESC键
btn_Cancel_Click(sender, e);
}
private void txt_UserPwd_KeyDown(object sender, KeyEventArgs e)
{
//此事件与KeyPress事件只需其一即可,不可两者均写
if (e.KeyCode == Keys.Enter) //回车键
btn_Login_Click(sender, e);
else if (e.KeyCode == Keys.Cancel) //ESC键
btn_Cancel_Click(sender, e);
}
}
}
回车键和取消键的功能,也可以在设计阶段设置Form1.AcceptButton和Form1.CancelButton属性,效果比上述代码更好,动作也更合理。
5、演示DEMO源代码在github上的仓库地址:
https://github.com/xieyunc/csharp_teach.git