Solved

WPF with C# Button LeftButtonDown/MouseDown event handler

Posted on 2009-04-03
6
10,238 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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