Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 学习代理事件的简单快速示例_C#_Winforms_Events_Delegates - Fatal编程技术网

C# 学习代理事件的简单快速示例

C# 学习代理事件的简单快速示例,c#,winforms,events,delegates,C#,Winforms,Events,Delegates,我是这个话题的新手。我创建了一个c#win表单。在这个表单中,我有两个文本框和一个标签。我要做的是创建一个委托事件来跟踪文本框的更改,并将textbox1和textbox2中的两个数字相加。标签将自动显示结果。希望有人能给我举个例子,非常感谢!我现在有些东西 events.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Project3 {

我是这个话题的新手。我创建了一个c#win表单。在这个表单中,我有两个文本框和一个标签。我要做的是创建一个委托事件来跟踪文本框的更改,并将
textbox1
textbox2
中的两个数字相加。标签将自动显示结果。希望有人能给我举个例子,非常感谢!我现在有些东西

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;
        }
    }
}
表格1.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)
        {

        }
    }
}

如果您只想了解如何将结果委派到标签中,并了解
委派
事件
,以下是一个示例,您可能希望尝试并分析该示例以供学习:

示例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:

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);
    }
}
表格1.cs

Event.cs


注:以上示例绝不是计算两个值的好方法,这只是一个示例。这种方法的缺点会导致您在某种程度上编写出“意大利面条”代码,并来回返回逻辑所在的位置。

有一个简单的解决方案

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

        }

您不需要创建自己的委托或定义新事件。只需在
TextChanged
处理程序中编写代码。@AVD我知道,但我尽量不使用给定的处理程序。谢谢!这是一个很好的例子!但你们能把事件类和表单类分开吗?我想要两个类。你可以(参见编辑的示例)。但同样,这只是为了学习,有更好的方法来处理
TextChanged
事件中的计算。请添加一些文本,解释您正在做什么以及为什么要这样做
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);
            }

        }