In my WPF
application I am doing some input validation in the lostfocus
event of a textbox
control. If the text does not meet the correct criteria I clear the text in the textbox
. That works fine. What I am having trouble doing is returning focus to the control after viewing a message box. For example when I use the below code, the text box focus method re-triggers the lost focus event after I close the message box.
private void TaskNameBox_LostFocus(object sender, RoutedEventArgs e)
{
... validation logic here
MessageBox.Show("Message.", "Error", MessageBoxButton.OK);
TaskNameBox.Focus();
}
I don't know why the Focus method would retrigger the lost focus event, but I need a way to get the focus back on the TaskNameBox Control after losing it. Any suggestions would be very much appreciated. I am new to WPF
.
Use it following:
private void TaskNameBox_LostFocus(object sender, RoutedEventArgs e)
{
... validation logic here
MessageBox.Show("Message.", "Error", MessageBoxButton.OK);
Dispatcher.BeginInvoke((ThreadStart)delegate
{
TaskNameBox.Focus();
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With