TextBox LostFocus无限循环

本文关键字:无限循环 LostFocus TextBox | 更新日期: 2023-09-27 18:08:27

在我的表单中有一个文本框,供用户键入项目代码。当文本框的焦点丢失时,它将查看数据库以检查项目代码是否存在。然而,当我试图通过点击其他文本框失去焦点时,我得到了无限循环。

    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;
                        }
                    }
                }
            }
        }
    }

每次方法结束后,txtICode.IsFocused都被设置为true,循环将永远继续下去。我试着删除txtICode.Focus();,但它没有区别。我的代码有什么问题吗?

我使用。net 3.5和WPF的形式

TextBox LostFocus无限循环

您不必在LostFocus事件中将焦点恢复到TextBox

删除这两行:

txtICode.Focus();
return;

你可以实现更干净的代码&阅读方式:

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");
    }
 }
           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;
                }
            }

你可以使用BeginInvoke方法来异步执行:

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;
                    }
                }
            }
        }
    });
}