?
Solved

WPF with C# Button LeftButtonDown/MouseDown event handler

Posted on 2009-04-03
6
Medium Priority
?
10,412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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

Technology Partners: 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

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

770 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