- 最後登錄
- 2024-3-29
- 在線時間
- 0 小時
- 註冊時間
- 2016-8-22
- 閱讀權限
- 20
- 精華
- 0
- UID
- 16624936
- 帖子
- 61
- 積分
- 49 點
- 潛水值
- 26590 米
| 您的程式看不出來有甚麼問題,下面程式碼給您參考,看看和您的程式有甚麼差別
Form1.Designer.cs- namespace WindowsFormsApplication1
- {
- partial class Form1
- {
- /// <summary>
- /// 設計工具所需的變數。
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清除任何使用中的資源。
- /// </summary>
- /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form 設計工具產生的程式碼
- /// <summary>
- /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
- /// 修改這個方法的內容。
- /// </summary>
- private void InitializeComponent()
- {
- this.textBox1 = new System.Windows.Forms.TextBox();
- this.textBox2 = new System.Windows.Forms.TextBox();
- this.textBox3 = new System.Windows.Forms.TextBox();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.radioButton4 = new System.Windows.Forms.RadioButton();
- this.radioButton3 = new System.Windows.Forms.RadioButton();
- this.radioButton2 = new System.Windows.Forms.RadioButton();
- this.radioButton1 = new System.Windows.Forms.RadioButton();
- this.button1 = new System.Windows.Forms.Button();
- this.groupBox1.SuspendLayout();
- this.SuspendLayout();
- //
- // textBox1
- //
- this.textBox1.Location = new System.Drawing.Point(13, 41);
- this.textBox1.Name = "textBox1";
- this.textBox1.Size = new System.Drawing.Size(100, 25);
- this.textBox1.TabIndex = 0;
- //
- // textBox2
- //
- this.textBox2.Location = new System.Drawing.Point(13, 94);
- this.textBox2.Name = "textBox2";
- this.textBox2.Size = new System.Drawing.Size(100, 25);
- this.textBox2.TabIndex = 1;
- //
- // textBox3
- //
- this.textBox3.Location = new System.Drawing.Point(13, 146);
- this.textBox3.Name = "textBox3";
- this.textBox3.Size = new System.Drawing.Size(100, 25);
- this.textBox3.TabIndex = 2;
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.radioButton4);
- this.groupBox1.Controls.Add(this.radioButton1);
- this.groupBox1.Controls.Add(this.radioButton2);
- this.groupBox1.Controls.Add(this.radioButton3);
- this.groupBox1.Location = new System.Drawing.Point(162, 41);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(200, 185);
- this.groupBox1.TabIndex = 3;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "groupBox1";
- //
- // radioButton4
- //
- this.radioButton4.AutoSize = true;
- this.radioButton4.Location = new System.Drawing.Point(18, 109);
- this.radioButton4.Name = "radioButton4";
- this.radioButton4.Size = new System.Drawing.Size(32, 19);
- this.radioButton4.TabIndex = 3;
- this.radioButton4.Tag = "DIV";
- this.radioButton4.Text = "/";
- this.radioButton4.UseVisualStyleBackColor = true;
- //
- // radioButton3
- //
- this.radioButton3.AutoSize = true;
- this.radioButton3.Location = new System.Drawing.Point(18, 75);
- this.radioButton3.Name = "radioButton3";
- this.radioButton3.Size = new System.Drawing.Size(35, 19);
- this.radioButton3.TabIndex = 2;
- this.radioButton3.Tag = "MUL";
- this.radioButton3.Text = "*";
- this.radioButton3.UseVisualStyleBackColor = true;
- //
- // radioButton2
- //
- this.radioButton2.AutoSize = true;
- this.radioButton2.Location = new System.Drawing.Point(18, 50);
- this.radioButton2.Name = "radioButton2";
- this.radioButton2.Size = new System.Drawing.Size(33, 19);
- this.radioButton2.TabIndex = 1;
- this.radioButton2.Tag = "SUB";
- this.radioButton2.Text = "-";
- this.radioButton2.UseVisualStyleBackColor = true;
- //
- // radioButton1
- //
- this.radioButton1.AutoSize = true;
- this.radioButton1.Checked = true;
- this.radioButton1.Location = new System.Drawing.Point(18, 24);
- this.radioButton1.Name = "radioButton1";
- this.radioButton1.Size = new System.Drawing.Size(36, 19);
- this.radioButton1.TabIndex = 0;
- this.radioButton1.TabStop = true;
- this.radioButton1.Tag = "ADD";
- this.radioButton1.Text = "+";
- this.radioButton1.UseVisualStyleBackColor = true;
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(47, 246);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 4;
- this.button1.Text = "button1";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(430, 363);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.groupBox1);
- this.Controls.Add(this.textBox3);
- this.Controls.Add(this.textBox2);
- this.Controls.Add(this.textBox1);
- this.Name = "Form1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Form1";
- this.groupBox1.ResumeLayout(false);
- this.groupBox1.PerformLayout();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.TextBox textBox1;
- private System.Windows.Forms.TextBox textBox2;
- private System.Windows.Forms.TextBox textBox3;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.RadioButton radioButton4;
- private System.Windows.Forms.RadioButton radioButton3;
- private System.Windows.Forms.RadioButton radioButton2;
- private System.Windows.Forms.RadioButton radioButton1;
- private System.Windows.Forms.Button button1;
- }
- }
複製代碼 Form1.cs- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- delegate double Cal(int a, int b);
- Cal cal = null;
- public Form1()
- {
- InitializeComponent();
- this.radioButton1.CheckedChanged+=new EventHandler(radioButton_CheckedChanged);
- this.radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
- this.radioButton3.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
- this.radioButton4.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
- cal = new Cal(ADD);
- }
- private void radioButton_CheckedChanged(object sender, EventArgs e)
- {
- RadioButton rdo = sender as RadioButton;
- if (rdo != null)
- {
- switch (rdo.Tag.ToString())
- {
- case "ADD":
- cal = new Cal(ADD);
- break;
- case "SUB":
- cal = new Cal(SUB);
- break;
- case "MUL":
- cal = new Cal(MUL);
- break;
- case "DIV":
- cal = new Cal(DIV);
- break;
- default:
- cal = null;
- break;
- }
- }
- }
- static double ADD(int a, int b)
- {
- return a + b;
- }
- static double SUB(int a, int b)
- {
- return a-b;
- }
- static double MUL(int a, int b)
- {
- return a * b;
- }
- static double DIV(int a, int b)
- {
- return a / b;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int a = int.Parse(textBox1.Text);
- int b = int.Parse(textBox2.Text);
- textBox3.Text = cal.Invoke(a, b).ToString();
- }
- }
- }
複製代碼 ... |
|