找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
發表文章前請先閱讀相關版規尊貴會員無限下載附件搞笑、娛樂、精彩的影片讓你看
神奇寶貝julia人妖世紀帝國高中國中
whatsup北投portal k姫と穢欲寢取られhouse pa亞人

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

[繁]迷宮飯11-

[繁]肌肉魔法使-MASHL

✡ 斗破蒼穹 年番/鬥

[繁]香格里拉・開拓異

老媽沒收兒子的手機

[繁]我獨自升級08-
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 4737|回復: 6

[求助] C# 用委派寫 介面(+-*/)[複製鏈接]

Rank: 1

帖子
269
積分
188 點
潛水值
22260 米
發表於 2017-3-8 01:46 PM|顯示全部樓層
若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。
不好意思 請問一下 我是哪邊有問題了?
因為他都沒有跑錯誤訊息 ! 然後按開始 有介面沒錯 可是 變成 要顯示結果的 第3格 變成我能打字進去
按了運算並無用! 這是哪邊出錯ㄌ!?
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊
如果發覺自己無法使用一些功能或出現問題,請按重新整理一次,並待所有網頁內容完全載入後5秒才進行操作。

使用道具檢舉

Rank: 1

帖子
61
積分
49 點
潛水值
26590 米
發表於 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. }
複製代碼
...
瀏覽完整內容,請先 註冊登入會員

點評

asdfg0205 好的 謝謝你!  發表於 2017-3-9 10:00 PM
如果發覺自己無法使用一些功能或出現問題,請按重新整理一次,並待所有網頁內容完全載入後5秒才進行操作。

使用道具檢舉

Rank: 2Rank: 2

帖子
1051
積分
619 點
潛水值
27270 米
發表於 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(); 也會正常阿....
瀏覽完整內容,請先 註冊登入會員

使用道具檢舉

Rank: 1

帖子
61
積分
49 點
潛水值
26590 米
發表於 2017-3-10 03:33 PM|顯示全部樓層
To:cockroachrun

基本上沒甚麼不同,請參考下面網址
Delegate() vs. Delegate.Invoke()
下載: 訪客無法瀏覽下載點,請先 註冊登入會員

使用道具檢舉

bwong42!@# 該用戶已被刪除
發表於 2017-3-11 08:14 PM|顯示全部樓層
若瀏覽伊莉的時侯發生問題或不正常情況,請使用Internet Explorer(I.E)。
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs.
...
瀏覽完整內容,請先 註冊登入會員





成為伊莉的版主,你將獲得更高級和無限的權限。把你感興趣的版面一步步地發展和豐盛,那種滿足感等著你來嚐嚐喔。

使用道具檢舉

Rank: 1

帖子
76
積分
193 點
潛水值
4402 米
發表於 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. }
複製代碼


...
瀏覽完整內容,請先 註冊登入會員
回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。

使用道具檢舉

Rank: 1

帖子
58
積分
163 點
潛水值
7017 米
發表於 2017-3-28 10:07 AM|顯示全部樓層
如果瀏覽伊莉時速度太慢或無法連接,可以使用其他分流瀏覽伊莉,www01.eyny.com(02,03)。
CheckedChanged這個事件,包含選中和取消都會觸發,
所以你本來選中1改成2,那radio1_checkedChanged和radio2_checkedChanged兩個都會觸發到,
這裡應該會造成問題。
要加上if (radioButton1.Checked) 的判斷

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部