5

For begin I want to describe my problem for you.

I want to show huge number of records in something like combobox, but because combobox isn't a good solution for displaying such huge number of data hence I simulate combobox behaviour with DataGridView.

Now my problem is when user click out of DataGridView , DataGridView should be closed(like combobox when it isn't collapsed or dropped). but there is a lot of other control on the form and I cant handle click event of all of them to detect that out of DataGridView has been clicked.

to sum up I look for a simple solution for invisible DataGridView if user click outta that .

at the end I know a vague awareness of MouseCapture property of controls but I cant work with that and I dont know how can I use that for handle my desire.I am appreciate you if you can help me for using MouseCapture for solving this problem or giving another solution.

thanks for you

10
  • obviously Lost focus and leave isnt my asnwer
    – hamed
    Commented Jan 9, 2017 at 20:33
  • I think the drop-down portion of the ComboBox control is actually in a separate window from the text box. You could try something similar: show your DataGridView in a separate, dedicated window, and hide that window when it is deactivated.
    – adv12
    Commented Jan 9, 2017 at 20:35
  • Perhaps there is a simple solution, but I am a bit rusty in WinForms, so for now can only suggest something like stackoverflow.com/questions/4991044/… Commented Jan 9, 2017 at 20:40
  • @adv12 how can I detect it is deactivated? I dont see activate property or event on datagridview
    – hamed
    Commented Jan 9, 2017 at 20:47
  • @hamed, you listen for the Deactivate event on the containing form, not on the DataGridView.
    – adv12
    Commented Jan 9, 2017 at 20:49

1 Answer 1

4

A custom control should make this fairly simple, especially if this is a top-level control (i.e. directly in your main window). You can listen for click events on the parent object and use the ClientRectangle property to determine if the click was outside the DataGridView.

Here's a basic example:

class MyDataGridView : DataGridView, IMessageFilter {
    public MyDataGridView() {
        Application.AddMessageFilter(this);
        this.HandleDestroyed += (sender, args) => Application.RemoveMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m) {
        if (m.Msg == 0x201) {
            if (!ClientRectangle.Contains(PointToClient(Control.MousePosition))) {
                Hide();
            }
        }
        return false;
    }
}
4
  • Dear @Peter I test your solution, I again face with my problem. I said I have several Control on my form then as you said I create a custom control and put that on form but because we monitor Parent.MouseClick only if you click on parent form we detect click out of custom control , and if user click on other controls on parent form custom control doesnt sense that click .
    – hamed
    Commented Jan 10, 2017 at 18:42
  • Hi @Hamed, I understand now. I think this new version should work
    – Peter
    Commented Jan 10, 2017 at 19:00
  • dear @pete this new solution work properly. you are great.
    – hamed
    Commented Jan 11, 2017 at 20:00
  • Thanks, Peter your answer is helpful for me to archive my requirement, thanks for sharing your valuable answer with us...@ Commented Jan 3, 2020 at 13:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.