Taras
Taras

Reputation: 1128

TextBox.IsFocused property?

I want to change a textBox.text only if it is not in focus. Example:

public void updateValue()
    {
        if (!this.valueTB.IsFocused)
            this.valueTB.Text = updatedValue.ToString();
    } 

But the problem is that this.valueTB.IsFocused property changes only when it's actually focused but not when it's focused out.

Upvotes: 1

Views: 7506

Answers (5)

Ionică Bizău
Ionică Bizău

Reputation: 113345

Use only IsFocused propery:

public void updateValue()
{
     if((!tb.IsFocused) || (tb.IsFocused == false))
     {
          tb.Text = "The text was updated when I wasn't focused. Is't OK?";
     }
} 

Upvotes: 0

Jan Kohlhase
Jan Kohlhase

Reputation: 16

maybe you could take a bool value which tells you if the TB is focused or not:

    bool TBIsFocused = false;
    private void valueTB_Enter(object sender, EventArgs e)
    {
        TBIsFocused = true;
    }

    private void valueTB_Leave(object sender, EventArgs e)
    {
        TBIsFocused = false;
    }

Upvotes: 0

SidPen
SidPen

Reputation: 180

the problem is that IsFocused event gets fired only once when it gets the focus thats it and then it doesn't fire. I think you want it to fire this event continuous till Textbox has the focus. For that you have to keep on checking whether it has the focus or not at specific interval.

Upvotes: 0

Inkey
Inkey

Reputation: 2207

You can use the textbox focus event Lost

And GotFocus

Upvotes: 0

Clemens
Clemens

Reputation: 128062

The IsFocused property certainly changes its value when the TextBox gets or loses the focus.

But TextBox also provides the GotFocus and LostFocus events.

Upvotes: 2

Related Questions