代码是:
private void button1_Click(object sender, EventArgs e)
{
double result = 0;
double num1=Convert.ToDouble(textBox1.Text);
double num2=Convert.ToDouble(textBox2.Text);
if (radioButton1.Checked)
{
result = num1 + num2;
}
else if (radioButton2.Checked)
{
result = num1 - num2;
}
else if (radioButton3.Checked)
{
result = num1 * num2;
}
else if (radioButton4.Checked&&num1!=0)
{
result = num1 / num2;
}
textBox3.Text = result.ToString();
}
public class FrmComputer : Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button bt计算;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label3;
public FrmComputer()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.bt计算 = new System.Windows.Forms.Button();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(45, 35);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(23, 12);
this.label1.TabIndex = 0;
this.label1.Text = "数1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(45, 79);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(23, 12);
this.label2.TabIndex = 1;
this.label2.Text = "数2";
//
// bt计算
//
this.bt计算.Location = new System.Drawing.Point(95, 162);
this.bt计算.Name = "bt计算";
this.bt计算.Size = new System.Drawing.Size(75, 23);
this.bt计算.TabIndex = 2;
this.bt计算.Text = "计算";
this.bt计算.UseVisualStyleBackColor = true;
this.bt计算.Click += new System.EventHandler(this.bt计算_Click);
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(48, 117);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(29, 16);
this.radioButton1.TabIndex = 3;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "+";
this.radioButton1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(94, 31);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 4;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(94, 76);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 5;
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(90, 117);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(29, 16);
this.radioButton2.TabIndex = 6;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "-";
this.radioButton2.UseVisualStyleBackColor = true;
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(132, 117);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(29, 16);
this.radioButton3.TabIndex = 7;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "*";
this.radioButton3.UseVisualStyleBackColor = true;
//
// radioButton4
//
this.radioButton4.AutoSize = true;
this.radioButton4.Location = new System.Drawing.Point(174, 117);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(29, 16);
this.radioButton4.TabIndex = 8;
this.radioButton4.TabStop = true;
this.radioButton4.Text = "/";
this.radioButton4.UseVisualStyleBackColor = true;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(90, 207);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(104, 21);
this.textBox3.TabIndex = 10;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(41, 211);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 9;
this.label3.Text = "结果";
//
// FrmComputer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(268, 262);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.radioButton4);
this.Controls.Add(this.radioButton3);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.bt计算);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "FrmComputer";
this.ShowIcon = false;
this.Text = "数值计算";
this.ResumeLayout(false);
this.PerformLayout();
}
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void bt计算_Click(object sender, EventArgs e)
{
string err =checkValue();
if (err != "")
{
MessageBox.Show(err);
return;
}
if (this.radioButton1.Checked)
{
this.textBox3.Text = (value1 + value2).ToString();
return;
}
if (this.radioButton2.Checked)
{
this.textBox3.Text = (value1 - value2).ToString();
return;
}
if (this.radioButton3.Checked)
{
this.textBox3.Text = (value1 * value2).ToString();
return;
}
if (this.radioButton4.Checked)
{
if (this.textBox3.Text == "0")
{
MessageBox.Show("除数不能为零值!");
return;
}
this.textBox3.Text = (value1 / value2).ToString();
return;
}
}
dynamic value1;
dynamic value2;
private string checkValue()
{
try
{
if (this.textBox1.Text.Contains("."))
{
value1 = Convert.ToDouble(this.textBox1.Text);
}
else
{
value1 = Convert.ToInt32(this.textBox1.Text);
}
}
catch
{
return "数值1:输入值有误!";
}
try
{
if (this.textBox1.Text.Contains("."))
{
value2 = Convert.ToDouble(this.textBox2.Text);
}
else
{
value2 = Convert.ToInt32(this.textBox2.Text);
}
}
catch
{
return "数值2:输入值有误!";
}
return "";
}
}
把加减乘除的代码写在button里面,就好了。简单的
窗体,button
参考 Demo2