OnlyACry

C#小作业

Designer.cs

  1 namespace _2_1
  2 {
  3     partial class Form1
  4     {
  5         /// <summary>
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region Windows 窗体设计器生成的代码
 24 
 25         /// <summary>
 26         /// 设计器支持所需的方法 - 不要修改
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.Num = new System.Windows.Forms.Label();
 32             this.txt = new System.Windows.Forms.TextBox();
 33             this.button1 = new System.Windows.Forms.Button();
 34             this.button2 = new System.Windows.Forms.Button();
 35             this.button3 = new System.Windows.Forms.Button();
 36             this.button4 = new System.Windows.Forms.Button();
 37             this.button5 = new System.Windows.Forms.Button();
 38             this.button6 = new System.Windows.Forms.Button();
 39             this.button7 = new System.Windows.Forms.Button();
 40             this.button8 = new System.Windows.Forms.Button();
 41             this.button9 = new System.Windows.Forms.Button();
 42             this.button10 = new System.Windows.Forms.Button();
 43             this.button11 = new System.Windows.Forms.Button();
 44             this.button13 = new System.Windows.Forms.Button();
 45             this.button14 = new System.Windows.Forms.Button();
 46             this.button15 = new System.Windows.Forms.Button();
 47             this.button16 = new System.Windows.Forms.Button();
 48             this.button12 = new System.Windows.Forms.Button();
 49             this.SuspendLayout();
 50             // 
 51             // Num
 52             // 
 53             this.Num.AutoSize = true;
 54             this.Num.Location = new System.Drawing.Point(33, 29);
 55             this.Num.Name = "Num";
 56             this.Num.Size = new System.Drawing.Size(17, 18);
 57             this.Num.TabIndex = 0;
 58             this.Num.Text = " ";
 59             // 
 60             // txt
 61             // 
 62             this.txt.Location = new System.Drawing.Point(36, 64);
 63             this.txt.Name = "txt";
 64             this.txt.Size = new System.Drawing.Size(381, 28);
 65             this.txt.TabIndex = 1;
 66             // 
 67             // button1
 68             // 
 69             this.button1.Location = new System.Drawing.Point(36, 134);
 70             this.button1.Name = "button1";
 71             this.button1.Size = new System.Drawing.Size(59, 44);
 72             this.button1.TabIndex = 2;
 73             this.button1.Text = "1";
 74             this.button1.UseVisualStyleBackColor = true;
 75             this.button1.Click += new System.EventHandler(this.button1_Click);
 76             // 
 77             // button2
 78             // 
 79             this.button2.Location = new System.Drawing.Point(139, 134);
 80             this.button2.Name = "button2";
 81             this.button2.Size = new System.Drawing.Size(59, 44);
 82             this.button2.TabIndex = 3;
 83             this.button2.Text = "2";
 84             this.button2.UseVisualStyleBackColor = true;
 85             this.button2.Click += new System.EventHandler(this.button1_Click);
 86             // 
 87             // button3
 88             // 
 89             this.button3.Location = new System.Drawing.Point(247, 134);
 90             this.button3.Name = "button3";
 91             this.button3.Size = new System.Drawing.Size(59, 44);
 92             this.button3.TabIndex = 4;
 93             this.button3.Text = "3";
 94             this.button3.UseVisualStyleBackColor = true;
 95             this.button3.Click += new System.EventHandler(this.button1_Click);
 96             // 
 97             // button4
 98             // 
 99             this.button4.Location = new System.Drawing.Point(36, 213);
100             this.button4.Name = "button4";
101             this.button4.Size = new System.Drawing.Size(59, 44);
102             this.button4.TabIndex = 5;
103             this.button4.Text = "4";
104             this.button4.UseVisualStyleBackColor = true;
105             this.button4.Click += new System.EventHandler(this.button1_Click);
106             // 
107             // button5
108             // 
109             this.button5.Location = new System.Drawing.Point(139, 213);
110             this.button5.Name = "button5";
111             this.button5.Size = new System.Drawing.Size(59, 44);
112             this.button5.TabIndex = 6;
113             this.button5.Text = "5";
114             this.button5.UseVisualStyleBackColor = true;
115             this.button5.Click += new System.EventHandler(this.button1_Click);
116             // 
117             // button6
118             // 
119             this.button6.Location = new System.Drawing.Point(247, 213);
120             this.button6.Name = "button6";
121             this.button6.Size = new System.Drawing.Size(59, 44);
122             this.button6.TabIndex = 7;
123             this.button6.Text = "6";
124             this.button6.UseVisualStyleBackColor = true;
125             this.button6.Click += new System.EventHandler(this.button1_Click);
126             // 
127             // button7
128             // 
129             this.button7.Location = new System.Drawing.Point(36, 297);
130             this.button7.Name = "button7";
131             this.button7.Size = new System.Drawing.Size(59, 44);
132             this.button7.TabIndex = 8;
133             this.button7.Text = "7";
134             this.button7.UseVisualStyleBackColor = true;
135             this.button7.Click += new System.EventHandler(this.button1_Click);
136             // 
137             // button8
138             // 
139             this.button8.Location = new System.Drawing.Point(139, 297);
140             this.button8.Name = "button8";
141             this.button8.Size = new System.Drawing.Size(59, 44);
142             this.button8.TabIndex = 9;
143             this.button8.Text = "8";
144             this.button8.UseVisualStyleBackColor = true;
145             this.button8.Click += new System.EventHandler(this.button1_Click);
146             // 
147             // button9
148             // 
149             this.button9.Location = new System.Drawing.Point(247, 297);
150             this.button9.Name = "button9";
151             this.button9.Size = new System.Drawing.Size(59, 44);
152             this.button9.TabIndex = 10;
153             this.button9.Text = "9";
154             this.button9.UseVisualStyleBackColor = true;
155             this.button9.Click += new System.EventHandler(this.button1_Click);
156             // 
157             // button10
158             // 
159             this.button10.Location = new System.Drawing.Point(36, 380);
160             this.button10.Name = "button10";
161             this.button10.Size = new System.Drawing.Size(59, 44);
162             this.button10.TabIndex = 11;
163             this.button10.Text = "0";
164             this.button10.UseVisualStyleBackColor = true;
165             this.button10.Click += new System.EventHandler(this.button1_Click);
166             // 
167             // button11
168             // 
169             this.button11.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
170             this.button11.Location = new System.Drawing.Point(139, 380);
171             this.button11.Name = "button11";
172             this.button11.Size = new System.Drawing.Size(59, 44);
173             this.button11.TabIndex = 12;
174             this.button11.Text = ".";
175             this.button11.UseVisualStyleBackColor = true;
176             this.button11.Click += new System.EventHandler(this.button1_Click);
177             // 
178             // button13
179             // 
180             this.button13.Location = new System.Drawing.Point(358, 134);
181             this.button13.Name = "button13";
182             this.button13.Size = new System.Drawing.Size(59, 44);
183             this.button13.TabIndex = 14;
184             this.button13.Text = "+";
185             this.button13.UseVisualStyleBackColor = true;
186             this.button13.Click += new System.EventHandler(this.button13_Click);
187             // 
188             // button14
189             // 
190             this.button14.Location = new System.Drawing.Point(358, 213);
191             this.button14.Name = "button14";
192             this.button14.Size = new System.Drawing.Size(59, 44);
193             this.button14.TabIndex = 15;
194             this.button14.Text = "-";
195             this.button14.UseVisualStyleBackColor = true;
196             this.button14.Click += new System.EventHandler(this.button13_Click);
197             // 
198             // button15
199             // 
200             this.button15.Location = new System.Drawing.Point(358, 297);
201             this.button15.Name = "button15";
202             this.button15.Size = new System.Drawing.Size(59, 44);
203             this.button15.TabIndex = 16;
204             this.button15.Text = "*";
205             this.button15.UseVisualStyleBackColor = true;
206             this.button15.Click += new System.EventHandler(this.button13_Click);
207             // 
208             // button16
209             // 
210             this.button16.Location = new System.Drawing.Point(358, 380);
211             this.button16.Name = "button16";
212             this.button16.Size = new System.Drawing.Size(59, 44);
213             this.button16.TabIndex = 17;
214             this.button16.Text = "/";
215             this.button16.UseVisualStyleBackColor = true;
216             this.button16.Click += new System.EventHandler(this.button13_Click);
217             // 
218             // button12
219             // 
220             this.button12.Location = new System.Drawing.Point(247, 380);
221             this.button12.Name = "button12";
222             this.button12.Size = new System.Drawing.Size(59, 44);
223             this.button12.TabIndex = 18;
224             this.button12.Text = "=";
225             this.button12.UseVisualStyleBackColor = true;
226             this.button12.Click += new System.EventHandler(this.button12_Click);
227             // 
228             // Form1
229             // 
230             this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
231             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
232             this.ClientSize = new System.Drawing.Size(456, 475);
233             this.Controls.Add(this.button12);
234             this.Controls.Add(this.button16);
235             this.Controls.Add(this.button15);
236             this.Controls.Add(this.button14);
237             this.Controls.Add(this.button13);
238             this.Controls.Add(this.button11);
239             this.Controls.Add(this.button10);
240             this.Controls.Add(this.button9);
241             this.Controls.Add(this.button8);
242             this.Controls.Add(this.button7);
243             this.Controls.Add(this.button6);
244             this.Controls.Add(this.button5);
245             this.Controls.Add(this.button4);
246             this.Controls.Add(this.button3);
247             this.Controls.Add(this.button2);
248             this.Controls.Add(this.button1);
249             this.Controls.Add(this.txt);
250             this.Controls.Add(this.Num);
251             this.Name = "Form1";
252             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
253             this.Text = "计算器";
254             this.Load += new System.EventHandler(this.Form1_Load);
255             this.ResumeLayout(false);
256             this.PerformLayout();
257 
258         }
259 
260         #endregion
261 
262         private System.Windows.Forms.Label Num;
263         private System.Windows.Forms.TextBox txt;
264         private System.Windows.Forms.Button button1;
265         private System.Windows.Forms.Button button2;
266         private System.Windows.Forms.Button button3;
267         private System.Windows.Forms.Button button4;
268         private System.Windows.Forms.Button button5;
269         private System.Windows.Forms.Button button6;
270         private System.Windows.Forms.Button button7;
271         private System.Windows.Forms.Button button8;
272         private System.Windows.Forms.Button button9;
273         private System.Windows.Forms.Button button10;
274         private System.Windows.Forms.Button button11;
275         private System.Windows.Forms.Button button13;
276         private System.Windows.Forms.Button button14;
277         private System.Windows.Forms.Button button15;
278         private System.Windows.Forms.Button button16;
279         private System.Windows.Forms.Button button12;
280     }
281 }
《我也不知道为什么这个文件会有这么长》

Form1.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace _2_1
12 {
13     public partial class Form1 : Form
14     {
15         private string save = "";
16         private double x = 0, y = 0;
17         private Button btn = new Button();
18         public Form1()
19         {
20             InitializeComponent();
21         }
22 
23         private void Form1_Load(object sender, EventArgs e)
24         {
25             txt.Text = "";
26             Num.Text = "";
27         }
28 
29         private void button13_Click(object sender, EventArgs e)
30         {
31             btn = (Button)sender;
32             //判断文本框中是否为空,如果为空,那么输出错误信息
33             if (txt.Text == "") MessageBox.Show("错误!", " ");
34             else
35             {
36                 //如果运算符不是“=”,那么保存当前运算符,并保留初始值
37                 if (txt.Text != "") x = Convert.ToDouble(txt.Text);
38                 else MessageBox.Show("错误!将赋值为0", " ");
39                 //将txt当前的文本内容放到lable上,txt重置
40                 Num.Text = txt.Text;
41                 txt.Text = "";
42                 //保留当前运算符
43                 save = btn.Text;
44             }
45         }
46         //“=”运算符
47         private void button12_Click(object sender, EventArgs e)
48         {
49             if (txt.Text == "")
50             {
51                 MessageBox.Show("错误!", " ");
52             }
53             else
54             {
55                 y = Convert.ToDouble(txt.Text);
56                 if (save == "+") txt.Text = (x + y).ToString();
57                 else if (save == "-") txt.Text = (x - y).ToString();
58                 else if (save == "*") txt.Text = (x * y).ToString();
59                 else
60                 {
61                     if (x == 0)
62                     {
63                         MessageBox.Show("错误,除数不能为零!", "");
64                     }
65                     else
66                     {
67                         txt.Text = (x / y).ToString();
68                     }
69                 }
70                 MessageBox.Show("结果为" + txt.Text, " ");
71                 //全部清零为下次运算做准备
72                 txt.Text = "";
73                 Num.Text = "";
74             }
75         }
76 
77         private void button1_Click(object sender, EventArgs e)
78         {
79             //new一个实例,管理数字按钮
80             btn = (Button)sender;
81             //文本框文本为按钮名称加上之前输入的内容
82             txt.Text = txt.Text + btn.Text;
83         }
84     }
85 }

分类:

技术点:

相关文章:

  • 2021-12-04
  • 2021-11-27
  • 2021-09-30
  • 2021-12-02
  • 2021-12-02
  • 2021-11-21
  • 2021-11-27
  • 2021-09-07
猜你喜欢
  • 2021-11-21
  • 2021-05-29
  • 2021-11-21
  • 2021-11-27
  • 2021-09-16
  • 2021-11-21
  • 2021-11-05
相关资源
相似解决方案