简单的自定义事件 [英] simple custom event

查看:87
本文介绍了简单的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习自定义事件,我试图创建一个,但好像我有一个问题

I'm trying to learn custom events and I have tried to create one but seems like I have a problem

我已经创建了一个表格,静态类和自定义事件。我正在努力实现的是当我preSS按钮形式将调用类的静态函数,然后将FUNC不时上升事件报告当前状态。如果事件引发Form1中会听,如果是,它将改变LABEL1的文本

I have created a Form, static class and custom event. What I'm trying to achieve is when I press button Form will call static class function and then func will from time to time rise an event to report current status. Form1 will listen if the event is raised and if it is, it will change Text of label1

下面是我迄今为止

public partial class Form1 : Form
{
    public EventHandler<Progress> progress; 

    public Form1()
    {
        InitializeComponent();
        progress += SetStatus;
    }

    private void SetStatus(object sender, Progress e)
    {
        label1.Text = e.Status;
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
         TestClass.Func();
    }

 }

文件2

class TestClass
{
    public static void Func()
    {
        //time consuming code
        Report status 
        // time consuming code
        report status
    }
}

public class Progress : EventArgs
{
    public string Status { get; private set; }

    private Progress() {}

    public Progress(string status)
    {
        Status = status;
    }
}

现在我不明白的是,我怎么能起到从TestClass中的事件,这样Form1中可以处理事件和变更label.Text

Now what I don't understand is, how can I rise an event from TestClass so Form1 can handle event and change label.Text

推荐答案

这是创建自定义事件,并提高他们的简单方法。您创建一个委托并在您从投掷类的事件。然后从code的另一部分订阅事件。你已经有了一个自定义事件参数类,因此您可以以此为基础进行其他事件参数类。 N.B:我没有这个选项编译code

This is an easy way to create custom events and raise them. You create a delegate and an event in the class you are throwing from. Then subscribe to the event from another part of your code. You have already got a custom event argument class so you can build on that to make other event argument classes. N.B: I have not compiled this code.

public partial class Form1 : Form
{
    private TestClass _testClass;
    public Form1()
    {
        InitializeComponent();
        _testClass = new TestClass();
        _testClass.OnUpdateStatus += new TestClass.StatusUpdateHandler(UpdateStatus);
    }

    private void UpdateStatus(object sender, ProgressEventArgs e)
    {
        SetStatus(e.Status);
    }

    private void SetStatus(string status)
    {
        label1.Text = status;
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
         TestClass.Func();
    }

}

public class TestClass
{
    public delegate void StatusUpdateHandler(object sender, ProgressEventArgs e);
    public event StatusUpdateHandler OnUpdateStatus;

    public static void Func()
    {
        //time consuming code
        UpdateStatus(status);
        // time consuming code
        UpdateStatus(status);
    }

    private void UpdateStatus(string status)
    {
        // Make sure someone is listening to event
        if (OnUpdateStatus == null) return;

        ProgressEventArgs args = new ProgressEventArgs(status);
        OnUpdateStatus(this, args);
    }
}

public class ProgressEventArgs : EventArgs
{
    public string Status { get; private set; }

    public ProgressEventArgs(string status)
    {
        Status = status;
    }
}

这篇关于简单的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆