?
Solved

WPF with C# Button LeftButtonDown/MouseDown event handler

Posted on 2009-04-03
6
Medium Priority
?
10,554 Views
Last Modified: 2013-11-12
Hi, I'm having trouble capturing LeftButtonDown and MouseDown events for the button in Wpf.  Right button events are captured fine, but left seem to be ignored.  Is this a WPF bug or am I missing something really simple?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
 
        private void button1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Left down");
        }
 
        private void button1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Right down");
        }
    }
}
 
And the WPF XAML...
 
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" MouseLeftButtonDown="button1_MouseLeftButtonDown" MouseRightButtonDown="button1_MouseRightButtonDown">Button</Button>
    </Grid>
</Window>

Open in new window

0
Comment
Question by:SINCO
6 Comments
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24059176
Have you registered the event handler ?

in the designer partial class check that you passed the event handler to the correct delegate.
0
 
LVL 16

Assisted Solution

by:CuteBug
CuteBug earned 150 total points
ID: 24059189
0
 
LVL 8

Accepted Solution

by:
unmeshdave earned 600 total points
ID: 24059249
some controls handles events and transforms them to another event so ultimate element will not get that event as it is already handled by control class itself.
here, for button class, the MouseLeftButtonDown is handled in Button class and transforms it into click event ultimately, so this event will not be passed to element.
exception is that, you can get 'handled event' at element if you are registered to get handled events. you can not do it in markup, you have to do it in code behind.
In code behind, add handler after initializecomponent() call as follows

button1.AddHandler(Control.MouseLeftButtonDownEvent, new MouseButtonEventHandler(button1_MouseLeftButtonDown), true);

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:unmeshdave
ID: 24059257
note the 3rd parameter is 'true'.
0
 

Author Comment

by:SINCO
ID: 24059384
Thanks for the help, that makes a lot of sense now, although to me it seems a bit strange that a mousedown event should be supressed when no handler for a click event is subscribed for the button
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24059879
well, weather u register or not but control will transfer the leftbuttondown event for mouse to click. if you register for click event, u will get event otherwise won't. It should be in that way only.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question