Solved

WPF with C# Button LeftButtonDown/MouseDown event handler

Posted on 2009-04-03
6
10,177 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax error 9 45
Two different visual studio versions 3 22
Replace &lt; with < 14 55
SSL https .net web site force redirect 3 34
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

911 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

24 Experts available now in Live!

Get 1:1 Help Now