Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
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 :

C#
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 :
C#
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

C#
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);
     }
Posted
Updated 1-Oct-12 9:00am
v4
Comments
Sergey Alexandrovich Kryukov 1-Oct-12 13:30pm    
Even robots tag the UI libraries they use. It could be WPF, ASP.NET, Forms, Silverlight, Metro, something else...
--SA
Sergey Alexandrovich Kryukov 1-Oct-12 14:48pm    
Sorry, I had to remove your "this code does not work" post, to conform to the rules. You should better put all the code samples in the body of the question using "Improve question". If you tried to notify me.., you also would need to add a comment to any of my posts, better be this answer.

Besides, please understand that "code does not work" is not informative, unless you describe what you expected and what you observed. I'm not trying to be too picky, I, frankly, really failed to see what exactly "does not work"...

(And also, please don't re-post -- it also cannot help you. Try to explain all your concerns in this page, in comments or by modifying the question, as I explained above.)
--SA
EgyptianRobot 1-Oct-12 15:01pm    
sorry for that
Sergey Alexandrovich Kryukov 1-Oct-12 15:07pm    
No problem, as soon as you post in proper ways in future.
And of course, if you provide more clear feedback, I'll gladly try to add to my answer, to make it more useful and acceptable. I just need to know more on what went wrong.
--SA
EgyptianRobot 1-Oct-12 15:27pm    
okay, now I tried three different ways to implement the two events together and I get the same result that click event is not firing.
by the way I tried the three different ways in two different projects (very simple project) and got the same result.

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900