C#中window窗体和控件

C#中window窗体和控件

布局与事件–界面的基本问题
在这里插入图片描述

布局

在这里插入图片描述
在这里插入图片描述
就是拉动窗体的时候,按钮也在跟着变动。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

事件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//简单的计算器
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace wincalc
{////// Summary description for calcForm.///public class calcForm : System.Windows.Forms.Form{private System.Windows.Forms.Button button1;private System.Windows.Forms.Button button2;private System.Windows.Forms.Button button3;private System.Windows.Forms.Button button4;private System.Windows.Forms.Button button5;private System.Windows.Forms.Button button6;private System.Windows.Forms.Button button7;private System.Windows.Forms.Button button8;private System.Windows.Forms.Button button9;private System.Windows.Forms.Button button10;private System.Windows.Forms.Button bClr;private System.Windows.Forms.Button bDot;private System.Windows.Forms.Button bPlus;private System.Windows.Forms.Button bSub;private System.Windows.Forms.Button bMul;private System.Windows.Forms.Button bDiv;private System.Windows.Forms.Button bEqu;private System.Windows.Forms.TextBox txtCalc;//以下是要添加的代码//定义变量Double dblAcc;Double dblSec;bool blnClear,blnFrstOpen;String strOper;//以上是添加的代码////// Required designer variable.///private System.ComponentModel.Container components = null;public calcForm(){//// Required for Windows Form Designer support//InitializeComponent();//// TODO: Add any constructor code after InitializeComponent call////以下是要添加的代码//初始化设量dblAcc=0;dblSec=0;blnFrstOpen=true;blnClear=true;strOper=new string('=',1);//以上是添加的代码}////// Clean up any resources being used.///protected override void Dispose( bool disposing ){if( disposing ){if(components != null){components.Dispose();}}base.Dispose( disposing );}#region Windows Form Designer generated code////// Required method for Designer support - do not modify/// the contents of this method with the code editor.///private void InitializeComponent(){this.bPlus = new System.Windows.Forms.Button();this.bMul = new System.Windows.Forms.Button();this.bDot = new System.Windows.Forms.Button();this.txtCalc = new System.Windows.Forms.TextBox();this.bClr = new System.Windows.Forms.Button();this.bDiv = new System.Windows.Forms.Button();this.bSub = new System.Windows.Forms.Button();this.button8 = new System.Windows.Forms.Button();this.button9 = new System.Windows.Forms.Button();this.bEqu = new System.Windows.Forms.Button();this.button10 = new System.Windows.Forms.Button();this.button4 = new System.Windows.Forms.Button();this.button5 = new System.Windows.Forms.Button();this.button6 = new System.Windows.Forms.Button();this.button7 = new System.Windows.Forms.Button();this.button1 = new System.Windows.Forms.Button();this.button2 = new System.Windows.Forms.Button();this.button3 = new System.Windows.Forms.Button();this.SuspendLayout();//// bPlus//this.bPlus.BackColor = System.Drawing.SystemColors.Control;this.bPlus.ForeColor = System.Drawing.SystemColors.ControlText;this.bPlus.Location = new System.Drawing.Point(208, 112);this.bPlus.Name = "bPlus";this.bPlus.Size = new System.Drawing.Size(32, 80);this.bPlus.TabIndex = 1;this.bPlus.Text = "+";//以下是要添加的代码bPlus.Click += new System.EventHandler(this.btn_Oper);//以上是添加的代码//// bMul//this.bMul.Location = new System.Drawing.Point(160, 112);this.bMul.Name = "bMul";this.bMul.Size = new System.Drawing.Size(32, 32);this.bMul.TabIndex = 1;this.bMul.Text = "*";//以下是要添加的代码bMul.Click += new System.EventHandler(this.btn_Oper);//以上是添加的代码//// bDot//this.bDot.ForeColor = System.Drawing.Color.Black;this.bDot.Location = new System.Drawing.Point(112, 208);this.bDot.Name = "bDot";this.bDot.Size = new System.Drawing.Size(32, 32);this.bDot.TabIndex = 0;this.bDot.Text = ".";//以下是要添加的代码bDot.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// txtCalc//this.txtCalc.Location = new System.Drawing.Point(16, 24);this.txtCalc.Name = "txtCalc";this.txtCalc.ReadOnly = true;this.txtCalc.RightToLeft = System.Windows.Forms.RightToLeft.Yes;this.txtCalc.Size = new System.Drawing.Size(224, 21);this.txtCalc.TabIndex = 2;this.txtCalc.Text = "";//// bClr//this.bClr.BackColor = System.Drawing.SystemColors.Control;this.bClr.ForeColor = System.Drawing.SystemColors.ControlText;this.bClr.Location = new System.Drawing.Point(208, 64);this.bClr.Name = "bClr";this.bClr.Size = new System.Drawing.Size(32, 32);this.bClr.TabIndex = 0;this.bClr.Text = "AC";//以下是要添加的代码bClr.Click += new System.EventHandler(this.btn_clr);//以上是添加的代码//// bDiv//this.bDiv.Location = new System.Drawing.Point(160, 160);this.bDiv.Name = "bDiv";this.bDiv.Size = new System.Drawing.Size(32, 32);this.bDiv.TabIndex = 1;this.bDiv.Text = "/";//以下是要添加的代码bDiv.Click += new System.EventHandler(this.btn_Oper);//以上是添加的代码//// bSub//this.bSub.Location = new System.Drawing.Point(160, 64);this.bSub.Name = "bSub";this.bSub.Size = new System.Drawing.Size(32, 32);this.bSub.TabIndex = 1;this.bSub.Text = "-";//以下是要添加的代码bSub.Click += new System.EventHandler(this.btn_Oper);//以上是添加的代码//// button8//this.button8.Location = new System.Drawing.Point(16, 64);this.button8.Name = "button8";this.button8.Size = new System.Drawing.Size(32, 32);this.button8.TabIndex = 0;this.button8.Text = "7";//以下是要添加的代码button8.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button9//this.button9.Location = new System.Drawing.Point(64, 64);this.button9.Name = "button9";this.button9.Size = new System.Drawing.Size(32, 32);this.button9.TabIndex = 0;this.button9.Text = "8";//以下是要添加的代码button9.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// bEqu//this.bEqu.BackColor = System.Drawing.SystemColors.Control;this.bEqu.ForeColor = System.Drawing.SystemColors.ControlText;this.bEqu.Location = new System.Drawing.Point(160, 208);this.bEqu.Name = "bEqu";this.bEqu.Size = new System.Drawing.Size(80, 32);this.bEqu.TabIndex = 1;this.bEqu.Text = "=";//以下是要添加的代码bEqu.Click += new System.EventHandler(this.btn_equ);//以上是添加的代码//// button10//this.button10.Location = new System.Drawing.Point(112, 64);this.button10.Name = "button10";this.button10.Size = new System.Drawing.Size(32, 32);this.button10.TabIndex = 0;this.button10.Text = "9";//以下是要添加的代码button10.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button4//this.button4.Location = new System.Drawing.Point(112, 160);this.button4.Name = "button4";this.button4.Size = new System.Drawing.Size(32, 32);this.button4.TabIndex = 0;this.button4.Text = "3";//以下是要添加的代码button4.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button5//this.button5.Location = new System.Drawing.Point(16, 112);this.button5.Name = "button5";this.button5.Size = new System.Drawing.Size(32, 32);this.button5.TabIndex = 0;this.button5.Text = "4";//以下是要添加的代码button5.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button6//this.button6.Location = new System.Drawing.Point(64, 112);this.button6.Name = "button6";this.button6.Size = new System.Drawing.Size(32, 32);this.button6.TabIndex = 0;this.button6.Text = "5";//以下是要添加的代码button6.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button7//this.button7.Location = new System.Drawing.Point(112, 112);this.button7.Name = "button7";this.button7.Size = new System.Drawing.Size(32, 32);this.button7.TabIndex = 0;this.button7.Text = "6";//以下是要添加的代码button7.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button1//this.button1.BackColor = System.Drawing.SystemColors.Control;this.button1.ForeColor = System.Drawing.Color.Black;this.button1.Location = new System.Drawing.Point(16, 208);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(80, 32);this.button1.TabIndex = 0;this.button1.Text = "0";//以下是要添加的代码button1.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button2//this.button2.Location = new System.Drawing.Point(16, 160);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(32, 32);this.button2.TabIndex = 0;this.button2.Text = "1";//以下是要添加的代码button2.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// button3//this.button3.Location = new System.Drawing.Point(64, 160);this.button3.Name = "button3";this.button3.Size = new System.Drawing.Size(32, 32);this.button3.TabIndex = 0;this.button3.Text = "2";//以下是要添加的代码button3.Click += new System.EventHandler(this.btn_clk);//以上是添加的代码//// calcForm//this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(256, 261);this.Controls.AddRange(new System.Windows.Forms.Control[] {this.txtCalc,this.bEqu,this.bDiv,this.bMul,this.bSub,this.bPlus,this.bDot,this.bClr,this.button10,this.button9,this.button8,this.button7,this.button6,this.button5,this.button4,this.button3,this.button2,this.button1});this.Name = "calcForm";this.Text = "计算器";this.ResumeLayout(false);}#endregion//以下是要添加的代码//小数点的操作private void btn_clk(object obj,EventArgs ea){if(blnClear)txtCalc.Text="";Button b3=(Button)obj;txtCalc.Text+=b3.Text;if(txtCalc.Text==".")txtCalc.Text="0.";dblSec=Convert.ToDouble(txtCalc.Text);blnClear=false;}//程序开始点private static void Main(){Application.Run(new calcForm());}private void btn_Oper(object obj,EventArgs ea){Button tmp=(Button)obj;strOper=tmp.Text;if(blnFrstOpen)dblAcc=dblSec;elsecalc();blnFrstOpen=false;blnClear=true;}//等号运算private void btn_equ(object obj,EventArgs ea){calc();}//所有运算操作private void calc(){switch(strOper){case "+":dblAcc+=dblSec; //加号运算break;case "-":dblAcc-=dblSec; //减号运算break;case "*":dblAcc*=dblSec; //乘号运算break;case "/":dblAcc/=dblSec; //除号运算break;}strOper="="; //等号运算blnFrstOpen=true;txtCalc.Text=Convert.ToString(dblAcc);//将运算结果转换成字符类型,并输出dblSec=dblAcc;//将运算数A的值放入运算数B中,以便后面运算}//清除按钮private void btn_clr(object obj,EventArgs ea){clear();}//清除按钮的操作private void clear(){dblAcc=0;dblSec=0;blnFrstOpen=true;txtCalc.Text="";txtCalc.Focus();//设置焦点为txtCalc}//以上是添加的代码}
} 

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/242257.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PHP自动加载机制

概述首先,为什么PHP需要自动加载呢? 在PHP面向对象(OO)编程中,为了方便管理,我们都会把一个类写在一个单独的文件中,那么如果想在A类中使用B类的功能,就需要把B类加载到A类。对于这样的需求在最原始的时候&…

操作系统知识——互斥和死锁

银行家算法银行家算法是最有代表性的避免死锁算法,是Dijkstra提出的。这是由于该算法能用于银行系统现金贷款的发放而得名。 我们可以把操作系统看作是银行家,操作系统管理的资源相当于银行家管理的资金,进程向操作系统请求分配资源相当于用户…

C#常用的控件

C#常用的控件 窗体 菜单、工具栏、对话框 用户控件及控件的继承

C++输入函数的应用

最近因为找工作的事情,在刷一些编程题,也陆陆续续参加了一些笔试,一般编程中使用的语言是C,因为编程题会有时间的限制,C的效率最高,不容易出现超时的问题所以使用最多的语言就是C。 在编程中,其…

C#绘图及图像

C#绘图及图像 绘图的基本方法 绘图的应用

PHP开发API

引言如何使用 PHP 开发 API(Application Programming Interface,应用程序编程接口) 呢? 做过 API 的人应该了解,其实开发 API 比开发 WEB 更简洁,但可能逻辑更复杂,因为 API 其实就是数据输出&a…

热部署和热加载

2018-8-22今天入职第二天,看到老大在群里发了一个go热加载实现的链接,之前没有听说过热加载,所以搜索了一下,下面是关于热部署和热加载的相关整理。 引言在项目线上更新时,可能会遇到需要热部署的情况,虽然…

C#实现定时器

C#实现定时器 方法一 布局 代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace…

HTTP请求流程

hhtp请求流程首先http是一个应用层的协议,在这个层的协议,只是一种通讯规范,也就是因为双方要进行通讯,大家要事先约定一个规范。 http请求与响应的步骤如下: URL解析、域名解析 输入URL的时候,首先浏览器会…

PHP执行外部程序的方法

引言在一些特殊情况下,会使用PHP调用外部程序执行,比如:调用shell命令、shell脚本、可执行程序等等 PHP执行外部程序的方法 system(),system函数将执行的结果直接输出exec():执行一个外部程序,将输出结果保…

C语言函数变量的引用

C语言函数变量的引用 有时候&#xff0c;你想引用函数中的某些变量。 #include <stdio.h>int main() {struct guoba{int pang;int piqi;};void tong(int *p){int month1 3;int month5 8;struct guoba xiaoguoba;xiaoguoba.pang month1 month5;*p xiaoguoba.pang ;…

php 终止程序的方法——return、exit()、die()

php中终止程序的运行有三种方式&#xff1a; return&#xff0c;主脚本程序中使用exit()&#xff0c;脚本中使用die()&#xff0c; 脚本中使用三者所在的脚本文件他们之后的代码都不会执行了。php开发模式下想直接看到变量的值使用exit()、die()。 returnreturn是语言结构而不是…

Eric Evans说DDD还未结束

在Explore DDD 2018大会上&#xff0c;Eric Evans做了“怀疑、乐观和实用主义”的主题演讲&#xff0c;他在演讲中表示&#xff0c;“DDD还没有结束”。在“领域驱动设计”这本书出版十五年之后&#xff0c;作者对这个想法长期以来如此流行表示了惊讶之情。他强调&#xff0c;D…

mark

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注…

C语言的进阶-指针的应用

指针的应用 #include <stdio.h> void swap(int *p,int *q); int main() {int a 5;int b 8;swap(&a,&b);printf("a%d,b%d\n",a,b);return 0; }void swap(int *p,int *q) {int t *p;*p *q;*q t; }函数只能返回一个值&#xff01; 可以通过参数返回多…

Go简单开始

Go是一门编译型语言&#xff0c;Go语言的工具链将源代码及其依赖转换成计算机的机器指令2。Go语言提供的工具都通过一个单独的命令 go 调用&#xff0c; go 命令有一系列子命令。最简单的一个子命令就是run。这个命令编译一个或多个以.go结尾的源文件&#xff0c;链接库文件&am…

C语言进阶-指针与数组

C语言进阶-指针与数组 q可以改变指向的内容&#xff0c;但不能改变指向的地址 p可以改变指向地址&#xff0c;但不能改变指向内容

测试下文章

这是一篇测试 编辑

发布文章测试

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注…