伊莉討論區

標題: C# 用委派寫 介面(+-*/) [打印本頁]

作者: asdfg0205    時間: 2017-3-8 01:46 PM     標題: C# 用委派寫 介面(+-*/)

不好意思 請問一下 我是哪邊有問題了?
因為他都沒有跑錯誤訊息 ! 然後按開始 有介面沒錯 可是 變成 要顯示結果的 第3格 變成我能打字進去
按了運算並無用! 這是哪邊出錯ㄌ!?

作者: Josie_2016    時間: 2017-3-9 06:04 PM

您的程式看不出來有甚麼問題,下面程式碼給您參考,看看和您的程式有甚麼差別

Form1.Designer.cs
  1. namespace WindowsFormsApplication1
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// 設計工具所需的變數。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;

  9.         /// <summary>
  10.         /// 清除任何使用中的資源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }

  21.         #region Windows Form 設計工具產生的程式碼

  22.         /// <summary>
  23.         /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
  24.         /// 修改這個方法的內容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.textBox1 = new System.Windows.Forms.TextBox();
  29.             this.textBox2 = new System.Windows.Forms.TextBox();
  30.             this.textBox3 = new System.Windows.Forms.TextBox();
  31.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  32.             this.radioButton4 = new System.Windows.Forms.RadioButton();
  33.             this.radioButton3 = new System.Windows.Forms.RadioButton();
  34.             this.radioButton2 = new System.Windows.Forms.RadioButton();
  35.             this.radioButton1 = new System.Windows.Forms.RadioButton();
  36.             this.button1 = new System.Windows.Forms.Button();
  37.             this.groupBox1.SuspendLayout();
  38.             this.SuspendLayout();
  39.             //
  40.             // textBox1
  41.             //
  42.             this.textBox1.Location = new System.Drawing.Point(13, 41);
  43.             this.textBox1.Name = "textBox1";
  44.             this.textBox1.Size = new System.Drawing.Size(100, 25);
  45.             this.textBox1.TabIndex = 0;
  46.             //
  47.             // textBox2
  48.             //
  49.             this.textBox2.Location = new System.Drawing.Point(13, 94);
  50.             this.textBox2.Name = "textBox2";
  51.             this.textBox2.Size = new System.Drawing.Size(100, 25);
  52.             this.textBox2.TabIndex = 1;
  53.             //
  54.             // textBox3
  55.             //
  56.             this.textBox3.Location = new System.Drawing.Point(13, 146);
  57.             this.textBox3.Name = "textBox3";
  58.             this.textBox3.Size = new System.Drawing.Size(100, 25);
  59.             this.textBox3.TabIndex = 2;
  60.             //
  61.             // groupBox1
  62.             //
  63.             this.groupBox1.Controls.Add(this.radioButton4);
  64.             this.groupBox1.Controls.Add(this.radioButton1);
  65.             this.groupBox1.Controls.Add(this.radioButton2);
  66.             this.groupBox1.Controls.Add(this.radioButton3);
  67.             this.groupBox1.Location = new System.Drawing.Point(162, 41);
  68.             this.groupBox1.Name = "groupBox1";
  69.             this.groupBox1.Size = new System.Drawing.Size(200, 185);
  70.             this.groupBox1.TabIndex = 3;
  71.             this.groupBox1.TabStop = false;
  72.             this.groupBox1.Text = "groupBox1";
  73.             //
  74.             // radioButton4
  75.             //
  76.             this.radioButton4.AutoSize = true;
  77.             this.radioButton4.Location = new System.Drawing.Point(18, 109);
  78.             this.radioButton4.Name = "radioButton4";
  79.             this.radioButton4.Size = new System.Drawing.Size(32, 19);
  80.             this.radioButton4.TabIndex = 3;
  81.             this.radioButton4.Tag = "DIV";
  82.             this.radioButton4.Text = "/";
  83.             this.radioButton4.UseVisualStyleBackColor = true;
  84.             //
  85.             // radioButton3
  86.             //
  87.             this.radioButton3.AutoSize = true;
  88.             this.radioButton3.Location = new System.Drawing.Point(18, 75);
  89.             this.radioButton3.Name = "radioButton3";
  90.             this.radioButton3.Size = new System.Drawing.Size(35, 19);
  91.             this.radioButton3.TabIndex = 2;
  92.             this.radioButton3.Tag = "MUL";
  93.             this.radioButton3.Text = "*";
  94.             this.radioButton3.UseVisualStyleBackColor = true;
  95.             //
  96.             // radioButton2
  97.             //
  98.             this.radioButton2.AutoSize = true;
  99.             this.radioButton2.Location = new System.Drawing.Point(18, 50);
  100.             this.radioButton2.Name = "radioButton2";
  101.             this.radioButton2.Size = new System.Drawing.Size(33, 19);
  102.             this.radioButton2.TabIndex = 1;
  103.             this.radioButton2.Tag = "SUB";
  104.             this.radioButton2.Text = "-";
  105.             this.radioButton2.UseVisualStyleBackColor = true;
  106.             //
  107.             // radioButton1
  108.             //
  109.             this.radioButton1.AutoSize = true;
  110.             this.radioButton1.Checked = true;
  111.             this.radioButton1.Location = new System.Drawing.Point(18, 24);
  112.             this.radioButton1.Name = "radioButton1";
  113.             this.radioButton1.Size = new System.Drawing.Size(36, 19);
  114.             this.radioButton1.TabIndex = 0;
  115.             this.radioButton1.TabStop = true;
  116.             this.radioButton1.Tag = "ADD";
  117.             this.radioButton1.Text = "+";
  118.             this.radioButton1.UseVisualStyleBackColor = true;
  119.             //
  120.             // button1
  121.             //
  122.             this.button1.Location = new System.Drawing.Point(47, 246);
  123.             this.button1.Name = "button1";
  124.             this.button1.Size = new System.Drawing.Size(75, 23);
  125.             this.button1.TabIndex = 4;
  126.             this.button1.Text = "button1";
  127.             this.button1.UseVisualStyleBackColor = true;
  128.             this.button1.Click += new System.EventHandler(this.button1_Click);
  129.             //
  130.             // Form1
  131.             //
  132.             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
  133.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  134.             this.ClientSize = new System.Drawing.Size(430, 363);
  135.             this.Controls.Add(this.button1);
  136.             this.Controls.Add(this.groupBox1);
  137.             this.Controls.Add(this.textBox3);
  138.             this.Controls.Add(this.textBox2);
  139.             this.Controls.Add(this.textBox1);
  140.             this.Name = "Form1";
  141.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  142.             this.Text = "Form1";
  143.             this.groupBox1.ResumeLayout(false);
  144.             this.groupBox1.PerformLayout();
  145.             this.ResumeLayout(false);
  146.             this.PerformLayout();

  147.         }

  148.         #endregion

  149.         private System.Windows.Forms.TextBox textBox1;
  150.         private System.Windows.Forms.TextBox textBox2;
  151.         private System.Windows.Forms.TextBox textBox3;
  152.         private System.Windows.Forms.GroupBox groupBox1;
  153.         private System.Windows.Forms.RadioButton radioButton4;
  154.         private System.Windows.Forms.RadioButton radioButton3;
  155.         private System.Windows.Forms.RadioButton radioButton2;
  156.         private System.Windows.Forms.RadioButton radioButton1;
  157.         private System.Windows.Forms.Button button1;
  158.     }
  159. }
複製代碼
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.Windows.Forms;

  9. namespace WindowsFormsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         delegate double Cal(int a, int b);
  14.         Cal cal = null;
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             this.radioButton1.CheckedChanged+=new EventHandler(radioButton_CheckedChanged);
  19.             this.radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
  20.             this.radioButton3.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
  21.             this.radioButton4.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
  22.             cal = new Cal(ADD);
  23.         }
  24.         private void radioButton_CheckedChanged(object sender, EventArgs e)
  25.         {
  26.             RadioButton rdo = sender as RadioButton;
  27.             if (rdo != null)
  28.             {
  29.                 switch (rdo.Tag.ToString())
  30.                 {
  31.                     case "ADD":
  32.                         cal = new Cal(ADD);
  33.                         break;
  34.                     case "SUB":
  35.                         cal = new Cal(SUB);
  36.                         break;
  37.                     case "MUL":
  38.                         cal = new Cal(MUL);
  39.                         break;
  40.                     case "DIV":
  41.                         cal = new Cal(DIV);
  42.                         break;
  43.                     default:
  44.                         cal = null;
  45.                         break;
  46.                 }
  47.             }
  48.         }
  49.        static double ADD(int a, int b)
  50.         {
  51.             return a + b;
  52.         }
  53.         static double SUB(int a, int b)
  54.         {
  55.             return a-b;
  56.         }
  57.         static double MUL(int a, int b)
  58.         {
  59.             return a * b;
  60.         }
  61.         static double DIV(int a, int b)
  62.         {
  63.             return a / b;
  64.         }

  65.         private void button1_Click(object sender, EventArgs e)
  66.         {
  67.             int a = int.Parse(textBox1.Text);
  68.             int b = int.Parse(textBox2.Text);
  69.             textBox3.Text = cal.Invoke(a, b).ToString();
  70.         }
  71.     }
  72. }
複製代碼

作者: 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 編輯

供您參考,這樣或許更清楚明瞭.........?
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;

  4. namespace HW_11271158
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         private delegate double Cal(int a, int b);

  9.         private Cal _cal = new Cal(ADD);

  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.             addRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
  14.             subRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
  15.             mulRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
  16.             divRadioButton.CheckedChanged += new EventHandler(operation_CheckedChanged);
  17.         }

  18.         private void operation_CheckedChanged(object sender, EventArgs e)
  19.         {
  20.             var currentRadioButton = sender as RadioButton;
  21.             var operation = new Dictionary<string, Cal> {
  22.                { "ADD", ADD },
  23.                { "SUB", SUB },
  24.                { "MUL", MUL },
  25.                { "DIV", DIV },
  26.             };
  27.             _cal = operation[currentRadioButton.Tag.ToString()];
  28.         }

  29.         private static double ADD(int a, int b)
  30.         {
  31.             return a + b;
  32.         }

  33.         private static double SUB(int a, int b)
  34.         {
  35.             return a - b;
  36.         }

  37.         private static double MUL(int a, int b)
  38.         {
  39.             return a * b;
  40.         }

  41.         private static double DIV(int a, int b)
  42.         {
  43.             return a / b;
  44.         }

  45.         private void button1_Click(object sender, EventArgs e)
  46.         {
  47.             var a = int.Parse(textBox1.Text);
  48.             var b = int.Parse(textBox2.Text);
  49.             textBox3.Text = _cal(a, b).ToString();
  50.         }
  51.     }
  52. }
複製代碼



作者: 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!