Solved

WPF with C# Button LeftButtonDown/MouseDown event handler

Posted on 2009-04-03
6
10,129 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 50 total points
ID: 24059189
0
 
LVL 8

Accepted Solution

by:
unmeshdave earned 200 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now