简体   繁体   English

TextBox LostFocus无限循环

[英]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. 在方法结束后,每次将txtICode.IsFocused设置为true,并且循环将永远持续下去。 I tried removing txtICode.Focus(); 我尝试删除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. 我使用的是.Net 3.5和WPF。

You do not have to restore focus to TextBox in the LostFocus event. 您不必在LostFocus事件中将焦点还原到TextBox

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: 您可以使用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;
                    }
                }
            }
        }
    });
}
           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;
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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