I've seen a few similar questions on SO but nothing that seems to actually address the issue. 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. 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.
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(). 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. Any thought of what's going on and maybe how I could fix it?