0

In my code below, I am trying to simulate the Click event for my button Browse.

private void TextBox_MouseDown(object sender, RoutedEventArgs e)
{
    this.Browse.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
}

This is the error that I am getting:

'System.Windows.Forms.ButtonBase' does not contain a definition for 'ClickEvent'

How can I raise the click event?

It appears there are some who are questioning the purpose of this code. I got it here https://stackoverflow.com/a/733974/1477388 and am simply trying to make it work.

Community
  • 1
  • 1
user1477388
  • 20,790
  • 32
  • 144
  • 264

3 Answers3

5

You're working with a WPF application

this.Browse.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));

(routed events are part of WPF, not winforms), however you've referenced the wrong namespace

'System.Windows.Forms.ButtonBase' does not contain a definition for 'ClickEvent'

Go to the top of your class and remove the following

using System.Windows.Forms;

and replace it with

using System.Windows.Controls.Primitives

Blam.

  • That seems to fix it; however, now I can't use `OpenFileDialog` – user1477388 Aug 13 '13 at 18:46
  • 2
    @user1477388 Use `Microsoft.Win32.OpenFileDialog`. Forget winforms, it's a really old technology no one cares about anymore. – Federico Berasategui Aug 13 '13 at 18:47
  • 1
    @HighCore: Your original version is more correct. Winforms compared to wpf is crap. –  Aug 13 '13 at 18:49
  • Okay, okay, please don't kill my inbox with a winforms/wpf debate. Besides, WPF wins. So there's no point. –  Aug 13 '13 at 18:51
  • @Will: Obviously WPF is better, just pointed out that WinForms is still quite relevant in some regards. – H.B. Aug 13 '13 at 18:53
2
  1. Clicks should not fire on MouseDown.
  2. Put the TextBlock in a Button or use a Hyperlink.
  3. That code looks fine otherwise, however you have a using for the wrong namespace (WinForms) instead of WPF. You want: System.Windows.Controls.Primitives
Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
0
  1. Put the code that you want to be called at the Click event into a method
  2. Call this method at the Click event
  3. Call this method at the MouseDown event
meilke
  • 3,280
  • 1
  • 15
  • 31
  • Actually, this solves the problem since I simply called Button_Click directly from my XAML code instead of calling another method to call it from the .cs code. I am still wondering why it won't find that event though. – user1477388 Aug 13 '13 at 18:44
  • 1
    I did that for the longest time, too. Unfortunately, you end up using null parameters etc. which is always ugly. Use a dedicated method that only takes parameters that are actually used. Easier on the eye. – meilke Aug 13 '13 at 18:52