简体   繁体   English

为什么textbox.focus会抛出lostfocus事件?

[英]Why is textbox.focus throwing the lostfocus event?

I've seen a few similar questions on SO but nothing that seems to actually address the issue. 我在SO上看过一些类似的问题,但似乎没有任何东西可以解决这个问题。 Here's a simplified version of the function. 这是该函数的简化版本。

Private Sub Check_Quantity(sender As System.Object, e As System.Windows.RoutedEventArgs) _
    Handles textbox_quantity.LostFocus

        Dim worked As Boolean = Integer.TryParse(textbox_quantity.Text, quantity)

        If Not worked Then
            MsgBox("Enter a valid number for the quantity")
            textbox_quantity.Focus()
            textbox_quantity.SelectAll()
            quantity = 0
        End If
End Sub

It's important to note that this is WPF. 重要的是要注意这是WPF。 What I want to do is very simple. 我想做的很简单。 When someone finishes with the textbox the program checks that what they entered is a number. 当某人完成文本框时,程序会检查他们输入的是一个数字。 If it does it sticks this in an integer. 如果确实如此,则将其固定为整数。 If not, it tells them to fix it and keeps the focus on the textbox. 如果没有,它告诉他们修复它并将焦点保持在文本框上。 The issue is a few things, but what it comes down to is this function runs in an infinite loop. 问题是一些问题,但它归结为这个函数在无限循环中运行。 This same function works fine in WinForms, but not in WPF. 这个功能在WinForms中运行良好,但在WPF中运行不正常。

On some other questions people have said that the messagebox appearing causes focus to be lost, but in testing this isn't true. 在其他一些问题上,人们已经说消息框出现导致焦点丢失,但在测试中这不是真的。 It still loops regardless of if the messagebox is called or not. 无论是否调用消息框,它仍然循环。 The problem is the call to textbox_quantity.Focus(). 问题是调用textbox_quantity.Focus()。 Without that it works fine. 没有它,它工作正常。 Regardless of whether it's there or not though, focus is not set to the textbox, though textbox_quantity.Focus() still returns a value of true. 无论它是否存在,虽然textbox_quantity.Focus()仍然返回值true,但焦点不会设置到文本框。 Any thought of what's going on and maybe how I could fix it? 想到发生了什么,也许我怎么能解决它?

The problem is that focus() is executed exactly when you call it... so just before the focus is given to the other control... Hence the loop... A workaround i've found is to postpone the execution of such code using System.Threading.ThreadPool.QueueUserWorkItem. 问题是focus()恰好在你调用它时执行...所以就在焦点被放到另一个控件之前...因此循环...我发现的一个解决方法是推迟执行这样的使用System.Threading.ThreadPool.QueueUserWorkItem的代码。 But since focus() must be called from the window thread, i also have to use Me.Dispatcher.Invoke. 但由于必须从窗口线程调用focus(),我还必须使用Me.Dispatcher.Invoke。
So the result is a little bit complicated, but it works !! 所以结果有点复杂,但它的确有效!

            System.Threading.ThreadPool.QueueUserWorkItem(
                                  Sub()
                       Me.Dispatcher.Invoke(Sub()
                                                sender.Focus()
                                                sender.SelectAll()
                                            End Sub)
                                  End Sub)

我会尝试在SelectAll调用之后移动Focus,或者在那里添加一个e.Handled = true语句。

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

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