学习委托事件的简单快速示例

时间:2015-08-11 03:46:30

标签: c# winforms events delegates

我是这个主题的新手。我创建了一个 c#win form 。在此表单中,我有两个文本框一个标签。我想要做的是创建一个委托事件来跟踪文本框的更改,并添加来自textbox1textbox2的两个数字。标签将自动显示结果。希望有人可以为我提供一个例子,非常感谢你!我现在有一些东西,

events.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Project3
{
    public delegate void Calculate(int obj1, int obj2);
    public class events
    {
        int result;
        public int Add(int x, int y)
        {
            result = x + y;
            return result;
        }
    }
}

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 Project3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text ="";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果您只是想了解如何委托将结果添加到标签中,并了解delegateevent,以下是您可能想要尝试的示例分析用于学习目的:

示例1:

public delegate int CalculateEventHandler(object obj1, object obj2);
public partial class Form1 : Form
{
    public event CalculateEventHandler Calculate;
    private string OnCalculate(string text1, string text2)
    {
        string result = "0";
        if (this.Calculate != null)
        {
            result = this.Calculate(this.textBox1.Text, this.textBox2.Text).ToString();
        }
        return result;
    }

    public Form1()
    {
        this.InitializeComponent();
        this.InitializeEvent();
    }

    private void InitializeEvent()
    {
        this.Calculate += Form1_Calculate;
    }

    private int Form1_Calculate(object obj1, object obj2)
    {
        int a = 0;
        int b = 0;
        int.TryParse(obj1.ToString(), out a);
        int.TryParse(obj2.ToString(), out b);
        return a + b;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.label1.Text = OnCalculate(this.textBox1.Text, this.textBox2.Text);
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        this.label1.Text = OnCalculate(this.textBox1.Text, this.textBox2.Text);
    }
}

示例2:

Form1.cs的

public partial class Form1 : Form
{
    public Form1()
    {
        this.InitializeComponent();
        this.InitializeEvent();
    }

    private void InitializeEvent()
    {
        Event.Calculate += Event_Calculate;
    }

    private int Event_Calculate(object obj1, object obj2)
    {
        int x = 0;
        int y = 0;
        int.TryParse(obj1.ToString(), out x);
        int.TryParse(obj2.ToString(), out y);
        return Event.Add( x, y );
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.label1.Text = Event.OnCalculate(this.textBox1.Text, this.textBox2.Text).ToString();
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        this.label1.Text = Event.OnCalculate(this.textBox1.Text, this.textBox2.Text).ToString();
    }
}

Event.cs

public delegate int CalculateEventHandler(object obj1, object obj2);
public class Event
{
    static public event CalculateEventHandler Calculate;
    static public int Add(int x, int y)
    {
        int result = x + y;
        return result;
    }

    static public int OnCalculate( object obj1, object obj2 )
    {
        int result = 0;
        if( Calculate != null )
        {
            result = Calculate(obj1, obj2);
        }
        return result;
    }
}

注意:以上示例绝不是计算两个值的好方法,这只是一个例子。这种方法的缺点会导致你以某种方式 spaghetti 代码,来回到逻辑的去向。

答案 1 :(得分:0)

有一个简单的解决方案

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text == string.Empty)
                {
                    //textBox2.Text = (0).ToString();
                    label1.Text = ( Convert.ToInt32(textBox1.Text)).ToString();

                }
                else if (textBox1.Text == string.Empty)
                {
                    label1.Text = (Convert.ToInt32(textBox2.Text)).ToString();

                }
                else
                {
                    label1.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();

                }
            }
            catch (Exception e3)
            {
                MessageBox.Show(e3.Message);
            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text == string.Empty)
                {
                    //textBox2.Text = (0).ToString();
                    label1.Text = (Convert.ToInt32(textBox1.Text)).ToString();

                }
                else if (textBox1.Text == string.Empty)
                {
                    label1.Text = (Convert.ToInt32(textBox2.Text)).ToString();

                }
                else
                {
                    label1.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();

                }
            }
            catch (Exception e3)
            {
                MessageBox.Show(e3.Message);
            }

        }