简体   繁体   中英

TextBox LostFocus infinite loop

I have a textbox in my form for user to key in an item code. When the focus of the textbox is lost, it will look into the database to check if the item code exists or not. However, I am getting infinite loop when I try to lose focus by clicking on other textboxes.

    private void txtICode_LostFocus(object sender, RoutedEventArgs e)
    {
        if (txtICode.IsFocused != true)
        {
            if (NewData)
            {
                if (txtICode.Text != null)
                {
                    if (txtICode.Text != "")
                    {
                        Item temp = new Item();
                        Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code }, new string[] { txtICode.Text });
                        if (list.Length > 0)
                        {
                            System.Windows.Forms.MessageBox.Show("This item code is already being used.", "Invalid information");
                            txtICode.Focus();
                            return;
                        }
                    }
                }
            }
        }
    }

The txtICode.IsFocused is set to true every time after the end of the method and the loop just continues forever. I tried removing txtICode.Focus(); but it makes no difference. Is there anything wrong with my code?

I am using .Net 3.5 and WPF for my form.

You do not have to restore focus to TextBox in the LostFocus event.

remove these 2 lines :

txtICode.Focus();
return;

You could implement code more clean & readable way :

private void txtICode_LostFocus(object sender, RoutedEventArgs e)
{        
    if (!NewData)
        return;

    if (String.IsNullOrEmpty(txtICode.Text))
        return;

    Item temp = new Item();
    Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code }, new string[] { txtICode.Text });
    if (list.Length > 0)
    {
        System.Windows.Forms.MessageBox.Show("This item code is already being used.", "Invalid information");
    }
 }

You can use BeginInvoke Method to execute asynchronously:

private void txtICode_LostFocus(object sender, RoutedEventArgs e)
{
    txtICode.Dispatcher.BeginInvoke(() => {
    if (txtICode.IsFocused != true)
    {
        if (NewData)
        {
            if (txtICode.Text != null)
            {
                if (txtICode.Text != "")
                {
                    Item temp = new Item();
                    Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code }, new string[] { txtICode.Text });
                    if (list.Length > 0)
                    {
                        System.Windows.Forms.MessageBox.Show("This item code is already being used.", "Invalid information");
                        txtICode.Focus();
                        return;
                    }
                }
            }
        }
    });
}
           private void txtICode_LostFocus(object sender, RoutedEventArgs e)
            {
               string inputText = txtICode.Text;
               if (string.IsNullOrEmpty(inputText) || !NewData)
               {
                   return;
               }
               Item temp = new Item();
               Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code }, 
                                                       new string[] { inputText  });
               if (list != null && list.Length > 0)
               {
                  MessageBox.Show("This item code is already being used.", "Invalidinformation");
                  txtICode.Focus();
                  return;
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM