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

hamed
  • 471
  • 1
  • 9
  • 22
  • obviously Lost focus and leave isnt my asnwer – hamed Jan 09 '17 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 Jan 09 '17 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 http://stackoverflow.com/questions/4991044/winforms-intercepting-mouse-event-on-main-form-first-not-on-controls – Eugene Podskal Jan 09 '17 at 20:40
  • @adv12 how can I detect it is deactivated? I dont see activate property or event on datagridview – hamed Jan 09 '17 at 20:47
  • @hamed, you listen for the [Deactivate event](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.deactivate%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) on the containing form, not on the DataGridView. – adv12 Jan 09 '17 at 20:49
  • If I remember right from implementing something similar years ago, you may also need to do some P/Invoke with the native Windows `ShowWindow` function to get all the desired properties for the containing form. I don't remember what I couldn't do just with .NET functionality, but there was something missing. – adv12 Jan 09 '17 at 20:51
  • @adv12 yeap, I saw similar solution but it is very difficult and complex, also I saw http://stackoverflow.com/questions/6761786/how-can-a-control-handle-a-mouse-click-outside-of-that-control that give simple solution but i cant utilize that – hamed Jan 09 '17 at 20:58
  • dear @adv12 do you saw this link : http://stackoverflow.com/questions/6761786/how-can-a-control-handle-a-mouse-click-outside-of-that-control?noredirect=1&lq=1 if you understand what I should do from that link please describe that for me. – hamed Jan 09 '17 at 21:09
  • Hi @hamed, did implementing the custom control help with this problem? Let me know if you're still having a problem. If not, please mark my answer accepted. Thanks! – Peter Jan 10 '17 at 15:53
  • @Peter give me more time for test that – hamed Jan 10 '17 at 16:08

1 Answers1

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;
    }
}
Peter
  • 674
  • 6
  • 14
  • 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 Jan 10 '17 at 18:42
  • Hi @Hamed, I understand now. I think this new version should work – Peter Jan 10 '17 at 19:00
  • dear @pete this new solution work properly. you are great. – hamed Jan 11 '17 at 20:00
  • Thanks, Peter your answer is helpful for me to archive my requirement, thanks for sharing your valuable answer with us...@ – Nikunj Satasiya Jan 03 '20 at 13:16