winForm实例

 

//简单计算器(加法):

 1 private void button1_Click(object sender, EventArgs e)
 2         {
 3             string str1 = textBox1.Text;
 4             string str2 = textBox2.Text;
 5             int i1, i2;//接受值
 6             if (int.TryParse(str1, out i1))
 7             {
 8                 MessageBox.Show("第一个数不是合法的整数");//弹出窗口信息
 9                 
10             }
11             if (int.TryParse(str2, out i2) == false)
12             {
13                 MessageBox.Show("第二个数不是合法的整数");
14                 return;//不要忘了return
15             }
16             int i3 = i1 + i2;
17             textBox3.Text = Convert.ToString(i3);//i3.ToString();
18         }

//统计成绩:

 

 1 //string s = txt成绩.Text;//方法1,按照\r\n进行split
 2             string[] lines= txt成绩.Lines;
 3             string maxName = "";
 4             int maxScore = -1;
 5             foreach (string line in lines)
 6             {
 7                 string[] strs = line.Split('=');
 8                 string name = strs[0];
 9                 string strScore = strs[1];
10                 int score = Convert.ToInt32(strScore);
11                 if (score > maxScore)
12                 {
13                     maxName = name;
14                     maxScore = score;
15                 }
16             }
17             MessageBox.Show(string.Format("{0}是第一名,成绩{1}", maxName, maxScore));

//身份证判断

 1 string 身份证号 = textBox1.Text;
 2             //校验是否是合法的身份证号  不考虑X
 3             //131226198105223452
 4             pictureBox1.Visible = true;
 5             string strYear = 身份证号.Substring(6, 4);//从6开始 取长度为4
 6             int year = Convert.ToInt32(strYear);
 7             if (DateTime.Now.Year - year > 18)
 8             {
 9                 pictureBox1.Visible = true;
10             }
11             else
12             {
13                 pictureBox1.Visible = false;
14             }

//累加

 1  private void button1_Click(object sender, EventArgs e)
 2         {
 3             string s1 = textBox1.Text;
 4             string s2 = textBox2.Text;
 5             int i1, i2;
 6             if (int.TryParse(s1, out i1) == false)
 7             {
 8                 MessageBox.Show("数字1格式错误");
 9             }
10             if (int.TryParse(s2, out i2) == false)
11             {
12                 MessageBox.Show("数字1格式错误");
13             }
14             int sum = 0;
15             for (int i = 0; i <= i2; i++)
16             {
17                 sum += i;// sum=sum+i
18             }
19             textBox3.Text = Convert.ToString(sum);
20         }

//省市选择器

 1  private void cb省_SelectedIndexChanged(object sender, EventArgs e)
 2         {
 3             cb市.Items.Clear();//清空旧数据
 4             string 省 = Convert.ToString(cb省.SelectedItem);
 5             if (省 == "山东")
 6             {
 7                 cb市.Items.Add("滩坊");
 8                 cb市.Items.Add("临沂");
 9                 cb市.Items.Add("青岛");
10             }
11             if (省 == "河南")
12             {
13                 cb市.Items.Add("郑州");
14                 cb市.Items.Add("三门峡");
15                 cb市.Items.Add("洛阳");
16             }
17             if (省 == "辽宁")
18             {
19                 cb市.Items.Add("沈阳");
20                 cb市.Items.Add("鞍山");
21                 cb市.Items.Add("本溪");
22             }
23             //cb市.Items.Add("aaaaaa");
24         }

//登录设置

 1 public partial class Form1 : Form
 2     {
 3         private int ErrorTimes = 0;//类的变量
 4         public Form1()
 5         {
 6 
 7             InitializeComponent();
 8         }
 9 
10         private void btnLogin_Click(object sender, EventArgs e)
11         {
12             string username = txtUserName.Text.Trim();//Trim禁止用户帐号为空格
13             string password = txtPassword.Text;
14             if (username.Equals("admin", StringComparison.OrdinalIgnoreCase) && password == "888888")
15             {
16                 MessageBox.Show("登录成功");
17             }
18             else
19             {
20                 /*
21                 int i = 0;
22                 i++;
23                 if (i >= 3)
24                 {
25                     MessageBox.Show("错误次数过多,程序即将退出!");
26                     Application.Exit();
27                 }*/
28                 ErrorTimes++;
29                 //局部变量每次运行完毕变量的值都会被销毁,下次再运行,会重新初始化。而类的字段,只要是一个对象,那么只要对象不销毁,就会一直保持对象的字段值
30                 if (ErrorTimes >= 3)
31                 {
32                     MessageBox.Show("错误次数过多,程序即将退出!");
33                     Application.Exit();
34 
35                     MessageBox.Show("登录失败");
36                 }
37             }

//字体滚动

1  private void button1_Click(object sender, EventArgs e)
2         {
3             string str = textBox1.Text;
4             char first = str[0];//取第一个字符 char类型
5             string 剩下 = str.Substring(1);//取剩下的部分 1到 最后一个部分
6             textBox1.Text = 剩下 + first;
7 
8         }

//四则运算器

 1 private void btnResult_Click(object sender, EventArgs e)
 2         {
 3             string str1 = txtNumber1.Text;
 4             string str2 = txtNumber2.Text;
 5             int i1 = Convert.ToInt32(str1);
 6             int i2 = Convert.ToInt32(str2);
 7             int result;
 8             switch (cb操作符.SelectedIndex)
 9             { 
10                 case 0://+
11                     result = i1 + i2;
12                     break;
13                 case 1://-
14                     result = i1 - i2;
15                     break;
16                 case 2://*
17                     result = i1 * i2;
18                     break;
19                 case 3:// /
20                     if (i2 == 0)
21                     {
22                         MessageBox.Show("除数不能为零!");
23                         return;
24                     }
25                     result = i1 / i2;
26                     break;
27                 default://防患于未然
28                 throw new Exception("未知的运算符");//抛异常
29 
30             }
31             txtResult.Text=Convert.ToString(result);
32         }

//TextBox_Test

1 private void button1_Click(object sender, EventArgs e)
2         {
3             textBox4.AppendText(DateTime.Now.ToString()+"\n");
4         }

//Email帐号分解 主机+域名

 1 private void button1_Click(object sender, EventArgs e)
 2         {
 3             string email = textBox1.Text;
 4             string[] strs = email.Split('@');
 5             if (strs.Length != 2)
 6             {
 7                 MessageBox.Show("格式错误");
 8                 return;
 9             }
10             textBox2.Text = strs[0];
11             textBox3.Text = strs[1];
12         }

 

 

posted @ 2013-06-16 14:13  -112  阅读(334)  评论(0编辑  收藏  举报