Question

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?


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 do 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.


According to the .Net Framework, 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.

Was it helpful?

Solution

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

OTHER TIPS

I know this answer is way late, but hopefully it ends up being useful for someone who finds this. Also, I didn't entirely come up with it myself. I believe I originally found most of the info on the OpenNETCF boards, but what is typed below is extracted from one of my applications.

You can get a mousedown event by implementing the OpenNETCF.Windows.Forms.IMessageFilter interface and attaching it to your application's message filter.

static class Program {
    public static MouseUpDownFilter mudFilter = new MouseUpDownfilter();
    public static void Main() {
        Application2.AddMessageFilter(mudFilter);
        Application2.Run(new MainForm());
    }
}

This is how you could implement the MouseUpDownFilter:

public class MouseUpDownFilter : IMessageFilter {
    List ControlList = new List();

    public void WatchControl(Control buttonToWatch) {
        ControlList.Add(buttonToWatch);
    }

    public event MouseEventHandler MouseUp;
    public event MouseEventHandler MouseDown;

    public bool PreFilterMessage(ref Microsoft.WindowsCE.Forms.Message m) {
        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;

        // If the message code isn't one of the ones we're interested in
        // then we can stop here
        if (m.Msg != WM_LBUTTONDOWN && m.Msg != WM_LBUTTONDOWN) {
            return false;
        }

        //  see if the control is a watched button
        foreach (Control c in ControlList) {
            if (m.HWnd == c.Handle) {
                int i = (int)m.LParam;
                int x = i & 0xFFFF;
                int y = (i >> 16) & 0xFFFF;
                MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 1, x, y, 0);

                if (m.Msg == WM_LBUTTONDOWN)
                    MouseDown(c, args);
                else
                    MouseUp(c, args);

                // returning true means we've processed this message
                return true;
            }
        }
        return false;
    }
}

Now this MouseUpDownFilter will fire an MouseUp/MouseDown event when they occur on a watched control, for example your textbox. To use this filter you add some watched controls and assign to the events it might fire in your form's load event:

private void MainForm_Load(object sender, EventArgs e) {
    Program.mudFilter.WatchControl(this.textBox1);
    Program.mudFilter.MouseDown += new MouseEventHandler(mudFilter_MouseDown);
    Program.mudFilter.MouseUp += new MouseEventHandler(mudFilter_MouseUp);
}

void mudFilter_MouseDown(object sender, MouseEventArgs e) {
    if (sender == textBox1) {
        // do what you want to do in the textBox1 mouse down event :)
    }

}

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);
    }
}

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top