每次我运行Windows窗体时,它都是空白的,没有任何按钮。
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 Calculator_project
{
public partial class Calculator : Form
{
Double value = 0;
String operation = "";
bool operation_pressed = false;
public Calculator()
{
InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
if ((result.Text == "0") || (operation_pressed))
result.Clear();
operation_pressed = false;
Button b = (Button)sender;
result.Text = result.Text + b.Text;
}
private void Button1_Click(object sender, EventArgs e)
{
// Method intentionally left empty.
}
private void operator_click(object sender, EventArgs e)
{
Button b = (Button)sender;
operation = b.Text;
value = Double.Parse(result.Text);
operation_pressed = true;
equation.Text = value + " " + operation;
}
private void Button18_Click(object sender, EventArgs e)
{
equation.Text = "";
switch (operation)
{
case "+":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
case "-":
result.Text = (value - Double.Parse(result.Text)).ToString();
break;
case "*":
result.Text = (value * Double.Parse(result.Text)).ToString();
break;
case "/":
result.Text = (value / Double.Parse(result.Text)).ToString();
break;
default:
break;
}//end switch
}
private void Button6_Click(object sender, EventArgs e)
{
// Method intentionally left empty.
}
private void Calculator_Load(object sender, EventArgs e)
{
// Method intentionally left empty.
}
private void Button5_Click(object sender, EventArgs e)
{
result.Text = "0";
}
private void Button10_Click(object sender, EventArgs e)
{
result.Text = "0";
value = 0;
}
}
}
这只是我尝试通过视频教程制作的一个简单计算器。除了一些小错误外,我完全按照他的方法进行操作。如您所知,我是新手,我们将不胜感激。
这是表单设计器的代码。
namespace Calculator_project
{
partial class Calculator
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.result = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = 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.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.equation = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 51);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(45, 45);
this.button1.TabIndex = 1;
this.button1.Text = "7";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button_Click);
//
// result
//
this.result.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.result.Location = new System.Drawing.Point(12, 12);
this.result.Name = "result";
this.result.Size = new System.Drawing.Size(246, 30);
this.result.TabIndex = 2;
this.result.Text = "0";
this.result.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// button2
//
this.button2.Location = new System.Drawing.Point(63, 51);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(45, 45);
this.button2.TabIndex = 3;
this.button2.Text = "8";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.Button_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(114, 51);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(45, 45);
this.button3.TabIndex = 4;
this.button3.Text = "9";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.Button_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(165, 51);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(45, 45);
this.button4.TabIndex = 5;
this.button4.Text = "+";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.operator_click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(213, 51);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(45, 45);
this.button5.TabIndex = 6;
this.button5.Text = "CE";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.Button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(12, 102);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(45, 45);
this.button6.TabIndex = 7;
this.button6.Text = "4";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.Button_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(63, 102);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(45, 45);
this.button7.TabIndex = 8;
this.button7.Text = "5";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.Button_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(114, 102);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(45, 45);
this.button8.TabIndex = 9;
this.button8.Text = "6";
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.Button_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(165, 102);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(45, 45);
this.button9.TabIndex = 10;
this.button9.Text = "-";
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.operator_click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(213, 102);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(45, 45);
this.button10.TabIndex = 11;
this.button10.Text = "C";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.Button10_Click);
//
// button11
//
this.button11.Location = new System.Drawing.Point(12, 153);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(45, 45);
this.button11.TabIndex = 12;
this.button11.Text = "1";
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.Button_Click);
//
// button12
//
this.button12.Location = new System.Drawing.Point(63, 153);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(45, 45);
this.button12.TabIndex = 13;
this.button12.Text = "2";
this.button12.UseVisualStyleBackColor = true;
this.button12.Click += new System.EventHandler(this.Button_Click);
//
// button13
//
this.button13.Location = new System.Drawing.Point(114, 153);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(45, 45);
this.button13.TabIndex = 14;
this.button13.Text = "3";
this.button13.UseVisualStyleBackColor = true;
this.button13.Click += new System.EventHandler(this.Button_Click);
//
// button14
//
this.button14.Location = new System.Drawing.Point(165, 153);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(45, 45);
this.button14.TabIndex = 15;
this.button14.Text = "*";
this.button14.UseVisualStyleBackColor = true;
this.button14.Click += new System.EventHandler(this.operator_click);
//
// button15
//
this.button15.Location = new System.Drawing.Point(114, 204);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(45, 45);
this.button15.TabIndex = 16;
this.button15.Text = ".";
this.button15.UseVisualStyleBackColor = true;
this.button15.Click += new System.EventHandler(this.Button_Click);
//
// button16
//
this.button16.Location = new System.Drawing.Point(165, 204);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(45, 45);
this.button16.TabIndex = 17;
this.button16.Text = "/";
this.button16.UseVisualStyleBackColor = true;
this.button16.Click += new System.EventHandler(this.operator_click);
//
// button17
//
this.button17.Location = new System.Drawing.Point(213, 153);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(45, 96);
this.button17.TabIndex = 18;
this.button17.Text = "=";
this.button17.UseVisualStyleBackColor = true;
this.button17.Click += new System.EventHandler(this.Button18_Click);
//
// button18
//
this.button18.Location = new System.Drawing.Point(12, 204);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(96, 45);
this.button18.TabIndex = 19;
this.button18.Text = "0";
this.button18.UseVisualStyleBackColor = true;
this.button18.Click += new System.EventHandler(this.Button_Click);
//
// equation
//
this.equation.AutoSize = true;
this.equation.BackColor = System.Drawing.SystemColors.Window;
this.equation.Location = new System.Drawing.Point(20, 20);
this.equation.Name = "equation";
this.equation.Size = new System.Drawing.Size(0, 17);
this.equation.TabIndex = 20;
//
// Calculator
//
this.ClientSize = new System.Drawing.Size(270, 253);
this.Controls.Add(this.equation);
this.Controls.Add(this.button18);
this.Controls.Add(this.button17);
this.Controls.Add(this.button16);
this.Controls.Add(this.button15);
this.Controls.Add(this.button14);
this.Controls.Add(this.button13);
this.Controls.Add(this.button12);
this.Controls.Add(this.button11);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.result);
this.Controls.Add(this.button1);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.MaximizeBox = false;
this.Name = "Calculator";
this.Text = "Calculator";
this.Load += new System.EventHandler(this.Calculator_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox result;
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 button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Label equation;
}
}