【问题标题】:TextBox LostFocus infinite loopTextBox LostFocus 无限循环
【发布时间】:2013-04-10 12:30: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。

【问题讨论】:

  • 为什么要恢复对LostFocus事件的关注?
  • 删除消息框并检查!
  • 即使在我注释掉消息框后,我仍然会陷入无限循环。我什至注释掉了txtICode.Focus() 部分。
  • 您在哪里注册 LostFocus 活动,请确保您已多次订阅。

标签: c# wpf


【解决方案1】:

您不必在 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");
    }
 }

【讨论】:

  • 这很好用!谢谢!这些代码实际上是由其他人编写的,我正在尝试修复一些错误并改进代码。再次感谢!
【解决方案2】:
           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;
                }
            }

【讨论】:

    【解决方案3】:

    可以使用BeginInvoke Method异步执行:

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

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多