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("邵阳");
- }
- }
- }
- }
转载于:https://www.cnblogs.com/luowei010101/archive/2011/06/07/2074161.html
C# WinForm基础相关推荐
- C# WinForm GUI之WinForm基础
C# WinForm GUI之WinForm基础 创建WinForm 窗体的基本操作 控件的常用属性.方法和事件 Windows窗体(Windows Form). WinForm,可视界面,显示信息, ...
- Winform基础入门(一)
l 概念和基础 l 常用的控件及其作用 l 向文本框添加时间和控件ComboBox l MessageBox类和窗体的一些属性事件 l Tab键聚焦设 ...
- winform基础窗体设置及基础控件
WinForm - 也叫做C/S 客户端 另:B/S是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...
- winform 基础
listview: 1.修改视图 2.设置列头 3.编写行数据 记住的属性: 行为: Items - listview中的项 HideSelection - 失去焦点,依然保证选中状态 HoverSe ...
- 1.C#WinForm基础制作简单计算器
利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号MessageBox.Sh ...
- C#WinForm 分屏教程合集
C# WindowsForm程序同时启动多个窗口类 - CSDN博客 C#之WinForm基础 运行多个窗体时,点击按钮,一次关闭所有窗体 - CSDN博客 Winform窗口实现多显示屏显示的2种方 ...
- 《传智播客.Net培训.net视频教程》(.net视频asp.net培训传智播客asp.net视频教程开放课程c#视频移动开发winform SQL ADO.Net HTML JavaScript
本资源重要通知 2011年4月传智播客.Net培训-免费公开课现场视频 [重磅内容]微软移动开发介绍1-早起的鸟儿有食吃.rar 详情 53.2MB [重磅内容]微软移动开发介绍2-windows.p ...
- Winform下的地图开发控件(GMap.NET)使用心得
原文地址为: Winform下的地图开发控件(GMap.NET)使用心得 我们先看看GMap.NET的定义: GMap.NET是一个强大.免费.跨平台.开源的.NET控件,它在Windows Form ...
- gmap.net基础详细介绍
GMap.NET是一个强大.免费.跨平台.开源的.NET控件,它在Windows Forms 和WPF环境中能够通过Google, Yahoo!, Bing, OpenStreetMap, ArcGI ...
最新文章
- svn 面板缺少可选项_TortoiseSVN的设置
- 在MRC模式下使用SDWebImage
- 洛谷 P1207 [USACO1.2]双重回文数 Dual Palindromes
- 从零开始的AI·吃透kNN算法,学完我悟了(附实例代码)
- html让元素纵向排列,html – 如何使元素排列到父元素的外边缘
- node mysql 多个_使用Node.js处理多个MySQL查询
- FeHelper的安装与使用
- Are We Ready for SDN? Implementation Challenges for Software-Defined Networks
- javascript箭头函数和this的指向问题
- mysql视图子查询_mysql创建视图不能包涵子查询的解决办法。View's SELECT contains a subquery in the FROM clause...
- spring-boot 自定义启动图标彩蛋
- nacos配置ap_Nacos 1.0.0 功能预览
- Django缓存和内置信号
- php整形数组求里面的质数,PHP实现的分解质因数操作示例
- 在cmd下安装Scrapy怎么解决方案python3
- 药一点进销存管理软件_gsp管理规范_财务管理软件
- 通过IP查询地址之纯真IP数据库
- 串口总线舵机之舵机命令
- 百度有啊转型生活平台 启用新标与框计算对接
- Cocos2d-X Box2D内容讲解