如何实现鼠标按下和单击按钮的事件? [英] how to implement both mouse down and click events for button ?

查看:118
本文介绍了如何实现鼠标按下和单击按钮的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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);
     }

推荐答案

实际上,MouseDownClick事件不会相互干扰.您可能会注意到,单击事件是在MouseUp事件之后调用的,因此MouseDownClick是完全隔离的.您可以按常规方式处理它们.

只是为了您的理解:button1_Clickbutton1_MouseDown不是事件,但是它们可以用作事件处理程序.即使名称具有暗示性(并违反了良好的Microsoft命名约定,因此请始终将自动生成的名称重命名为更具语义的名称并遵循命名建议),实际上您没有提供证据证明它们曾经被添加到某些事件的调用列表中,仅使用"+ ="运算符即可完成操作(可能可以在自动生成的代码中找到它).我建议您在没有Designer的情况下进行类似的操作(这样会使维护工作变得更糟): myButton.Click + =(sender,eventArgs)= > {MessageBox.Show(不是很清楚在何处添加处理程序"); }
此外,在上面显示的代码块"{/*...*/}"中,您可以调用任何方法,这些方法不会强制您使用sendereventArgs参数,因为它们中的某些或两个经常不使用. >
—SA
Actually, MouseDown and Click events do not interfere. As you could notice, the click event is invoked after the MouseUp event, so MouseDown and Click are totally isolated. You handle them in a usual way.

Just for your understanding: your button1_Click or button1_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 use sender or eventArgs parameters, because some of them or both are often not used.

—SA


这篇关于如何实现鼠标按下和单击按钮的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆