查看完整版本: C# 用委派寫 介面(+-*/)
頁: [1]

asdfg0205 發表於 2017-3-8 01:46 PM

C# 用委派寫 介面(+-*/)

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

Josie_2016 發表於 2017-3-9 06:04 PM

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

Form1.Designer.csnamespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

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(); 也會正常阿.
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

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

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.

delegate is good for flexibility, however they are normally slow in performance....<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><br><br><br><br><br><div></div>

跨越青春嶺 發表於 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;
        }

        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();
        }
    }
}

...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

vincent-vincent 發表於 2017-3-28 10:07 AM

CheckedChanged這個事件,包含選中和取消都會觸發,
所以你本來選中1改成2,那radio1_checkedChanged和radio2_checkedChanged兩個都會觸發到,
這裡應該會造成問題。
要加上if (radioButton1.Checked) 的判斷
頁: [1]