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,一经查实,立即删除!

相关文章

C#常用的控件

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

C#绘图及图像

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

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…

mark

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右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; 可以通过参数返回多…

C语言进阶-指针与数组

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

发布文章测试

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

C语言进阶-指针运算

C语言进阶-指针运算 #include <stdio.h>int main() {char a[] {0,1,2,3,4,5,6,7,8,9};char *p a;printf("p%d\n",p);printf("p1%d\n",p1);int b[] {0,1,2,3,4,5,6,7,8,9};int *q b;printf("q%d\n",q);printf("q1%d\n",q1);r…

C语言进阶-动态内存分配

C语言进阶-动态内存分配 #include <stdio.h> #include<stdlib.h>int main() {int number;int *a;int i;printf("请输入数组的数量");scanf("%d",&number);//int a[number];a(int*)malloc(number*sizeof(int));//开辟数组的大小for(i 0;i&…

测测

编辑 1.多喝水 2.清黑头&#xff0c;一周做一次角质&#xff0c;一个月做一次小气泡 3.防晒 无论春夏秋冬 4.少吃辣&#xff0c;多喝柠檬水 5.早上护肤步骤&#xff1a;洁面-爽肤水-眼霜-精华-面霜-防晒 6.晚上护肤步骤&#xff1a;卸妆-洁面-水-眼霜-精华-面霜 7.一周三…

QT输出出现乱码的解决

QT输出出现乱码的解决

测试图片外链MD

11111 11111111111111111111111111111 555555555

测试阿里云HTML

一直在说事件&#xff0c;那么事件到底是指什么&#xff1f;这里所说的事件是指手指按下(down)、移动(move)、抬起(up)此为一个事件集合或者说是事件序列&#xff0c;从手指接触屏幕开始到手指离开屏幕结束。所以本篇所说的事件序列或者事件集合是指从手指刚接触屏幕到离开屏幕…

测试阿里云MD

准备工作已经完成&#xff0c;闲言少叙书归正传吧。 和拦截处理机制详解一样&#xff0c;为了系统的研究android对事件的处理&#xff0c;我也写了一个小demo对不同的情况进行测试并结合源码分析&#xff08;多说一句&#xff0c;其实看源码确实很枯燥&#xff0c;有时候因为水…

C语言进阶-结构体

C语言进阶-结构体

测送图片居中

带尺寸的图片: 居中的图片: 居中并且带尺寸的图片:

ADC0832程序完整版 源码+Proteus仿真

前段时间一直在为ADC0832的程序感到疑惑&#xff0c;从网上找了很多的代码&#xff0c;用Proteus仿真&#xff0c;最后都出现了一些奇怪的问题&#xff0c;有的根本没法读取数据&#xff0c;有的数据有错误。 当参考电压为5V时&#xff0c;如果把输入电压从0一直调到5V&#x…

我国是世界最大石油进口国,但是大家知道从哪些国家进口吗?

大家都知道我国是世界上最主要的石油进口国&#xff0c;目前我国原油对进口的依赖程度超过70%。下图是2018年上半年我国原油消费以及国内原油产量相关统计数据。从这个图当中&#xff0c;我们可以看出2018年上半年&#xff0c;我国每月消费的原油都在5000万吨以上&#xff0c;而…