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. 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分析

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

  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. 累加

  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

  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

  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. 统计成绩

  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. 下拉列表

  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. 省市选择

  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. }

转载于:https://www.cnblogs.com/luowei010101/archive/2011/06/07/2074161.html

C# WinForm基础相关推荐

  1. C# WinForm GUI之WinForm基础

    C# WinForm GUI之WinForm基础 创建WinForm 窗体的基本操作 控件的常用属性.方法和事件 Windows窗体(Windows Form). WinForm,可视界面,显示信息, ...

  2. Winform基础入门(一)

    l      概念和基础 l      常用的控件及其作用 l      向文本框添加时间和控件ComboBox l      MessageBox类和窗体的一些属性事件 l      Tab键聚焦设 ...

  3. winform基础窗体设置及基础控件

    WinForm - 也叫做C/S  客户端 另:B/S是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...

  4. winform 基础

    listview: 1.修改视图 2.设置列头 3.编写行数据 记住的属性: 行为: Items - listview中的项 HideSelection - 失去焦点,依然保证选中状态 HoverSe ...

  5. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号MessageBox.Sh ...

  6. C#WinForm 分屏教程合集

    C# WindowsForm程序同时启动多个窗口类 - CSDN博客 C#之WinForm基础 运行多个窗体时,点击按钮,一次关闭所有窗体 - CSDN博客 Winform窗口实现多显示屏显示的2种方 ...

  7. 《传智播客.Net培训.net视频教程》(.net视频asp.net培训传智播客asp.net视频教程开放课程c#视频移动开发winform SQL ADO.Net HTML JavaScript

    本资源重要通知 2011年4月传智播客.Net培训-免费公开课现场视频 [重磅内容]微软移动开发介绍1-早起的鸟儿有食吃.rar 详情 53.2MB [重磅内容]微软移动开发介绍2-windows.p ...

  8. Winform下的地图开发控件(GMap.NET)使用心得

    原文地址为: Winform下的地图开发控件(GMap.NET)使用心得 我们先看看GMap.NET的定义: GMap.NET是一个强大.免费.跨平台.开源的.NET控件,它在Windows Form ...

  9. gmap.net基础详细介绍

    GMap.NET是一个强大.免费.跨平台.开源的.NET控件,它在Windows Forms 和WPF环境中能够通过Google, Yahoo!, Bing, OpenStreetMap, ArcGI ...

最新文章

  1. svn 面板缺少可选项_TortoiseSVN的设置
  2. 在MRC模式下使用SDWebImage
  3. 洛谷 P1207 [USACO1.2]双重回文数 Dual Palindromes
  4. 从零开始的AI·吃透kNN算法,学完我悟了(附实例代码)
  5. html让元素纵向排列,html – 如何使元素排列到父元素的外边缘
  6. node mysql 多个_使用Node.js处理多个MySQL查询
  7. FeHelper的安装与使用
  8. Are We Ready for SDN? Implementation Challenges for Software-Defined Networks
  9. javascript箭头函数和this的指向问题
  10. mysql视图子查询_mysql创建视图不能包涵子查询的解决办法。View's SELECT contains a subquery in the FROM clause...
  11. spring-boot 自定义启动图标彩蛋
  12. nacos配置ap_Nacos 1.0.0 功能预览
  13. Django缓存和内置信号
  14. php整形数组求里面的质数,PHP实现的分解质因数操作示例
  15. 在cmd下安装Scrapy怎么解决方案python3
  16. 药一点进销存管理软件_gsp管理规范_财务管理软件
  17. 通过IP查询地址之纯真IP数据库
  18. 串口总线舵机之舵机命令
  19. 百度有啊转型生活平台 启用新标与框计算对接
  20. Cocos2d-X Box2D内容讲解

热门文章

  1. WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)
  2. 如何查看oracle 查版本号
  3. 前端工程师的mysql笔记
  4. 久坐 缺乏运动 消化能力 会减弱
  5. wingide 显示中文 及 配色方案
  6. 杂志类(CMS)主题使用攻略
  7. Lync Server 2010标准版系列PART4:部署准备
  8. 用C#开发网络防火墙技术分析
  9. WPF 设置类库项目为启动项,设置窗体跟随。
  10. re Python正则