winform,C# WinForm基础

1. WinForm基础
C# WinForm基础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. namespace WinsForm基础
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. string name = textBox1.Text;//得到编辑框中的文字
  20. //this.Text = name + "你好";//设置这个窗体的文字
  21. this.Text = string.Format("{0}你好", name);
  22. //当点击文本框时,隐藏文本框
  23. textBox1.Hide();
  24. }
  25. private void button2_Click(object sender, EventArgs e)
  26. {
  27. string str2 = textBox2.Text;
  28. string str3 = textBox3.Text;
  29. int i1, i2;
  30. if (!int.TryParse(str2, out i1))
  31. //out传参前,可以不对参数初始化,out的函数会清空变量,即使变量已经赋值也不行;
  32. //ref传参前,必须对参数初始化
  33. {
  34. MessageBox.Show("第一个数不是合法的整数");
  35. return;//不要忘了return
  36. }
  37. if (!int.TryParse(str3, out i2))
  38. {
  39. MessageBox.Show("第二个数不是合法的整数");
  40. return;
  41. }
  42. int i3 = i1 + i2;
  43. textBox4.Text = Convert.ToString(i3);
  44. }
  45. }
  46. }
2. email分析
C# WinForm基础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. namespace email分析
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. string email = textBox1.Text;
  20. string[] strs = email.Split('@');
  21. if (strs.Length != 2)
  22. {
  23. MessageBox.Show("非法的email地址!");
  24. return;//不要忘了return
  25. }
  26. textBox2.Text = strs[0];
  27. textBox3.Text = strs[1];
  28. }
  29. private void Form1_Load(object sender, EventArgs e)
  30. {
  31. }
  32. }
  33. }

3. 滚动1
C# WinForm基础winform
  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. namespace 滚动1
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. string str = textBox1.Text;
  20. char first = str[0];
  21. string 剩下 = str.Substring(1);
  22. textBox1.Text = 剩下 + first;
  23. }
  24. private void button2_Click(object sender, EventArgs e)
  25. {
  26. string str = textBox1.Text;
  27. char last = str[4];
  28. string 剩下 = str.Substring(0,4);
  29. textBox1.Text = last+剩下;
  30. }
  31. private void Form1_Load(object sender, EventArgs e)
  32. {
  33. }
  34. }
  35. }
4. 累加
C# WinForm基础winform
  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. namespace 累加
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void Form1_Load(object sender, EventArgs e)
  18. {
  19. }
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. string s1 = textBox1.Text;
  23. string s2 = textBox2.Text;
  24. int i1, i2;
  25. if (int.TryParse(s1, out i1) == false)
  26. {
  27. MessageBox.Show("数字1格式错误!");
  28. return;
  29. }
  30. if (int.TryParse(s2, out i2) == false)
  31. {
  32. MessageBox.Show("数字2格式错误!");
  33. return;
  34. }
  35. if (i1 >= i2)
  36. {
  37. MessageBox.Show("第二个数要大于第一个数!");
  38. return;
  39. }
  40. int sum = 0;
  41. for (int i = i1; i <= i2; i++)
  42. {
  43. sum = sum + i;
  44. }
  45. textBox3.Text = Convert.ToString(sum);
  46. }
  47. }
  48. }
5. 登录界面1
C# WinForm基础winform
  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. namespace 登录界面1
  10. {
  11. public partial class Form1 : Form
  12. {
  13. private int ErrorTimes = 0;//错误的次数
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. //方法一
  21. textBox4.AppendText(DateTime.Now.ToString()+"\n");
  22. //方法二
  23. //textBox4.Text = textBox4.Text+ DateTime.Now.ToString() + "\n" ;
  24. }
  25. private void login_Click(object sender, EventArgs e)
  26. {
  27. string username = txtUserName.Text.Trim();//Trim忽略大小写
  28. string password = txtPassWord.Text;
  29. if (username.Equals("admin", StringComparison.
  30. OrdinalIgnoreCase) && password == "888888")
  31. {
  32. MessageBox.Show("登录成功!");
  33. }
  34. else
  35. {
  36. ErrorTimes++;//错误次数加1
  37. if (ErrorTimes > 3)
  38. {
  39. MessageBox.Show("错误次数过多,程序即将退出!");
  40. Application.Exit();
  41. }
  42. MessageBox.Show("登录失败!");
  43. }
  44. }
  45. private void btnModify_Click(object sender, EventArgs e)
  46. {
  47. string oldpassword = txtUserName.Text;//取旧密码
  48. string newpassword = txtPassWord.Text;
  49. string newpassword2 = newPassWord2.Text;
  50. if (oldpassword != "888888")
  51. {
  52. MessageBox.Show("旧密码错误!");
  53. return;
  54. }
  55. if (newpassword != newpassword2)
  56. {
  57. MessageBox.Show("两次输入的新密码不一致!");
  58. return;
  59. }
  60. if (newpassword == oldpassword)
  61. {
  62. MessageBox.Show("旧密码和新密码不能一样!");
  63. return;
  64. }
  65. MessageBox.Show("修改成功!");
  66. }
  67. }
  68. }
6. 图片显示1
C# WinForm基础winform
  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. namespace 图片显示1
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. string 身份证号 = textBox1.Text;
  20. //叫验是否是合法的身份证号
  21. pictureBox1.Visible = true;
  22. string strYear = 身份证号.Substring(6,4);
  23. int year = Convert.ToInt32(strYear);
  24. if ((DateTime.Now.Year - year >= 18)==true)
  25. {
  26. pictureBox1.Visible = true;
  27. return;
  28. }
  29. else
  30. {
  31. MessageBox.Show("你的年龄小于18,无法查看!");
  32. //pictureBox1.Visible = false;
  33. return;
  34. }
  35. }
  36. }
  37. }
7. 统计成绩
C# WinForm基础winform
  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. namespace 统计成绩
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void btnResult_Click(object sender, EventArgs e)
  18. {
  19. //string s = txt成绩.Text;
  20. //方法1:按照\r\n进行split
  21. string[] lines=txt成绩.Lines;//方法2
  22. string maxName="";
  23. int maxScore = -1;
  24. foreach (string line in lines)
  25. {
  26. string[] strs = line.Split('=');
  27. string name=strs[0];
  28. string strScore=strs[1];
  29. int score = Convert.ToInt32(strScore);
  30. if (score > maxScore)
  31. {
  32. maxName = name;
  33. maxScore = score;
  34. }
  35. }
  36. MessageBox.Show(string.Format("{0}是第一名,成绩是{1}",
  37. maxName,maxScore));
  38. }
  39. }
  40. }
8. 下拉列表
C# WinForm基础winform
  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. namespace 下拉列表
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void comboBox1_Click(object sender, EventArgs e)
  18. {
  19. MessageBox.Show(Convert.ToString
  20. (comboBox1.SelectedIndex));//第几项
  21. MessageBox.Show(Convert.ToString
  22. (comboBox1.SelectedValue));
  23. MessageBox.Show(Convert.ToString
  24. (comboBox1.SelectedText));//数据库中将用到
  25. MessageBox.Show(Convert.ToString
  26. (comboBox1.SelectedItem));//选中的项的内容
  27. }
  28. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  29. {
  30. MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));
  31. }
  32. private void btnResult_Click(object sender, EventArgs e)
  33. {
  34. string str1 = txtNumber1.Text;
  35. string str2 = txtNumber2.Text;
  36. int i1 = Convert.ToInt32(str1);
  37. int i2 = Convert.ToInt32(str2);
  38. int result;
  39. switch (cb操作符.SelectedIndex)
  40. {
  41. case 0://+
  42. result = i1 + i2;
  43. break;
  44. case 1://-
  45. result = i1 - i2;
  46. break;
  47. case 2://*
  48. result = i1 * i2;
  49. break;
  50. case 3:// /
  51. if (i2 == 0)
  52. {
  53. MessageBox.Show("0不能为除数!");
  54. return;
  55. }
  56. result = i1 / i2;
  57. break;
  58. default:
  59. throw new Exception("未知的运算符");
  60. }
  61. txtResult.Text = Convert.ToString(result);
  62. }
  63. }
  64. }
9. 省市选择
C# WinForm基础winform
  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. namespace 省市选择
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void cb省_SelectedIndexChanged(object sender, EventArgs e)
  18. {
  19. cb市.Items.Clear();//清空旧数据
  20. string 省 = Convert.ToString(cb省.SelectedItem);
  21. if(省=="山东")
  22. {
  23. cb市.Items.Add("潍坊");
  24. cb市.Items.Add("临沂");
  25. cb市.Items.Add("青岛");
  26. }
  27. if (省 == "河南")
  28. {
  29. cb市.Items.Add("郑州");
  30. cb市.Items.Add("三门峡");
  31. cb市.Items.Add("洛阳");
  32. }
  33. if (省 == "湖南")
  34. {
  35. cb市.Items.Add("长沙");
  36. cb市.Items.Add("衡阳");
  37. cb市.Items.Add("邵阳");
  38. }
  39. }
  40. }
  41. }
Tags:  winform

延伸阅读

最新评论

发表评论