C# WinForm基础
1. WinForm基础
Form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WinsForm基础
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string name = textBox1.Text;//得到编辑框中的文字
- //this.Text = name + "你好";//设置这个窗体的文字
- this.Text = string.Format("{0}你好", name);
- //当点击文本框时,隐藏文本框
- textBox1.Hide();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- string str2 = textBox2.Text;
- string str3 = textBox3.Text;
- int i1, i2;
- if (!int.TryParse(str2, out i1))
- //out传参前,可以不对参数初始化,out的函数会清空变量,即使变量已经赋值也不行;
- //ref传参前,必须对参数初始化
- {
- MessageBox.Show("第一个数不是合法的整数");
- return;//不要忘了return
- }
- if (!int.TryParse(str3, out i2))
- {
- MessageBox.Show("第二个数不是合法的整数");
- return;
- }
- int i3 = i1 + i2;
- textBox4.Text = Convert.ToString(i3);
- }
- }
- }
2. email分析
Form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace email分析
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string email = textBox1.Text;
- string[] strs = email.Split('@');
- if (strs.Length != 2)
- {
- MessageBox.Show("非法的email地址!");
- return;//不要忘了return
- }
- textBox2.Text = strs[0];
- textBox3.Text = strs[1];
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
3. 滚动1
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 滚动1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string str = textBox1.Text;
- char first = str[0];
- string 剩下 = str.Substring(1);
- textBox1.Text = 剩下 + first;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- string str = textBox1.Text;
- char last = str[4];
- string 剩下 = str.Substring(0,4);
- textBox1.Text = last+剩下;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
4. 累加
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 累加
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string s1 = textBox1.Text;
- string s2 = textBox2.Text;
- int i1, i2;
- if (int.TryParse(s1, out i1) == false)
- {
- MessageBox.Show("数字1格式错误!");
- return;
- }
- if (int.TryParse(s2, out i2) == false)
- {
- MessageBox.Show("数字2格式错误!");
- return;
- }
- if (i1 >= i2)
- {
- MessageBox.Show("第二个数要大于第一个数!");
- return;
- }
- int sum = 0;
- for (int i = i1; i <= i2; i++)
- {
- sum = sum + i;
- }
- textBox3.Text = Convert.ToString(sum);
- }
- }
- }
5. 登录界面1
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 登录界面1
- {
- public partial class Form1 : Form
- {
- private int ErrorTimes = 0;//错误的次数
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //方法一
- textBox4.AppendText(DateTime.Now.ToString()+"\n");
- //方法二
- //textBox4.Text = textBox4.Text+ DateTime.Now.ToString() + "\n" ;
- }
- private void login_Click(object sender, EventArgs e)
- {
- string username = txtUserName.Text.Trim();//Trim忽略大小写
- string password = txtPassWord.Text;
- if (username.Equals("admin", StringComparison.
- OrdinalIgnoreCase) && password == "888888")
- {
- MessageBox.Show("登录成功!");
- }
- else
- {
- ErrorTimes++;//错误次数加1
- if (ErrorTimes > 3)
- {
- MessageBox.Show("错误次数过多,程序即将退出!");
- Application.Exit();
- }
- MessageBox.Show("登录失败!");
- }
- }
- private void btnModify_Click(object sender, EventArgs e)
- {
- string oldpassword = txtUserName.Text;//取旧密码
- string newpassword = txtPassWord.Text;
- string newpassword2 = newPassWord2.Text;
- if (oldpassword != "888888")
- {
- MessageBox.Show("旧密码错误!");
- return;
- }
- if (newpassword != newpassword2)
- {
- MessageBox.Show("两次输入的新密码不一致!");
- return;
- }
- if (newpassword == oldpassword)
- {
- MessageBox.Show("旧密码和新密码不能一样!");
- return;
- }
- MessageBox.Show("修改成功!");
- }
- }
- }
6. 图片显示1
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 图片显示1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string 身份证号 = textBox1.Text;
- //叫验是否是合法的身份证号
- pictureBox1.Visible = true;
- string strYear = 身份证号.Substring(6,4);
- int year = Convert.ToInt32(strYear);
- if ((DateTime.Now.Year - year >= 18)==true)
- {
- pictureBox1.Visible = true;
- return;
- }
- else
- {
- MessageBox.Show("你的年龄小于18,无法查看!");
- //pictureBox1.Visible = false;
- return;
- }
- }
- }
- }
7. 统计成绩
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 统计成绩
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnResult_Click(object sender, EventArgs e)
- {
- //string s = txt成绩.Text;
- //方法1:按照\r\n进行split
- string[] lines=txt成绩.Lines;//方法2
- string maxName="";
- int maxScore = -1;
- foreach (string line in lines)
- {
- string[] strs = line.Split('=');
- string name=strs[0];
- string strScore=strs[1];
- int score = Convert.ToInt32(strScore);
- if (score > maxScore)
- {
- maxName = name;
- maxScore = score;
- }
- }
- MessageBox.Show(string.Format("{0}是第一名,成绩是{1}",
- maxName,maxScore));
- }
- }
- }
8. 下拉列表
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 下拉列表
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void comboBox1_Click(object sender, EventArgs e)
- {
- MessageBox.Show(Convert.ToString
- (comboBox1.SelectedIndex));//第几项
- MessageBox.Show(Convert.ToString
- (comboBox1.SelectedValue));
- MessageBox.Show(Convert.ToString
- (comboBox1.SelectedText));//数据库中将用到
- MessageBox.Show(Convert.ToString
- (comboBox1.SelectedItem));//选中的项的内容
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));
- }
- private void btnResult_Click(object sender, EventArgs e)
- {
- string str1 = txtNumber1.Text;
- string str2 = txtNumber2.Text;
- int i1 = Convert.ToInt32(str1);
- int i2 = Convert.ToInt32(str2);
- int result;
- switch (cb操作符.SelectedIndex)
- {
- case 0://+
- result = i1 + i2;
- break;
- case 1://-
- result = i1 - i2;
- break;
- case 2://*
- result = i1 * i2;
- break;
- case 3:// /
- if (i2 == 0)
- {
- MessageBox.Show("0不能为除数!");
- return;
- }
- result = i1 / i2;
- break;
- default:
- throw new Exception("未知的运算符");
- }
- txtResult.Text = Convert.ToString(result);
- }
- }
- }
9. 省市选择
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 省市选择
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void cb省_SelectedIndexChanged(object sender, EventArgs e)
- {
- cb市.Items.Clear();//清空旧数据
- string 省 = Convert.ToString(cb省.SelectedItem);
- if(省=="山东")
- {
- cb市.Items.Add("潍坊");
- cb市.Items.Add("临沂");
- cb市.Items.Add("青岛");
- }
- if (省 == "河南")
- {
- cb市.Items.Add("郑州");
- cb市.Items.Add("三门峡");
- cb市.Items.Add("洛阳");
- }
- if (省 == "湖南")
- {
- cb市.Items.Add("长沙");
- cb市.Items.Add("衡阳");
- cb市.Items.Add("邵阳");
- }
- }
- }
- }