1

I would like my textbox to close itself when i click elsewhere, how can I do this?

I've seen Control.Capture which seems to grab the mouse events, but it doesn't work.

I also tried Control.Leave and Control.LostFocus events but they're only triggered when I press the tab key or click on another text field.

By closing I mean remove it from it's parent, but the important thing here I think is the event

Bryan Peeters
  • 161
  • 10
  • 2
    What do you meant by closing textbox? you mean hide? – Sriram Sakthivel May 07 '14 at 15:50
  • @SriramSakthivel Don't understand this too. You cannot close a textbox. – Andy May 07 '14 at 15:51
  • Actually I have an event on my button, when I click it i create a textbox to rename it, then i would like to close this box when I click away. – Bryan Peeters May 07 '14 at 15:51
  • Add a handler to mouse move on the form, you can then hide the text box when the mouse moves over the form. You should think about what happens if the user does everything on keyboard though so you should probably subscribe to lost focus on the text box too – WraithNath May 07 '14 at 15:52
  • @BryanPeeters Again, What do you meant by **close**? – Sriram Sakthivel May 07 '14 at 15:52
  • Should the textbox **only** disappear when you click somewhere else **in your application**, or should it also disappear if, for example, you click or ALT-TAB onto another application? What about tabbing from control to control in your application (not using the mouse)? –  May 07 '14 at 15:52
  • @SriramSakthivel i mean remove it from its parent – Bryan Peeters May 07 '14 at 15:54
  • @elgonzo any would be great – Bryan Peeters May 07 '14 at 15:54
  • Then it rather looks like you are being interested in a 'control focus lost' event, correct? –  May 07 '14 at 15:55
  • @elgonzo Exactly, but what I tried didn't work when I click on a blank panel – Bryan Peeters May 07 '14 at 15:56
  • Okay, i see. Look here for possible approaches: http://stackoverflow.com/questions/804374/capturing-mouse-events-from-every-component-on-c-sharp-winform (and ofcourse keep the LostFocus event handler, for all the other cases). –  May 07 '14 at 16:02
  • @elgonzo Tank you, I got over this answer but I was hoping for something simpler :D – Bryan Peeters May 07 '14 at 16:05

1 Answers1

1

Handle the WM_CAPTURECHANGED message and check whether textbox contains focus but it is not captured, if so hide it.

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    const int WM_CAPTURECHANGED = 0x0215;
    if (m.Msg == WM_CAPTURECHANGED)
    {
        if (!textBox1.Capture && textBox1.Focused && textBox1.Visible)
        {
            textBox1.Visible = false;
        }
    }
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Yes, you need to put this in your form where that textbox lives. Doesn't work is not helpful. Please tell what happens? I tried before posting and it worked. You keep the focus in textbox, then click somewhere texbox will hide automatically. – Sriram Sakthivel May 07 '14 at 16:13
  • The textbox doesn't hide – Bryan Peeters May 07 '14 at 16:17
  • Before clicking outside did you click the textbox first? If not do it and see. If still doesn't work post your updated code where you used this code, so that I can see where you're wrong. – Sriram Sakthivel May 07 '14 at 16:18
  • I've the same code as you, I set it to visible and I tried with Capture to true and false, but the condition never true. – Bryan Peeters May 07 '14 at 16:42
  • Okay I tried almost from scratch and it's working now, thank you :) – Bryan Peeters May 07 '14 at 16:52