0

I would like to allow only certain keys to be valid for my TextBox control. I have set it up as follows and it works fine.

private void TextBox_MapInputMin_KeyDown(object sender, KeyEventArgs e)
        {
            if (!IsFloatingPointTextBoxValid(e, textBox_MapInputMin))
            {
                // 'Handled' property stops key from being entered into the control
                e.Handled = true;
                return;
            }
        }

        private bool IsFloatingPointTextBoxValid(KeyEventArgs e, TextBox textBox)
        {
            if (e.Shift)
            {
                return false;
            }
            else if (e.Key == Microsoft.Xna.Framework.Input.Keys.Back)
            {
                return true;
            }
            else if (e.Key == Microsoft.Xna.Framework.Input.Keys.Left || e.Key == Microsoft.Xna.Framework.Input.Keys.Right)
            {
                return true;
            }
            else if (textBox.Text.Length < 7)
            {
                // Only allow character input if there are less than 7 characters

                if (e.Key == Microsoft.Xna.Framework.Input.Keys.OemPeriod || e.Key == Microsoft.Xna.Framework.Input.Keys.Decimal)
                {
                    return textBox.Text.Contains(".") ? false : true;
                }
                else if ((e.Key >= Microsoft.Xna.Framework.Input.Keys.D0) && (e.Key <= Microsoft.Xna.Framework.Input.Keys.D9))
                {
                    return true;
                }
                else if ((e.Key >= Microsoft.Xna.Framework.Input.Keys.NumPad0) && (e.Key <= Microsoft.Xna.Framework.Input.Keys.NumPad9))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

It works fine until I hold a key down that is. Repeating keys aren't picked up by the event and can be entered without being checked.

How do I prevent repeating keys from being entered incorrectly and make them undergo the same processing?

Edited by DaveTran: n/a

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by abelLazm
1

The following code works fine for allowing only numeric data and adding only one decimal point it also works fine for key hold

void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
         
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
           
        }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.