如何将更多参数传递给 Winform 控件事件?
How to pass more params to Winform control event?
我尝试将 3 个额外的 int 参数传递给 button1_Click 事件
private void button1_Click(object sender, EventArgs e, int Xaxis, int Yaxis, int Zaxis)
{
switch (Xaxis)
...
textBox1.Text = "Finished";
}
...
实现↓↓↓
...
public void Func1()
{
button1_Click(sender, e, 16, 5, 59) //Pass 16 5 59 to event button1_Click
//To perform a click event simulation with designated params
...
}
private void button2_Click(object sender, EventArgs e)
{
...
Func1();
}
但是Form1.Designer.cs
有点问题
this.button1.Click += new System.EventHandler(this.button1_Click);
//Compiler Error CS0123: No overload for 'method' matches delegate 'delegate'
如何正确地将 3 个额外的 int 参数传递给 button1_Click 事件?
我建议提取一个方法,让我们分开业务逻辑(axis
例程)和UI(按钮点击事件处理):
private void MyClick(Button button, int Xaxis, int Yaxis, int Zaxis) {
//TODO: All business logic here
switch (Xaxis)
...
textBox1.Text = "Finished";
}
那就用吧
// Just UI event handler
private void button1_Click(object sender, EventArgs e) {
//TODO: put right axis values here
MyClick(sender as Button, 0, 0, 0);
}
public void Func1()
{
MyClick(button1, 16, 5, 59); // Pass 16 5 59
//To perform a click event simulation with designated params
...
}
// Another UI event handler
private void button2_Click(object sender, EventArgs e)
{
...
Func1();
}
这是此答案的最通用版本,它以名为 AxisAction
的形式完整定义了一个事件,并由不同的按钮订阅以便用特定参数触发。数据本身包含在一个名为 Axis
的 struct
中,以便将 {X,Y,Z}
保持在一起。
// Your data model
public readonly struct Axis
{
public Axis(int x, int y, int z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public int X { get; }
public int Y { get; }
public int Z { get; }
}
// Your event arguments
public class MyEventArgs : EventArgs
{
public MyEventArgs(Axis axis)
{
Axis = axis;
}
public Axis Axis { get; }
}
// Your event method
public delegate void AxisEventHandler(object sender, MyEventArgs e);
public partial class Form1 : Form
{
// Your event
public event AxisEventHandler AxisAction;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Subscribe to event
AxisAction += Form1_AxisAction;
}
private void Form1_AxisAction(object sender, MyEventArgs e)
{
// Handle the event here
switch (e.Axis.X)
{
default:
break;
}
// done!
}
private void button1_Click(object sender, EventArgs e)
{
// Trigger your event from the mouse click
OnAxisAction(new MyEventArgs(new Axis(16, 5, 59)));
}
private void button2_Click(object sender, EventArgs e)
{
// Trigger your event from the mouse click
OnAxisAction(new MyEventArgs(new Axis(-6, 2, 8)));
}
private void OnAxisAction(MyEventArgs e)
{
// Call any event handlers on event trigger
AxisAction?.Invoke(this, e);
}
}
我尝试将 3 个额外的 int 参数传递给 button1_Click 事件
private void button1_Click(object sender, EventArgs e, int Xaxis, int Yaxis, int Zaxis)
{
switch (Xaxis)
...
textBox1.Text = "Finished";
}
...
实现↓↓↓
...
public void Func1()
{
button1_Click(sender, e, 16, 5, 59) //Pass 16 5 59 to event button1_Click
//To perform a click event simulation with designated params
...
}
private void button2_Click(object sender, EventArgs e)
{
...
Func1();
}
但是Form1.Designer.cs
有点问题this.button1.Click += new System.EventHandler(this.button1_Click);
//Compiler Error CS0123: No overload for 'method' matches delegate 'delegate'
如何正确地将 3 个额外的 int 参数传递给 button1_Click 事件?
我建议提取一个方法,让我们分开业务逻辑(axis
例程)和UI(按钮点击事件处理):
private void MyClick(Button button, int Xaxis, int Yaxis, int Zaxis) {
//TODO: All business logic here
switch (Xaxis)
...
textBox1.Text = "Finished";
}
那就用吧
// Just UI event handler
private void button1_Click(object sender, EventArgs e) {
//TODO: put right axis values here
MyClick(sender as Button, 0, 0, 0);
}
public void Func1()
{
MyClick(button1, 16, 5, 59); // Pass 16 5 59
//To perform a click event simulation with designated params
...
}
// Another UI event handler
private void button2_Click(object sender, EventArgs e)
{
...
Func1();
}
这是此答案的最通用版本,它以名为 AxisAction
的形式完整定义了一个事件,并由不同的按钮订阅以便用特定参数触发。数据本身包含在一个名为 Axis
的 struct
中,以便将 {X,Y,Z}
保持在一起。
// Your data model
public readonly struct Axis
{
public Axis(int x, int y, int z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public int X { get; }
public int Y { get; }
public int Z { get; }
}
// Your event arguments
public class MyEventArgs : EventArgs
{
public MyEventArgs(Axis axis)
{
Axis = axis;
}
public Axis Axis { get; }
}
// Your event method
public delegate void AxisEventHandler(object sender, MyEventArgs e);
public partial class Form1 : Form
{
// Your event
public event AxisEventHandler AxisAction;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Subscribe to event
AxisAction += Form1_AxisAction;
}
private void Form1_AxisAction(object sender, MyEventArgs e)
{
// Handle the event here
switch (e.Axis.X)
{
default:
break;
}
// done!
}
private void button1_Click(object sender, EventArgs e)
{
// Trigger your event from the mouse click
OnAxisAction(new MyEventArgs(new Axis(16, 5, 59)));
}
private void button2_Click(object sender, EventArgs e)
{
// Trigger your event from the mouse click
OnAxisAction(new MyEventArgs(new Axis(-6, 2, 8)));
}
private void OnAxisAction(MyEventArgs e)
{
// Call any event handlers on event trigger
AxisAction?.Invoke(this, e);
}
}