C# WinForm基础

1. WinForm基础

    

Form1.cs

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace WinsForm基础   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void button1_Click(object sender, EventArgs e)   
  20.         {   
  21.                
  22.             string name = textBox1.Text;//得到编辑框中的文字   
  23.             //this.Text = name + "你好";//设置这个窗体的文字   
  24.             this.Text = string.Format("{0}你好", name);   
  25.                
  26.                
  27.             //当点击文本框时,隐藏文本框    
  28.             textBox1.Hide();   
  29.   
  30.         }   
  31.   
  32.         private void button2_Click(object sender, EventArgs e)   
  33.         {   
  34.             string str2 = textBox2.Text;   
  35.             string str3 = textBox3.Text;   
  36.             int i1, i2;   
  37.             if (!int.TryParse(str2, out i1))   
  38.             //out传参前,可以不对参数初始化,out的函数会清空变量,即使变量已经赋值也不行;   
  39.             //ref传参前,必须对参数初始化   
  40.             {   
  41.                 MessageBox.Show("第一个数不是合法的整数");   
  42.                 return;//不要忘了return   
  43.             }   
  44.             if (!int.TryParse(str3, out i2))   
  45.             {   
  46.                 MessageBox.Show("第二个数不是合法的整数");   
  47.                 return;   
  48.             }   
  49.             int i3 = i1 + i2;   
  50.             textBox4.Text = Convert.ToString(i3);   
  51.         }   
  52.     }   
  53. }

2. email分析

Form1.cs

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace email分析   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void button1_Click(object sender, EventArgs e)   
  20.         {   
  21.             string email = textBox1.Text;   
  22.             string[] strs = email.Split('@');   
  23.             if (strs.Length != 2)   
  24.             {   
  25.                 MessageBox.Show("非法的email地址!");   
  26.                 return;//不要忘了return   
  27.             }   
  28.             textBox2.Text = strs[0];   
  29.             textBox3.Text = strs[1];   
  30.         }   
  31.   
  32.         private void Form1_Load(object sender, EventArgs e)   
  33.         {   
  34.   
  35.         }   
  36.     }   
  37. }   

3. 滚动1

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace 滚动1   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void button1_Click(object sender, EventArgs e)   
  20.         {   
  21.             string str = textBox1.Text;   
  22.             char first = str[0];   
  23.             string 剩下 = str.Substring(1);   
  24.             textBox1.Text = 剩下 + first;   
  25.         }   
  26.   
  27.         private void button2_Click(object sender, EventArgs e)   
  28.         {   
  29.             string str = textBox1.Text;   
  30.             char last = str[4];   
  31.             string 剩下 = str.Substring(0,4);   
  32.             textBox1.Text = last+剩下;   
  33.         }   
  34.   
  35.         private void Form1_Load(object sender, EventArgs e)   
  36.         {   
  37.   
  38.         }   
  39.     }   
  40. }

4. 累加

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace 累加   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void Form1_Load(object sender, EventArgs e)   
  20.         {   
  21.   
  22.         }   
  23.   
  24.         private void button1_Click(object sender, EventArgs e)   
  25.         {   
  26.             string s1 = textBox1.Text;   
  27.             string s2 = textBox2.Text;   
  28.             int i1, i2;   
  29.             if (int.TryParse(s1, out i1) == false)   
  30.             {   
  31.                 MessageBox.Show("数字1格式错误!");   
  32.                 return;   
  33.             }   
  34.             if (int.TryParse(s2, out i2) == false)   
  35.             {   
  36.                 MessageBox.Show("数字2格式错误!");   
  37.                 return;   
  38.             }   
  39.             if (i1 >= i2)   
  40.             {   
  41.                 MessageBox.Show("第二个数要大于第一个数!");   
  42.                 return;   
  43.             }   
  44.             int sum = 0;   
  45.             for (int i = i1; i <= i2; i++)   
  46.             {   
  47.                 sum = sum + i;   
  48.             }   
  49.             textBox3.Text = Convert.ToString(sum);   
  50.         }   
  51.     }   
  52. }   

5. 登录界面1

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace 登录界面1   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         private int ErrorTimes = 0;//错误的次数   
  15.         public Form1()   
  16.         {   
  17.             InitializeComponent();   
  18.         }   
  19.   
  20.         private void button1_Click(object sender, EventArgs e)   
  21.         {   
  22.             //方法一   
  23.             textBox4.AppendText(DateTime.Now.ToString()+"\n");   
  24.             //方法二   
  25.             //textBox4.Text = textBox4.Text+ DateTime.Now.ToString() + "\n" ;   
  26.         }   
  27.   
  28.         private void login_Click(object sender, EventArgs e)   
  29.         {   
  30.             string username = txtUserName.Text.Trim();//Trim忽略大小写   
  31.             string password = txtPassWord.Text;   
  32.             if (username.Equals("admin", StringComparison.   
  33.                 OrdinalIgnoreCase) && password == "888888")   
  34.             {   
  35.                 MessageBox.Show("登录成功!");   
  36.             }   
  37.             else  
  38.             {   
  39.                 ErrorTimes++;//错误次数加1   
  40.                 if (ErrorTimes > 3)   
  41.                 {   
  42.                     MessageBox.Show("错误次数过多,程序即将退出!");   
  43.                     Application.Exit();   
  44.                 }   
  45.                 MessageBox.Show("登录失败!");   
  46.             }   
  47.         }   
  48.   
  49.         private void btnModify_Click(object sender, EventArgs e)   
  50.         {   
  51.             string oldpassword = txtUserName.Text;//取旧密码   
  52.             string newpassword = txtPassWord.Text;   
  53.             string newpassword2 = newPassWord2.Text;   
  54.             if (oldpassword != "888888")   
  55.             {   
  56.                 MessageBox.Show("旧密码错误!");   
  57.                 return;   
  58.             }   
  59.             if (newpassword != newpassword2)   
  60.             {   
  61.                 MessageBox.Show("两次输入的新密码不一致!");   
  62.                 return;   
  63.             }   
  64.             if (newpassword == oldpassword)   
  65.             {   
  66.                 MessageBox.Show("旧密码和新密码不能一样!");   
  67.                 return;   
  68.             }   
  69.             MessageBox.Show("修改成功!");   
  70.   
  71.         }   
  72.     }   
  73. }   

6. 图片显示1

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace 图片显示1   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void button1_Click(object sender, EventArgs e)   
  20.         {   
  21.             string 身份证号 = textBox1.Text;   
  22.             //叫验是否是合法的身份证号   
  23.             pictureBox1.Visible = true;   
  24.             string strYear = 身份证号.Substring(6,4);   
  25.             int year = Convert.ToInt32(strYear);   
  26.             if ((DateTime.Now.Year - year >= 18)==true)   
  27.             {   
  28.                 pictureBox1.Visible = true;   
  29.                 return;   
  30.             }   
  31.             else  
  32.             {   
  33.                 MessageBox.Show("你的年龄小于18,无法查看!");   
  34.                 //pictureBox1.Visible = false;   
  35.                 return;   
  36.             }   
  37.         }   
  38.     }   
  39. }  

7. 统计成绩

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace 统计成绩   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void btnResult_Click(object sender, EventArgs e)   
  20.         {   
  21.             //string s = txt成绩.Text;   
  22.             //方法1:按照\r\n进行split   
  23.             string[] lines=txt成绩.Lines;//方法2   
  24.             string maxName="";   
  25.             int maxScore = -1;   
  26.             foreach (string line in lines)   
  27.             {   
  28.                 string[] strs = line.Split('=');   
  29.                 string name=strs[0];   
  30.                 string strScore=strs[1];   
  31.                 int score = Convert.ToInt32(strScore);   
  32.                 if (score > maxScore)   
  33.                 {   
  34.                     maxName = name;   
  35.                     maxScore = score;   
  36.                 }   
  37.             }   
  38.             MessageBox.Show(string.Format("{0}是第一名,成绩是{1}",   
  39.                 maxName,maxScore));   
  40.         }   
  41.     }   
  42. }   

8. 下拉列表

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace 下拉列表   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.         private void comboBox1_Click(object sender, EventArgs e)   
  19.         {   
  20.             MessageBox.Show(Convert.ToString   
  21.                 (comboBox1.SelectedIndex));//第几项   
  22.             MessageBox.Show(Convert.ToString   
  23.                 (comboBox1.SelectedValue));   
  24.             MessageBox.Show(Convert.ToString   
  25.                 (comboBox1.SelectedText));//数据库中将用到   
  26.             MessageBox.Show(Convert.ToString   
  27.                 (comboBox1.SelectedItem));//选中的项的内容   
  28.         }   
  29.   
  30.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)   
  31.         {   
  32.             MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));   
  33.         }   
  34.   
  35.         private void btnResult_Click(object sender, EventArgs e)   
  36.         {   
  37.             string str1 = txtNumber1.Text;   
  38.             string str2 = txtNumber2.Text;   
  39.             int i1 = Convert.ToInt32(str1);   
  40.             int i2 = Convert.ToInt32(str2);   
  41.             int result;   
  42.   
  43.             switch (cb操作符.SelectedIndex)   
  44.             {   
  45.                 case 0://+   
  46.                     result = i1 + i2;   
  47.                     break;   
  48.                 case 1://-   
  49.                     result = i1 - i2;   
  50.                     break;   
  51.                 case 2://*   
  52.                     result = i1 * i2;   
  53.                     break;   
  54.                 case 3:// /   
  55.                     if (i2 == 0)   
  56.                     {   
  57.                         MessageBox.Show("0不能为除数!");   
  58.                         return;   
  59.                     }   
  60.                     result = i1 / i2;   
  61.                     break;   
  62.                 default:   
  63.                     throw new Exception("未知的运算符");   
  64.             }   
  65.             txtResult.Text = Convert.ToString(result);   
  66.         }   
  67.   
  68.   
  69.     }   
  70. }   

9. 省市选择

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace 省市选择   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void cb省_SelectedIndexChanged(object sender, EventArgs e)   
  20.         {   
  21.             cb市.Items.Clear();//清空旧数据   
  22.             string 省 = Convert.ToString(cb省.SelectedItem);   
  23.             if(省=="山东")   
  24.             {   
  25.                 cb市.Items.Add("潍坊");   
  26.                 cb市.Items.Add("临沂");   
  27.                 cb市.Items.Add("青岛");   
  28.             }   
  29.             if (省 == "河南")   
  30.             {   
  31.                 cb市.Items.Add("郑州");   
  32.                 cb市.Items.Add("三门峡");   
  33.                 cb市.Items.Add("洛阳");   
  34.             }   
  35.             if (省 == "湖南")   
  36.             {   
  37.                 cb市.Items.Add("长沙");   
  38.                 cb市.Items.Add("衡阳");   
  39.                 cb市.Items.Add("邵阳");   
  40.             }   
  41.         }   
  42.     }   
  43. }   
原文地址:https://www.cnblogs.com/luowei010101/p/2074161.html