Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Events Forms event Desktop , +
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);
        }
Posted 1-Oct-12 8:27am
Edited 1-Oct-12 10:00am
v4
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 1-Oct-12 15:01pm
   
sorry for that
Sergey Alexandrovich Kryukov at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100