WinForm基础:综合练习
来源:互联网 发布:c 排序算法时间复杂度 编辑:程序博客网 时间:2024/05/05 06:40
WinForm:Windows Form是.net中用来开发Windows窗口程序的技术。
控件:窗体上很多元素都是相似的,因此将这些元素抽象为一些类,这些类就叫做控件。
1、控件要有一个名字,对控件进行操作的时候通过名字来引用。
2、控件属性可以在属性视图进行修改。
练习1:
简单的加法器,用户在文本框1、2中输入两个整数,点击按钮,在文本框3中显示这两个数的和,如果1或2为错误的数据格式,则弹出对话框提示错误。
创建Windows窗体应用程序,直接拖控件设计视图,视图如图所示:
代码:
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 WinForm{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click_1(object sender, EventArgs e) { string str1 = textBox1.Text; string str2 = textBox2.Text; int i1, i2; if (!int.TryParse(str1, out i1)) { MessageBox.Show("第一个数不是合法整数"); return;//不要忘了return } if (int.TryParse(str2, out i2) == false) { MessageBox.Show("第二个数不是合法整数"); return; } int i3 = i1 + i2; textBox3.Text = Convert.ToString(i3); } }}
注意当检测到输入值不合法时需要return,否则程序会将不合法值当做0继续计算,如图:
加入return之后跳出函数,不进行后续计算。
输入正确数值:
注意int.TryParse用法:
int.Parse()是一种类容转换;表示将数字内容的字符串转为int类型。如果字符串为空,则抛出ArgumentNullException异常;如果字符串内容不是数字,则抛出FormatException异常;如果字符串内容所表示数字超出int类型可表示的范围,则抛出OverflowException异常;int.TryParse 与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。最后一个参数为输出值,如果转换失败,输出值为 0
练习2:
输入Email地址,输出用户名和域名。
视图:
代码:
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; } textBox2.Text = strs[0]; textBox3.Text = strs[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 累加{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } 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)) { MessageBox.Show("第一个数为非法值"); return; } if (!int.TryParse(s2, out i2)||(i1>=i2)) { MessageBox.Show("第二个数为非法值"); return; } int i,sum=0; for (i = i1; i <= i2; i++) { sum = sum + i; } textBox3.Text = Convert.ToString(sum); } }}
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 button1_Click(object sender, EventArgs e) { string num = textBox1.Text; string year = num.Substring(6, 4); int byear = Convert.ToInt32(year); int now = DateTime.Now.Year; if ((now - byear) >= 18) { pictureBox1.Visible = true; } else MessageBox.Show("没有查看权限"); } }}
练习5:页面上有一个文本框,文本框的左侧和右侧各有一个按钮,点击左侧按钮文本框中的文字向左循环滚动一次,点击右侧按钮文本框中的文字向右循环滚动一次。
视图:
代码:
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 button1_Click(object sender, EventArgs e) { string str = textBox1.Text; char first = str[0]; string s = str.Substring(1); textBox1.Text = s + first; } private void button2_Click(object sender, EventArgs e) { string str = textBox1.Text; char last = str[str.Length-1]; string s = str.Substring(0,5); textBox1.Text = last+s; } }}
执行结果:
向左循环:
向右循环:
- WinForm基础:综合练习
- java基础综合练习
- WinForm基础:文本框练习
- C#基础-023 综合练习
- JAVASE基础 Item -- IO流综合练习
- 综合练习
- 综合练习
- 综合练习:打字练习
- 综合练习:打字练习
- 黑马程序员-Java基础综合练习:简易聊天服务器
- 黑马程序员-Java基础综合练习:简易聊天客户端
- 黑马程序员_c#基础综合练习:骑士飞行棋
- u3d代码脚本基础---判断循环综合练习
- C#基础综合练习--超市收银系统的设计
- JavaScript语言基础7---函数的综合练习
- 综合基础
- android基础篇------------java基础(9)(集合的综合练习)
- C语言综合练习
- Android环境变量
- 确定对象的五个方法
- Fixed HibernnateOptimisticLockingExceptions for startproject and team…
- Oracel SQL 记录
- PHP字符串连接
- WinForm基础:综合练习
- linux-2.6.26内核中ARM中断实现详解(3)
- Adobe(Photoshop And Dreamweaver)Cs5.5与Cs5破解
- 发Zend Studio 9.0.2正式版注册破解
- ARM中断异常处理的返回
- 【android基础】之Android获取网络上的图片结合ImageView的简单应用
- Nokia是否还有未来 - 小议诺基亚和微软的战略布局
- PC上十款经典的3D次世代游戏
- 网络测速