伊莉討論區
標題:
C# 用委派寫 介面(+-*/)
[打印本頁]
作者:
asdfg0205
時間:
2017-3-8 01:46 PM
標題:
C# 用委派寫 介面(+-*/)
不好意思 請問一下 我是哪邊有問題了?
因為他都沒有跑錯誤訊息 ! 然後按開始 有介面沒錯 可是 變成 要顯示結果的 第3格 變成我能打字進去
按了運算並無用! 這是哪邊出錯ㄌ!?
作者:
Josie_2016
時間:
2017-3-9 06:04 PM
您的程式看不出來有甚麼問題,下面程式碼給您參考,看看和您的程式有甚麼差別
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();
}
}
}
複製代碼
作者:
cockroachrun
時間:
2017-3-10 02:44 PM
本帖最後由 cockroachrun 於 2017-3-10 02:46 PM 編輯
小弟有個問題. 為什麼叫用delegate 要用 Invoke() 有什麼特別的意義嗎?
如程式中
textBox3.Text = cal.Invoke(a, b).ToString();
寫成 textBox3.Text = cal(a,b).ToString(); 也會正常阿.
作者:
Josie_2016
時間:
2017-3-10 03:33 PM
To:cockroachrun
基本上沒甚麼不同,請參考下面網址
Delegate() vs. Delegate.Invoke()
http://jacksondunstan.com/articles/3283
作者:
bwong42!@#
時間:
2017-3-11 08:14 PM
提示:
作者被禁止或刪除 內容自動屏蔽
作者:
跨越青春嶺
時間:
2017-3-12 10:54 PM
本帖最後由 跨越青春嶺 於 2017-3-12 10:57 PM 編輯
供您參考,這樣或許更清楚明瞭.........?
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace HW_11271158
{
public partial class Form1 : Form
{
private delegate double Cal(int a, int b);
private Cal _cal = new Cal(ADD);
public Form1()
{
InitializeComponent();
addRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
subRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
mulRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
divRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
}
private void operation_CheckedChanged(object sender, EventArgs e)
{
var currentRadioButton = sender as RadioButton;
var operation = new Dictionary<string, Cal> {
{ "ADD", ADD },
{ "SUB", SUB },
{ "MUL", MUL },
{ "DIV", DIV },
};
_cal = operation[currentRadioButton.Tag.ToString()];
}
private static double ADD(int a, int b)
{
return a + b;
}
private static double SUB(int a, int b)
{
return a - b;
}
private static double MUL(int a, int b)
{
return a * b;
}
private static double DIV(int a, int b)
{
return a / b;
}
private void button1_Click(object sender, EventArgs e)
{
var a = int.Parse(textBox1.Text);
var b = int.Parse(textBox2.Text);
textBox3.Text = _cal(a, b).ToString();
}
}
}
複製代碼
作者:
vincent-vincent
時間:
2017-3-28 10:07 AM
CheckedChanged這個事件,包含選中和取消都會觸發,
所以你本來選中1改成2,那radio1_checkedChanged和radio2_checkedChanged兩個都會觸發到,
這裡應該會造成問題。
要加上if (radioButton1.Checked) 的判斷
歡迎光臨 伊莉討論區 (http://www07.eyny.com/)
Powered by Discuz!