C# 如何在lambda表达式中伪装鼠标按钮
下面的代码负责创建一个5乘5的按钮网格。多亏了人们的帮助,我可以插入lambda expretion,将“按下网格中的哪个按钮”的信息传递给其他函数。问题是,我需要它来区分鼠标左键还是右键被按下,我不知道该怎么做。任何指向其他帖子/文章的提示或链接都将非常有用C# 如何在lambda表达式中伪装鼠标按钮,c#,winforms,lambda,mouse,mouse-buttons,C#,Winforms,Lambda,Mouse,Mouse Buttons,下面的代码负责创建一个5乘5的按钮网格。多亏了人们的帮助,我可以插入lambda expretion,将“按下网格中的哪个按钮”的信息传递给其他函数。问题是,我需要它来区分鼠标左键还是右键被按下,我不知道该怎么做。任何指向其他帖子/文章的提示或链接都将非常有用 Button[] grid5x5 = new Button[25]; void Spawn5x5Grid() { // Position of the first button i
Button[] grid5x5 = new Button[25];
void Spawn5x5Grid()
{
// Position of the first button
int x = 50, y = 150;
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
grid5x5[count] = new Button
{
Size = new Size(31,31),
Location = new Point(x, y)
};
this.Controls.Add(grid5x5[count]);
grid5x5[count].Click += (o, ee) =>
{
Button button = o as Button;
// index variable contains the position of the button in
// array
int index = Array.IndexOf(grid5x5, button);
};
count++;
x = x + 31;
}
x = 50;
y = y + 31;
}
}
按钮[]grid5x5=新按钮[25];
void Spawn5x5Grid()
{
//第一个按钮的位置
int x=50,y=150;
整数计数=0;
对于(int i=0;i<5;i++)
{
对于(int j=0;j<5;j++)
{
grid5x5[计数]=新建按钮
{
尺寸=新尺寸(31,31),
位置=新点(x,y)
};
this.Controls.Add(grid5x5[count]);
grid5x5[计数]。单击+=(o,ee)=>
{
按钮按钮=o as按钮;
//索引变量包含按钮在中的位置
//排列
int index=Array.IndexOf(grid5x5,按钮);
};
计数++;
x=x+31;
}
x=50;
y=y+31;
}
}
您真的应该包括这是什么类型的UI,web、WPF、winforms?信息要么在事件参数ee
中,要么右键单击甚至可能不会触发事件。@juharr它是winform.NETframework@juharr你说得对。此时,它只在鼠标左键上触发。如中所述,如果您需要知道按下了哪个按钮,请使用@KennethK。我认为这解决了问题。谢谢