Crouzilles
Crouzilles

Reputation: 813

Why is textbox.Text empty although a value has been entered?

I have this textbox with a KeyPressEventArgs.

In a dialog I can show the value entered in the textbox via the keychar but not via the textbox.Text member. Once a second character is entered, the textbox.Text member shows only one character, the first one, and so on, so basically, the last charactered isn't shown.

Here is the code:

private void textBoxDegrees_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == (char)Keys.Back) //The  character represents a backspace
        {

            e.Handled = false; //Do not reject the input
        }
        else
        {
            e.Handled = true; //Reject the input
            return;
        }
        MessageBox.Show(e.KeyChar.ToString());
        MessageBox.Show(textBoxDegrees.Text);
    }

Any idea what is going on?

Regards Crouz

Upvotes: 0

Views: 161

Answers (2)

mihkov
mihkov

Reputation: 1189

MessageBox.Show(textBoxDegrees.Text);

This will show your the text in the input before you enter the new value. If it contains 1234 and you press 5 it will show 1234.

You can use TextChanged event handler. It is auto generated when double click on the input.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 191058

KeyPress happens before the Text property is changed, so that you can filter it. Perhaps you want the Changed event.

Upvotes: 3

Related Questions