Home » C# ProgrammingRSS

Problems with LostFocus event

In Winforms I was able to validate a textbox and if invalid put the focus back to the control by something like

privatevoid textBox1_LostFocus(object sender, RoutedEventArgs e)
  {
   if (textBox1.Text == "")
   {
    textBox1.Focus();
    textBox1.SelectAll();
   }
  }

In WPF this does not work. If you have a simple window with two textboxes, the application loops when you tab off textBox1. I found a solution on this forum. This works:

privatevoid textBox1_LostFocus(object sender, RoutedEventArgs e)
  {
   if (textBox1.Text == "")
   {
    this.textBox1.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(ResetFocus));
   }
  }
  privatevoid ResetFocus()
  {
   textBox1.Focus();
   textBox1.SelectAll();
  }

I am not sure what this soution is doing, but I'm sure I could find out. However, what I would like to understand is why you can't just set the focus back to the control from within its LostFocus event handler.

Can anyone explain why this does not work in WPF?

 

5 Answers Found

 

Answer 1

Hi FryingSaucer,

You are practically doing the same thing. textBox1.Dispatcher.BeginInvoke is just for cross threading call to UI thread.

Thanks,

Muhammad

shujaatsiddiqi.blogspot.com

 

Answer 2

I've discovered I can do what I want more simply by fielding the PreviewLostKeyboardFocus event  and setting the Handled property to true. The control  does then not lose focus.

 

Answer 3

Hi FryingSaucer,

Yes, if you validate  the data in the PreviewLostKeyboardFocus, and set  the Handled as true, the LoastFocus event  will never raise.

Best regards,
Kevin Pan
 

Answer 4

I did discover how to validate  a textbox  and documented how I did this above for completeness. However, it's not really an answer to my original post, which was why can't you just reset the focus  to the original element after it's lost focus in WPF. I realise it's something to with having tabbed off to another control, but resetting the focus after losing it worked fine in Windows Forms. It may be something to do with the different threading in WPF from Windows Forms.

Although I've fixed my problem, if anyone can explain why the behaviour is different in WPF from Windows Forms, I'd still like to know. It would probably help my understanding of WPF architecture.

Thanks

 

Answer 5

Hi FryingSaucer,

I have made a test that set  the focus  back via the BeginInvoke method in the LostFocus event  of the TextBox, and it works  as expected.

The test is as follows:
XAML

<StackPanel>

    <TextBox LostFocus="TextBox_LostFocus" Name="textbox">hello</TextBox>

    <Button>Click</Button>

</StackPanel>

Code-behind

privatevoid TextBox_LostFocus(object sender, RoutedEventArgs e)

{

    if (textbox.Text == "")

    {

        this.textbox.Dispatcher.BeginInvoke((Action)(() => { textbox.Focus(); }));

    }

}

 

When the text  box is empty, and we press the Tab key, the focus will be set back.

Hope this helps.

Best regards,
Kevin Pan

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter