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

        
    }
}

运行结果
C#程序设计之windows应用程序设计基础
C#程序设计之windows应用程序设计基础
C#程序设计之windows应用程序设计基础

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

运行结果
C#程序设计之windows应用程序设计基础
C#程序设计之windows应用程序设计基础

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

运行结果
C#程序设计之windows应用程序设计基础

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

运行结果
C#程序设计之windows应用程序设计基础

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

运行结果
C#程序设计之windows应用程序设计基础
C#程序设计之windows应用程序设计基础

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

运行结果
C#程序设计之windows应用程序设计基础
C#程序设计之windows应用程序设计基础

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

运行结果
C#程序设计之windows应用程序设计基础

例题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应用程序设计基础文章来源地址https://www.toymoban.com/news/detail-456497.html

到了这里,关于C#程序设计之windows应用程序设计基础的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【QT】MDI应用程序设计

    目录 1 MDI简介 2 文档窗口类QFormDoc的设计 3 MDI主窗口设计与子窗口的使用 3.1 主窗口界面设计 3.2 MDI子窗口的创建与加入 3.3 QMdiArea常用功能函数 3.4 MDI的信号         传统的应用程序设计中有多文档界面(Multi-documentInterface,MDI)应用程序,Qt为设计 MDI应用程序提供了支持。

    2024年01月25日
    浏览(15)
  • 5、MATLAB程序设计与应用刘卫国(第三版)课后实验五:循环结构程序设计

    目录 一、  二、  三、  四、  五、 已知 求 y的近似值。当n分别取100、1 000、10 000时,结果是多少? 要求 :分别用循环结构和向量运算(使用sum 函数)来实现。 --------------------------------------- 示例代码 --------------------------------------------- --------------------------------------- 运行结果

    2023年04月26日
    浏览(21)
  • 3、MATLAB程序设计与应用刘卫国(第三版)课后实验三:顺序结构程序设计

    目录 一、  二、  三、  四、  五、  六、 从键盘输入一个4位整数,按如下规则加密后输出。加密规则:每位数字都加上7,然后用和除以10的余数取代该数字;然后将第一位数与第三位数互换,第二位数与第四位数互换。 ------------- -------- ------------ ------ 示例代码 ---------------

    2024年02月03日
    浏览(18)
  • 4、MATLAB程序设计与应用刘卫国(第三版)课后实验四:选择结构程序设计

    目录  一、  二、  三、  四、  五、 求分段函数的值   用 if语句实现,分别输出X=-5.0,-3.0,1.0,2.0,2.5,3.0,5.0时的y值。 ------------- -------- ------------ ------ 示例代码 - -------------------------- ------------------ ------------- -------- ------------ ------ 运行结果 - -------------------------- --------

    2024年02月05日
    浏览(13)
  • 【容器化应用程序设计和开发】2.5 容器化应用程序的安全性和合规性考虑

    往期回顾: 第一章:【云原生概念和技术】 第二章:2.1 容器化基础知识和Docker容器 第二章:2.2 Dockerfile 的编写和最佳实践 第二章:2.3 容器编排和Kubernetes调度 第二章:2.4 容器网络和存储 容器化应用程序是将应用程序和其依赖项打包到一个独立的、可移植的容器中,以便在

    2024年02月15日
    浏览(21)
  • Java程序设计:选实验6 输入输出应用

    (1) 编写一个程序,如果文件Exercisel_01.txt 不存在,就创建一个名为Exercisel_01.txt 的文件。向这个文件追加新数据。使用文本I/O将20个随机生成的整数写入这个文件。文件中的整数用空格分隔。 (2) 编写一个程序,如果文件Exercisel_02.dat 不存在,就创建一个名为Exercisel_02.dat 的文件

    2024年01月19日
    浏览(16)
  • 优雅设计之美:实现Vue应用程序的时尚布局

    前言 页面布局是减少代码重复和创建可维护且具有专业外观的应用程序的基本模式。如果使用的是Nuxt,则可以提供开箱即用的优雅解决方案。然而,令人遗憾的是,在Vue中,这些问题并未得到官方文档的解决。 经过多次尝试,小编得出了一个运行良好且可扩展而不会令人头

    2024年01月17日
    浏览(20)
  • 06-3_Qt 5.9 C++开发指南_多窗体应用程序的设计(主要的窗体类及其用途;窗体类重要特性设置;多窗口应用程序设计)

    常用的窗体基类是QWidget、QDialog 和QMainWindow,在创建 GUI应用程序时选择窗体基类就是从这 3 个类中选择。QWidget 直接继承于 QObject,是 QDialog 和 QMainWindow 的父类,其他继承于 QWidget 的窗体类还有 QSplashScreen、QMdiSubWindow和QDesktopWidget。另外还有一个类QWindow,它同时从 QObject 和Q

    2024年02月13日
    浏览(17)
  • 计算机程序设计-第9周(结构应用和链表)

    任务描述 本关任务:编写程序,定义一个结构date,包含年、月、日三个整型数据成员,使用结构date声明包含5个数组元素的结构数组,依次输入5个数组元素的值,按日期先后对数组进行排序,输出排序的结果。 测试说明 平台会对你编写的代码进行测试,并且约定输入输出的

    2024年02月12日
    浏览(12)
  • 【容器化应用程序设计和开发】2.2 容器编排和Kubernetes调度

    往期回顾: 第一章:【云原生概念和技术】 第二章:2.1 容器化基础知识和Docker容器 第二章:2.2 Dockerfile 的编写和最佳实践 容器编排是指自动化部署、管理和运行容器化应用程序的过程。Kubernetes 是一个流行的容器编排平台,它提供了一种自动化的方式来创建、部署和管理容

    2024年02月03日
    浏览(17)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包