YWah
YWah

Reputation: 579

Check wpf textbox focus

In my Textbox, I've set Textbox.MaxLength = 2. What I want to achieve is when I enter "6" in my Textbox, if the focus of the Textbox is lost, the value will become "06".

        if (!(string.IsNullOrEmpty(Textbox.Text)))
        {
            //Minute value cannot be more than 59
            if (Int32.Parse(Textbox.Text) > 59)
                Textbox.Text = string.Empty;

            else if (Int32.Parse(Textbox.Text) < 10 && Textbox.IsFocused == false)
                Textbox.Text = Int32.Parse(Text.Text).ToString("00");
        }

I've tried to set Texbox.IsFocused == false, but it still doesn't work. Any suggestions?

Upvotes: 1

Views: 4579

Answers (1)

serendip
serendip

Reputation: 56

use LostFocus event.

xaml

<TextBox x:Name="tbTextBox" Height="30" LostFocus="tbTextBox_LostFocus"/>

code behind

private void tbTextBox_LostFocus(object sender, RoutedEventArgs e)
{
    tbTextBox.Text = tbTextBox.IsFocused.ToString();
}

Upvotes: 2

Related Questions