如何实现鼠标按下和单击按钮的事件?
问题描述:
使用Windows应用程序
我正在制作将在触摸屏上使用的表格
我想同时使用两个事件(单击和鼠标按下)作为按钮
不能正常工作的代码就是这样:
Using windows application
I''m working on form which will be used on touch screen
I want to use both events (click and mouse down) for button
the code that doesn''t work simply like that :
private void button1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(this, DragDropEffects.All);
}
private void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
MessageBox.Show("Drag and Drop Comleted");
}
// this event never never fired
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Mouse Click Event Fired ");
}
我还尝试了另一种方式的代码,如下所示:
I tried also the code in another ways as follows :
public Form1()
{
InitializeComponent();
button1.MouseDown += new MouseEventHandler(button1_MouseDown);
panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Mouse Click Event Fired ");
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
MessageBox.Show("Drag and Drop Comleted");
}
void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
void button1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(this, DragDropEffects.All);
}
而且这种方式也不起作用
and this way also doesn''t work
public Form1()
{
InitializeComponent();
button1.MouseDown += new MouseEventHandler(button1_MouseDown);
panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Mouse Click Event Fired ");
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
MessageBox.Show("Drag and Drop Comleted");
}
void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
void button1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(this, DragDropEffects.All);
}
答
实际上,MouseDown
和Click
事件不会相互干扰.您可能会注意到,单击事件是在MouseUp
事件之后调用的,因此MouseDown
和Click
是完全隔离的.您可以按常规方式处理它们.
只是为了您的理解:button1_Click
或button1_MouseDown
不是事件,但是它们可以用作事件处理程序.即使名称具有暗示性(并违反了良好的Microsoft命名约定,因此请始终将自动生成的名称重命名为更具语义的名称并遵循命名建议),实际上您没有提供证据证明它们曾经被添加到某些事件的调用列表中,仅使用"+ ="运算符即可完成操作(可能可以在自动生成的代码中找到它).我建议您在没有Designer的情况下进行类似的操作(这样会使维护工作变得更糟): myButton.Click + =(sender,eventArgs)=>
{MessageBox.Show(不是很清楚在何处添加处理程序"); }
此外,在上面显示的代码块"{/*...*/}
"中,您可以调用任何方法,这些方法不会强制您使用sender
或eventArgs
参数,因为它们中的某些或两个经常不使用. >—SA
Actually,MouseDown
andClick
events do not interfere. As you could notice, the click event is invoked after theMouseUp
event, soMouseDown
andClick
are totally isolated. You handle them in a usual way.
Just for your understanding: yourbutton1_Click
orbutton1_MouseDown
are not events, but they can be used as event handlers. Even the names are suggestive (and violate good Microsoft naming conventions, so always rename auto-generated names to something more semantic and following the naming recommendations), you actually provide no evidence that they are used to be added to the invocation list of some events, which is only done using the "+=" operator (probably you can find it in the auto-generated code). I recommend doing something like that, without the Designer (with makes maintenance much worse):
myButton.Click += (sender, eventArgs) =>
{ MessageBox.Show("Not it''s clear where the handler is added"); }
Besides, in the code block "{/*...*/}
" shown above, you can call any method, which does not force you to usesender
oreventArgs
parameters, because some of them or both are often not used.—SA