views:

3578

answers:

9

Is there any way to capture the MouseDown even from the .NET 2.0 TextBox control? I know the inherited Control class has the event, but it's not exposed in TextBox. Is there a way to override the event handler?

I also tried the OpenNETCF TextBox2 control which does have the MouseDown event exposed, but no matter what I do, it doesn't fire the handler.

Any suggestions?

A: 

What kind of crazy mobile device do you have that has a mouse? :)

Seriously, the reason there is no MouseDown event is because Windows Mobile doesn't have a mouse.

What are you trying to do in the MouseDown event handler? Maybe there is another way.

ageektrapped
A: 

What kind of crazy mobile device do you have that has a mouse? :)

Yes, windows mobile does not have an actual mouse, but you are mistaken that Windows Mobile .NET does not support the Mouse events. A click or move on the screen is still considered a "Mouse" event. It was done this way so that code could port over from full Windows easily. And this is not a Windows Mobile specific issue. The TextBox control on Windows does not have native mouse events either. I just happened to be using Windows Mobile in this case.

Edit: And on a side note...as Windows Mobile is built of the WindowsCE core which is often used for embedded desktop systems and Slim Terminal Services clients or "WinTerms" it has support for a hardware mouse and has for a long time. Most devices just don't have the ports to plug one in.

Adam Haile
A: 

According to the .Net Framwork, the MouseDown Event Handler on a TextBox is supported. What happens when you try to run the code?

GateKiller
A: 

Fair enough. You probably know more than I do about Windows Mobile. :) I just started programming for it. But in regular WinForms, you can override the OnXxx event handler methods all you want. A quick look in Reflector with the CF shows that Control, TextBoxBase and TextBox don't prevent you from overriding the OnMouseDown event handler.

Have you tried this?:

public class MyTextBox : TextBox
{
    public MyTextBox()
    {
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        //do something specific here
        base.OnMouseDown(e);
    }
}
ageektrapped
A: 

According to the .Net Framwork, the MouseDown Event Handler on a TextBox is supported. What happens when you try to run the code?

Actually, that's only there because it inherits it from "Control", as does every other Form control. It is however, overridden (and changed to private I believe) in the TextBox class. So it will not show up in IntelliSense in Visual Studio.

However, you actually can write the code:

textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);

and it will compile and run just fine, the only problem is that textBox1_MouseDown() will not be fired when you tap the TextBox control. I assume this is because of the Event being overridden internally. I don't even want to change what's happening on the event internally, I just want to add my own event handler to that event so I can fire some custom code as you could with any other event.

Adam Haile
A: 

Fair enough. You probably know more than I do about Windows Mobile. :) I just started programming for it. But in regular WinForms, you can override the OnXxx event handler methods all you want. A quick look in Reflector with the CF shows that Control, TextBoxBase and TextBox don't prevent you from overriding the OnMouseDown event handler.

Tried it... and while it compiles, nothing happens. And actually OnMouseDown doesn't even show up for "base."

Also, while I know it's possible to override the method, what about overriding the event? I just want it so that I can attach to MouseDown from my application. Overriding OnMouseDown internally makes it so I still then would have to fire a custom event to catch externally.

Adam Haile
A: 

Looks like you're right. Bummer. No MouseOver event.

One of the fallbacks that always works with .NET, though, is P/Invoke. Someone already took the time to do this for the .NET CF TextBox. I found this on CodeProject:

http://www.codeproject.com/KB/cs/TextBox_subclassing.aspx

Hope this helps

ageektrapped
A: 

is there an 'OnEnter' event that you could capture instead?

it'd presumably also capture when you tab into the textbox as well as enter the text box by tapping/clicking on it, but if that isn't a problem, then this may be a more straightforward work-around

dalelane
+2  A: 
Frank Razenberg