0

i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it. enter image description here

i tried this:

private void txtProduct_Enter(object sender, EventArgs e)
{
    listProduct.Show();
    UIElement el = (UIElement)sender;
    el.CaptureMouse();
}  

private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
    if (e.Clicks >= 1)
    {
        txtProduct_Leave(sender, new EventArgs());
    }
}

private void txtProduct_Leave(object sender, EventArgs e)
{
    listProduct.Hide();
}

but obviously it shows error. how do i achieve this? any help?

Wahid Masud
  • 993
  • 10
  • 35
  • Did you try other [suggestions,](https://stackoverflow.com/questions/6489032/wpf-remove-focus-when-clicking-outside-of-a-textbox) [similar to yours on SO](https://stackoverflow.com/questions/7208356/how-to-deselect-textbox-if-user-clicks-elsewhere-on-the-form) ? – Michael Oct 14 '17 at 08:59
  • Try `txtbox_Leave` function. – Archana Parmar Oct 14 '17 at 09:00
  • yah I have tried every possible suggestions I could find. nothing worked. Please see my edited code. – Wahid Masud Oct 14 '17 at 09:44

1 Answers1

0

I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.

//my_page.designer.cs  
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);  

//my_page.cs  
private void groupBox2_clicked(object sender, EventArgs e)
{
    listProduct.Hide();
}
Wahid Masud
  • 993
  • 10
  • 35