恢复对象中包含的几个值 c#
Recover several values contain in an object c#
我在我的按钮中开始活动:
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
}
我的问题是:我尝试使用我的事件来恢复多个值,但我不知道如何分隔对象中的值以便在不同的文本框中显示它们?
这是一个关于如何使用 delegates:
的例子
当然,您可以更改委托中的参数。我选择了"string data"和"bool success",但是你可以改变它们。
public class Foo
{
public delegate void MyDataDelegate(string data, bool success); //create a delegate (=custom return/param type for event)
public 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
{
Foo myFoo;
public MyClass()
{
myFoo = new Foo();
myFoo.OnMyEvent += OnMyEventtriggered; //subscribe to the event in the costructor of your class
}
private void OnMyEventtriggered(string data, bool success)
{
//do something
}
}
我在我的按钮中开始活动:
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
}
我的问题是:我尝试使用我的事件来恢复多个值,但我不知道如何分隔对象中的值以便在不同的文本框中显示它们?
这是一个关于如何使用 delegates:
的例子
当然,您可以更改委托中的参数。我选择了"string data"和"bool success",但是你可以改变它们。
public class Foo
{
public delegate void MyDataDelegate(string data, bool success); //create a delegate (=custom return/param type for event)
public 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
{
Foo myFoo;
public MyClass()
{
myFoo = new Foo();
myFoo.OnMyEvent += OnMyEventtriggered; //subscribe to the event in the costructor of your class
}
private void OnMyEventtriggered(string data, bool success)
{
//do something
}
}