恢复对象c#中包含的多个值

问题描述 投票:0回答:1

我在按钮上开始活动:

private void button1_Click(object sender, EventArgs e)
{
 _info.Event(value,data); // _info declared above for call from other class
 _info.Event(value2,data2);
}

我的表单:

public Form1()
{
 _info = new Info();
 _info.NewData += CustomEvent;
}

并显示:

private void CustomEvent(object sender, MyEvent e)
{
 textBox1.Text = (e.data).ToString();
 textBox2.Text = (e.data).ToString(); //only this value show in both textboxs
}

我的问题是:我尝试使用事件来恢复多个值,但是我不知道如何在对象中分离值以在不同的文本框中显示它们?

c# winforms events display data-recovery
1个回答
0
投票

这是有关如何使用delegates的示例:当然,您可以更改委托中的参数。我选择了“字符串数据”和“成功布尔”,但是您可以更改它们。


public class Foo
{
    public delegate void MyDataDelegate(string data, bool success); //create a delegate (=custom return/param type for event)
    public static event MyDataDelegate OnMyEvent; //create event from the type of the early created delegate

    private void myMethod()
    {
        //do something 
        bool success = true;
        OnMyEvent?.Invoke("some data", success); //invoke event (use "?" to check if it is null). Pass the params
    }
}

public class MyClass
{
    public MyClass()
    {
        Foo.OnMyEvent += OnMyEventtriggered; //subscribe to the event in the costructor of your class
    }

    private void OnMyEventtriggered(string data, bool success)
    {
        //do something
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.