函数上的自定义 EventArgs 消息框不显示来自发件人 class 的 属性
Custom EventArgs Messagebox on Function does not show property from sender class
我有一个 class 有两个属性:weight 和 Id。我想在设置称重时触发一个事件。要触发的事件是一个显示 Id 属性 的消息框。显示了消息框,但它不包含 ID 属性。
完整代码如下:
https://pastebin.com/zpHn48gL
public class MyClass
{
//Custom type Event declaration
public event EventHandler<Mas4TEventArgs> Info;
decimal _weigh;
//properties
public string Id { get; set; }
public decimal Weigh
{
get { return this._weigh; }
set //When this property is changed, invoke Info Event, passing Id property to be shown on messagebox.
{
this._weigh= value;
Info?.Invoke(this, new Mas4TEventArgs(this.Id));
}
}
}
public class Mas4TEventArgs : EventArgs
{
//constructor
public Mas4TEventArgs(string pId) { IdArgu = pId; }
//property IdArgu
public string IdArgu { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyClass C = new MyClass();
//suscription
C.Info += C_Info;
//Function to be triggered by the event
void C_Info(object sendr, Mas4TEventArgs ev)
{
try
{ //ev.IdArgu doesn't show on the messagebox.
MessageBox.Show("Evento consumido. " + " Id: " + ev.IdArgu);
}
catch (Exception) { }
}
//properties
C.Weigh = Convert.ToDecimal(textBox1.Text);
C.Id = TxtId.Text;
//just to check the two properties have been parsed correctly.This works as intended.
MessageBox.Show("Ingresado: Peso: " + C.Peso.ToString() + " Id: " + C.Id);
}
}
或者,如果您喜欢这样:
https://lpaste.net/3710707699130826752
您的代码包含以下关键行...
C.Weigh = Convert.ToDecimal(textBox1.Text);
C.Id = TxtId.Text;
请注意 Id
在 Weigh
之后设置。因此,在设置 Weigh
值时,它将引发一个事件,其中包含 Id
中的任何内容,然后再在下一行设置 Id
。
交换两行,以便首先设置 Id
,您的代码将按预期运行。
我有一个 class 有两个属性:weight 和 Id。我想在设置称重时触发一个事件。要触发的事件是一个显示 Id 属性 的消息框。显示了消息框,但它不包含 ID 属性。
完整代码如下: https://pastebin.com/zpHn48gL
public class MyClass
{
//Custom type Event declaration
public event EventHandler<Mas4TEventArgs> Info;
decimal _weigh;
//properties
public string Id { get; set; }
public decimal Weigh
{
get { return this._weigh; }
set //When this property is changed, invoke Info Event, passing Id property to be shown on messagebox.
{
this._weigh= value;
Info?.Invoke(this, new Mas4TEventArgs(this.Id));
}
}
}
public class Mas4TEventArgs : EventArgs
{
//constructor
public Mas4TEventArgs(string pId) { IdArgu = pId; }
//property IdArgu
public string IdArgu { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyClass C = new MyClass();
//suscription
C.Info += C_Info;
//Function to be triggered by the event
void C_Info(object sendr, Mas4TEventArgs ev)
{
try
{ //ev.IdArgu doesn't show on the messagebox.
MessageBox.Show("Evento consumido. " + " Id: " + ev.IdArgu);
}
catch (Exception) { }
}
//properties
C.Weigh = Convert.ToDecimal(textBox1.Text);
C.Id = TxtId.Text;
//just to check the two properties have been parsed correctly.This works as intended.
MessageBox.Show("Ingresado: Peso: " + C.Peso.ToString() + " Id: " + C.Id);
}
}
或者,如果您喜欢这样: https://lpaste.net/3710707699130826752
您的代码包含以下关键行...
C.Weigh = Convert.ToDecimal(textBox1.Text);
C.Id = TxtId.Text;
请注意 Id
在 Weigh
之后设置。因此,在设置 Weigh
值时,它将引发一个事件,其中包含 Id
中的任何内容,然后再在下一行设置 Id
。
交换两行,以便首先设置 Id
,您的代码将按预期运行。