C#程序设计之windows应用程序设计基础_设计一个windows窗体应用程序。窗体上放置一个按钮,单击该按钮,在屏幕上画一个圆,-程序员宅基地

技术标签: c#  C#程序设计  windows  开发语言  

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

例题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;
        }
    }
}

运行结果
在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_46669450/article/details/125476106

智能推荐

【史上最易懂】马尔科夫链-蒙特卡洛方法:基于马尔科夫链的采样方法,从概率分布中随机抽取样本,从而得到分布的近似_马尔科夫链期望怎么求-程序员宅基地

文章浏览阅读1.3k次,点赞40次,收藏19次。虽然你不能直接计算每个房间的人数,但通过马尔科夫链的蒙特卡洛方法,你可以从任意状态(房间)开始采样,并最终收敛到目标分布(人数分布)。然后,根据一个规则(假设转移概率是基于房间的人数,人数较多的房间具有较高的转移概率),你随机选择一个相邻的房间作为下一个状态。比如在巨大城堡,里面有很多房间,找到每个房间里的人数分布情况(每个房间被访问的次数),但是你不能一次进入所有的房间并计数。但是,当你重复这个过程很多次时,你会发现你更有可能停留在人数更多的房间,而在人数较少的房间停留的次数较少。_马尔科夫链期望怎么求

linux以root登陆命令,su命令和sudo命令,以及限制root用户登录-程序员宅基地

文章浏览阅读3.9k次。一、su命令su命令用于切换当前用户身份到其他用户身份,变更时须输入所要变更的用户帐号与密码。命令su的格式为:su [-] username1、后面可以跟 ‘-‘ 也可以不跟,普通用户su不加username时就是切换到root用户,当然root用户同样可以su到普通用户。 ‘-‘ 这个字符的作用是,加上后会初始化当前用户的各种环境变量。下面看下加‘-’和不加‘-’的区别:root用户切换到普通..._限制su root登陆

精通VC与Matlab联合编程(六)_精通vc和matlab联合编程 六-程序员宅基地

文章浏览阅读1.2k次。精通VC与Matlab联合编程(六)作者:邓科下载源代码浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程  Matlab C/C++函数库是Matlab扩展功能重要的组成部分,包含了大量的用C/C++语言重新编写的Matlab函数,主要包括初等数学函数、线形代数函数、矩阵操作函数、数值计算函数_精通vc和matlab联合编程 六

Asp.Net MVC2中扩展ModelMetadata的DescriptionAttribute。-程序员宅基地

文章浏览阅读128次。在MVC2中默认并没有实现DescriptionAttribute(虽然可以找到这个属性,通过阅读MVC源码,发现并没有实现方法),这很不方便,特别是我们使用EditorForModel的时候,我们需要对字段进行简要的介绍,下面来扩展这个属性。新建类 DescriptionMetadataProvider然后重写DataAnnotationsModelMetadataPro..._asp.net mvc 模型description

领域模型架构 eShopOnWeb项目分析 上-程序员宅基地

文章浏览阅读1.3k次。一.概述  本篇继续探讨web应用架构,讲基于DDD风格下最初的领域模型架构,不同于DDD风格下CQRS架构,二者架构主要区别是领域层的变化。 架构的演变是从领域模型到C..._eshoponweb

Springboot中使用kafka_springboot kafka-程序员宅基地

文章浏览阅读2.6w次,点赞23次,收藏85次。首先说明,本人之前没用过zookeeper、kafka等,尚硅谷十几个小时的教程实在没有耐心看,现在我也不知道分区、副本之类的概念。用kafka只是听说他比RabbitMQ快,我也是昨天晚上刚使用,下文中若有讲错的地方或者我的理解与它的本质有偏差的地方请包涵。此文背景的环境是windows,linux流程也差不多。 官网下载kafka,选择Binary downloads Apache Kafka 解压在D盘下或者什么地方,注意不要放在桌面等绝对路径太长的地方 打开conf_springboot kafka

随便推点

VS2008+水晶报表 发布后可能无法打印的解决办法_水晶报表 不能打印-程序员宅基地

文章浏览阅读1k次。编好水晶报表代码,用的是ActiveX模式,在本机运行,第一次运行提示安装ActiveX控件,安装后,一切正常,能正常打印,但发布到网站那边运行,可能是一闪而过,连提示安装ActiveX控件也没有,甚至相关的功能图标都不能正常显示,再点"打印图标"也是没反应解决方法是: 1.先下载"PrintControl.cab" http://support.businessobjects.c_水晶报表 不能打印

一. UC/OS-Ⅱ简介_ucos-程序员宅基地

文章浏览阅读1.3k次。绝大部分UC/OS-II的源码是用移植性很强的ANSI C写的。也就是说某产品可以只使用很少几个UC/OS-II调用,而另一个产品则使用了几乎所有UC/OS-II的功能,这样可以减少产品中的UC/OS-II所需的存储器空间(RAM和ROM)。UC/OS-II是为嵌入式应用而设计的,这就意味着,只要用户有固化手段(C编译、连接、下载和固化), UC/OS-II可以嵌入到用户的产品中成为产品的一部分。1998年uC/OS-II,目前的版本uC/OS -II V2.61,2.72。1.UC/OS-Ⅱ简介。_ucos

python自动化运维要学什么,python自动化运维项目_运维学python该学些什么-程序员宅基地

文章浏览阅读614次,点赞22次,收藏11次。大家好,本文将围绕python自动化运维需要掌握的技能展开说明,python自动化运维从入门到精通是一个很多人都想弄明白的事情,想搞清楚python自动化运维快速入门 pdf需要先了解以下几个事情。这篇文章主要介绍了一个有趣的事情,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。_运维学python该学些什么

解决IISASP调用XmlHTTP出现msxml3.dll (0x80070005) 拒绝访问的错误-程序员宅基地

文章浏览阅读524次。2019独角兽企业重金招聘Python工程师标准>>> ..._hotfix for msxml 4.0 service pack 2 - kb832414

python和易语言的脚本哪门更实用?_易语言还是python适合辅助-程序员宅基地

文章浏览阅读546次。python和易语言的脚本哪门更实用?_易语言还是python适合辅助

redis watch使用场景_详解redis中的锁以及使用场景-程序员宅基地

文章浏览阅读134次。详解redis中的锁以及使用场景,指令,事务,分布式,命令,时间详解redis中的锁以及使用场景易采站长站,站长之家为您整理了详解redis中的锁以及使用场景的相关内容。分布式锁什么是分布式锁?分布式锁是控制分布式系统之间同步访问共享资源的一种方式。为什么要使用分布式锁?​ 为了保证共享资源的数据一致性。什么场景下使用分布式锁?​ 数据重要且要保证一致性如何实现分布式锁?主要介绍使用redis来实..._redis setnx watch

推荐文章

热门文章

相关标签