C#程序设计之windows应用程序设计基础

  • 例题1
  • 例题2
  • 例题3
  • 例题4
  • 例题5
  • 例题6
  • 例题7
  • 例题9

例题1

题目描述
设计一个“简单通讯录”程序,在窗体上建立一个下拉式列表框、两个文本框和两个标签,实现以下功能:当用户在下拉式列表框中选择一个学生姓名后,在“学生姓名”、“地址”两个文本框中分别显示出对应的学生和地址。

代码
窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void lstStudent_SelectedIndexChanged(object sender, EventArgs e){if (this.lstStudent.SelectedItems.Count == 0){return;}else{string strName=this.lstStudent.SelectedItem.ToString();switch (strName){case "张三":this.txtName.Text = "张三";this.txtAddress.Text = "江苏";break;case "李国":this.txtName.Text = "李国";this.txtAddress.Text = "北京";break;case "赵田":this.txtName.Text = "赵田";this.txtAddress.Text = "上海";break;default:break;}}}}
}

运行结果


例题2

题目描述
设计一个Windows应用程序,在窗体上有一个文本框、一个按钮,实现当用户单击按钮时文本框内显示当前是第几次单击该按钮的功能

代码
窗体代码

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Chap9_2
{/// <summary>/// Form1 的摘要说明。/// </summary>public class Form1 : System.Windows.Forms.Form{private System.Windows.Forms.Label lblText;private System.Windows.Forms.Button btnClickMe;private System.Windows.Forms.TextBox txtName;/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.Container components = null;public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();//// TODO: 在 InitializeComponent 调用后添加任何构造函数代码//}/// <summary>/// 清理所有正在使用的资源。/// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){this.lblText = new System.Windows.Forms.Label();this.btnClickMe = new System.Windows.Forms.Button();this.txtName = new System.Windows.Forms.TextBox();this.SuspendLayout();// // lblText// this.lblText.Location = new System.Drawing.Point(107, 103);this.lblText.Name = "lblText";this.lblText.Size = new System.Drawing.Size(138, 41);this.lblText.TabIndex = 0;this.lblText.Text = "请点击下面的按钮";// // btnClickMe// this.btnClickMe.Location = new System.Drawing.Point(107, 185);this.btnClickMe.Name = "btnClickMe";this.btnClickMe.Size = new System.Drawing.Size(128, 31);this.btnClickMe.TabIndex = 1;this.btnClickMe.Text = "点我";this.btnClickMe.Click += new System.EventHandler(this.btnClickMe_Click);// // txtName// this.txtName.Location = new System.Drawing.Point(96, 257);this.txtName.Name = "txtName";this.txtName.Size = new System.Drawing.Size(149, 25);this.txtName.TabIndex = 2;this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtName_Validating);// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);this.ClientSize = new System.Drawing.Size(328, 313);this.Controls.Add(this.txtName);this.Controls.Add(this.btnClickMe);this.Controls.Add(this.lblText);this.Name = "Form1";this.Text = "Form1";this.ResumeLayout(false);this.PerformLayout();}#endregion/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main() {Application.Run(new Form1());}private int count = 0;private void btnClickMe_Click(object sender, System.EventArgs e){count++;lblText.Text = "你一共点击了" + count.ToString() + "次按钮";}private void txtName_Validating(object sender, System.ComponentModel.CancelEventArgs e){if(txtName.Text.Trim() == string.Empty){MessageBox.Show ("用户名为空,请重新输入!");txtName.Focus();}}//private int count = 0;}
}

运行结果

例题3

题目描述
在窗体上创建3个文本框,编写一个程序,当程序运行时,在第一个文本框中输入一行文字,则在另两个文本框中同时显示相同的内容,但显示的字号和字体不同,要求输入的字符数不超过10个。

代码
窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace TextBox
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void textBox1_TextChanged(object sender, EventArgs e){textBox2.Text = textBox1.Text;textBox3.Text = textBox1.Text;            }}
}

运行结果

例题4

题目描述
实现一个简单的计算器程序,此程序的设计界面如图,运行结果如图。

代码
窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}char  chrOP;private void cmbOP_SelectedIndexChanged(object sender, EventArgs e){switch (cmbOP.SelectedIndex){case 0:chrOP = '+' ;break;case 1:chrOP = '-';break;case 2:chrOP = '*' ;break;case 3:chrOP = '/' ;break;}}private void btnCalculator_Click(object sender, EventArgs e){string s1, s2;s1 = txtNum1.Text;if (s1 == ""){MessageBox.Show("请输入数值1");txtNum1.Focus();return;}s2 = txtNum2.Text;if (s2 == ""){MessageBox.Show("请输入数值2");txtNum2.Focus();return;} Single arg1 = Convert.ToSingle(s1);Single arg2 = Convert.ToSingle(s2);Single r;switch (chrOP){case '+':r = arg1 + arg2;break;                    case '-':r = arg1 - arg2;break;case '*':r = arg1 * arg2;break;case '/':if (arg2 == 0){throw new ApplicationException();}else{r = arg1 / arg2;}break;default:throw new ArgumentException();//MessageBox.Show("请选择操作符");//r = 0;//break;}txtResult.Text = r.ToString();}}
}

运行结果

例题5

题目描述
编写一个程序:输人两个数,并可以用命令按钮选择执行加、减、乘、除运算。窗体上创建两个文本框用于输人数值,创建3个标签分别用于显示运算符、等号和运算结果,创建5个命令按钮分别执行加、减、乘、除运算和结束程序的运行,如图9-63所示,要求在文本框中只能输人数字,否则将报错。程序的运行结果如图9-64所示。

代码
窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace Button
{public partial class Form1 : Form{Single result;public Form1(){InitializeComponent();}private void btnAdd_Click(object sender, EventArgs e){label1.Text = "+";result = Convert.ToSingle(textBox1.Text) + Convert.ToSingle(textBox2.Text);textBox3.Text = result.ToString();}//只允许输入0~9的数字private void textBox1_KeyPress(object sender, KeyPressEventArgs e){//在限制用户输入非0~9之间的数字的同时,不应限制用户输入“回车”和“退格”,//否则将给用户带来不便if ((e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) && e.KeyChar != 13){MessageBox.Show("只能输入数字", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);e.Handled = true;//表示已经处理了KeyPress事件}}private void textBox2_KeyPress(object sender, KeyPressEventArgs e){//在限制用户输入非0~9之间的数字的同时,不应限制用户输入“回车”和“退格”,//否则将给用户带来不便if ((e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) && e.KeyChar != 13){MessageBox.Show("只能输入数字", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);e.Handled = true;//表示已经处理了KeyPress事件}}private void btnSub_Click(object sender, EventArgs e){label1.Text = "-";result = Convert.ToSingle(textBox1.Text) - Convert.ToSingle(textBox2.Text);textBox3.Text = result.ToString();}private void btnMul_Click(object sender, EventArgs e){label1.Text = "*";result = Convert.ToSingle(textBox1.Text) * Convert.ToSingle(textBox2.Text);textBox3.Text = result.ToString();}private void btnDiv_Click(object sender, EventArgs e){if (Convert.ToSingle(textBox2.Text) == 0){MessageBox.Show("请输入数字", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);textBox2.Text = ""; textBox2.Focus();textBox3.Text = "";  }else{label1.Text = "/";result = Convert.ToSingle(textBox1.Text) / Convert.ToSingle(textBox2.Text);textBox3.Text = result.ToString(); }}private void btnEnd_Click(object sender, EventArgs e){Application.Exit();}        }
}

运行结果

例题6

题目描述
建立一个简单的购物计划,物品单价已列出,用户只需在购买物品时选择购买的物品,并单击“总计”按钮,即可显示购物的总价格。在本程序中采用了以下设计技巧:
利用窗体初始化来建立初始界面,这样做比利用属性列表操作更加方便。
利用复选框的Text属性显示物品名称,利用Labell~Label4的Text属性显示各物品价格,利用文本框的Text属性显示所购物品价格。
对于复选框,可以利用其Checked属性值或CheckState属性值的改变去处理一些问题,在本例中被选中的物品才计人总价。

代码
窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace CheckBox
{public partial class Form1 : Form{Single sum = 0;public Form1(){InitializeComponent();}private void checkBox4_CheckedChanged(object sender, EventArgs e){sum +=  Convert.ToSingle(lblShampoo.Text);}private void checkBox3_CheckedChanged(object sender, EventArgs e){sum +=  Convert.ToSingle(lblToothpaste.Text);}private void checkBox2_CheckedChanged(object sender, EventArgs e){sum +=  Convert.ToSingle(lblToothbrush.Text);}private void chkSoap_CheckedChanged(object sender, EventArgs e){sum +=  Convert.ToSingle(lblSoap.Text);}private void btnSure_Click(object sender, EventArgs e){txtTotalPrice.Text = sum.ToString();}}
}

运行结果

例题7

题目描述
输入一个字符串,统计其中有多少个单词。单词之间用空格分隔,程序的运行结果如图。

代码
窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void btnCal_Click(object sender, EventArgs e){string strOrgin = this.txtOrgin.Text.Trim();string strTemp;int iCount = 0;int iLastIndex = strOrgin.LastIndexOf(" ");for (int i = 0; i <= iLastIndex; i++){strTemp = strOrgin.Substring(i, 1);if (strTemp == " ")iCount++;}this.txtWordCount.Text = Convert.ToString(iCount);}}
}

运行结果

例题9

题目描述
设定一个有大小写字母的字符串,先将字符串的大写字母输出,再将字符串的小写字母输出,程序的运行结果如图

代码
窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void btnCal_Click(object sender, EventArgs e){string strOrgin = this.txtOrgin.Text.Trim();string strUpString = "", strLowString = "";int iCount = strOrgin.Length;char[] charUp ={ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };char[] charLow ={ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };for (int i = 0; i < iCount; i++){string strTemp = strOrgin.Substring(i, 1);if (strTemp.IndexOfAny(charUp) >= 0)strUpString += strTemp;if (strTemp.IndexOfAny(charLow) >= 0)strLowString += strTemp;}this.txtUpString.Text = strUpString;this.txtLowString.Text = strLowString;}}
}

运行结果

C#程序设计之windows应用程序设计基础相关推荐

  1. c语言基础程序设计报告,c语言程序设计基础课程设计报告.doc

    c语言程序设计基础课程设计报告.doc 还剩 16页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,很抱歉,此页已超出免费预览范围啦! 如果喜欢就下载吧,价低环保! 内容要点: C 语言程序 ...

  2. c程序设计语言 qsort,【程序设计基础_C语言】北理工的恶龙(附qsort范例)

    [程序设计基础_C语言]北理工的恶龙(附qsort实例) 北理工的恶龙(附qsort实例) 背景:最近,北理工出现了一只恶龙,它长着很多 头,而且还会吐火,它将会把北理工烧成废墟, 于是,校长下令召集 ...

  3. Windows应用程序设计基础(常用控件2)

    列表框控件 列表框(ListBox)控件提供一个项目列表,,用户可从中选择一项或多项.(若超过可显示的项目数会自动添加滚动条) 列表框内的项目为列表项,列表项的加入是按照一定顺序进行的,这个顺序号称为 ...

  4. 【程序设计】C语言程序设计基础教材题解

    文章目录 第一章 计算机及程序设计概述 第二章 信息编码与数据类型 第三章 基本运算与顺序结构 第四章 逻辑判断与选择结构 第五章 迭代计算与循环结构 第六章 集合数据与数组 第七章 模块化与函数 第 ...

  5. java程序设计 郑莉_Java程序设计基础-清华大学-计算机科学与技术-郑莉教授-学堂在线-第一章...

    旁听课程记录 一.Java语言基础知识 1.机器语言  汇编语言   高级语言  { 面向过程的高级语言(C).面向对象的高级语言(Java_1995)} 2.面向对象语言的基本特征: 抽象和封装.继 ...

  6. 程序设计基础c语言版习题答案,C语言程序设计基础知识 习题一及参考答案

    C语言实用复习题目及答案.帮你更好地学习C程序设计~ 第一章:程序设计基础知识 一. 单项选择题 1.以下( )是面向过程的程序设计语言. A)机器语言 B)汇编语言 C)高级语言 D)第四代语言 2 ...

  7. 《Windows网络与通信程序设计(第3版)》——1.4 网络应用程序设计基础

    本节书摘来自异步社区<Windows网络与通信程序设计(第3版)>一书中的第1章,第1.4节,作者: 陈香凝 , 王烨阳 , 陈婷婷 , 张铮 更多章节内容可以访问云栖社区"异步 ...

  8. python语言程序设计2019版第二章课后答案-python语言程序设计基础课后答案第二章...

    python语言程序设计基础课后答案第二章 以下合法的用户自定义标识符是____________. 导入模块或者模块中的元素要使用关键字________ . 下列哪个函数是用来控制画笔的尺寸的____ ...

  9. c++程序设计(第三版) pdf_【好课传送】C++语言程序设计基础入门视频

    [机器学习之美导读]C/C++语言发展至今已有40多年的历史,在全世界应用非常广泛,是主流的开发语言. C/C++体系语言是IT工程师长远发展的首选,具备C++背景的工程师被互联网IT后端团队认定为团 ...

最新文章

  1. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的连接恢复和命令拦截...
  2. JavaWeb:过滤器Filter
  3. CSS综合征病例,医药-churg-strauss 综合征 (css) 变应性嗜酸性肉芽肿.ppt
  4. docker-compose常用命令整理及使用示例
  5. python窗口化编程_python程序的窗口化
  6. C语言实现封装、继承、多态
  7. ios-UIButton-常用方法
  8. 数电渣渣的一点学习感想(更新中)
  9. BZOJ4557 JLOI2016侦察守卫(树形dp)
  10. android网络编程登录和验证,ASP.NET实现用户注册和验证功能(第4节)
  11. Atitit 2017年第68界机器视觉图像处理学术大会会议记要attilax总结自建学院自颁学位理论
  12. LabVIEW查找范例的使用举例
  13. 国家统计局:政府统计应用大数据的主要障碍
  14. 从来也科技首次入选Gartner RPA魔力象限报告,看国产RPA未来发展
  15. 地图定位偏移以及坐标系转换(一)-国内部分常见的地理坐标系
  16. 论文学习 Feature Generating Networks for Zero-Shot Learning
  17. 2021-9-20 18点00 程序外生活 - 中国A50指数 机器预测学习跟踪记录 - 回调有点大,但是还是再箱体里面,且没有破前低,恒大黑天鹅继续发酵,等待箱体稳定。
  18. php 框架效率测试,关于DoitPHP,ThinkPHP,Yii,CI,DooPHP等框架的性能对比测试
  19. 深沪货币基金逻辑简单总结
  20. Python怎样存储变量性能最优?这篇文章告诉你答案

热门文章

  1. tex中对页眉的编辑
  2. mysql之第n高的薪水
  3. 【100%通过率】华为OD机试真题 JS 实现【最接近最大输出功率的设备 /查找充电设备组合】【2023 Q1 | 200分】
  4. 【深度优先搜索算法】与【宽度优先搜索算法】
  5. 微信小程序——video视频全屏展示
  6. C语言图书管理系统[2023-01-06]
  7. 深度学习与多层神经网络的区别
  8. 使用DevExpress的PdfViewer控件加载http传输文件
  9. 如何一招永久删除hao123流氓网页挟持
  10. MacBook 安装 win10 系统的方法