Is there any way to detect a mouseclick outside a user control?

后端 未结 6 1365
青春惊慌失措
青春惊慌失措 2020-12-10 12:33

I\'m creating a custom dropdown box, and I want to register when the mouse is clicked outside the dropdown box, in order to hide it. Is it possible to detect a click outside

6条回答
  •  死守一世寂寞
    2020-12-10 12:47

    I just wanted to share this. It is probably not a good way of doing it that way, but looks like it works for drop down panel that closes on fake "MouseLeave", I tried to hide it on Panel MouseLeave but it does not work because moving from panel to button leaves the panel because the button is not the panel itself. Probably there is better way of doing this but I am sharing this because I used about 7 hours figuring out how to get it to work. Thanks to @FTheGodfather

    But it works only if the mouse moves on the form. If there is a panel this will not work.

        private void click_to_show_Panel_button_MouseDown(object sender, MouseEventArgs e)
        {
            item_panel1.Visible = true; //Menu Panel
        }
    
    
    
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!item_panel1.Bounds.Contains(e.Location))
            {
                item_panel1.Visible = false; // Menu panel 
            }
        }
    

提交回复
热议问题