Share via


C# edit for only upper case letters and number in a textbox

Question

Wednesday, July 3, 2013 4:38 PM

In a C# 2010 desktop application, I want a textbox field to contain only letters  in upper case. In addition, I want to allow for numbers. Any other values are  not valid for this textbox field.

Thus can you show me code on how to  setup the edit for uppercase letters and numbers only in the textbox?

All replies (6)

Thursday, July 4, 2013 12:43 PM âś…Answered

No it won't work for that scenario. You'd need to modify the code in TextChanged event to go through the entire string and remove all the characters that don't match the criteria instead of just checking the last character. A simple for loop would suffice.


Wednesday, July 3, 2013 5:16 PM

Here's how I like to go about it.  In the designer, under the Behavior section, set the CharacterCasing property to Upper.

Then handle the TextChanged and Validating events.  Check for valid input in those cases.  Add error handling functions to set and clear an input error, and provide feedback to the user as to why their input is being rejected.  (In this simple example I simply adjust the back color of the text box.)

public partial class Form1 : Form
{
    Color saved;
        
    public Form1()
    {
        InitializeComponent();
        saved = textBox1.BackColor;
    }

    void SetError()
    {
        textBox1.BackColor = Color.Pink;
    }

    void ClearError()
    {
        textBox1.BackColor = saved;
    }

    private void textBox1_TextChanged( object sender, EventArgs e )
    {
        Check();
    }

    private void textBox1_Validating( object sender, CancelEventArgs e )
    {
        if( !Check() )
        {
            e.Cancel = true;
        }
    }

    bool Check()
    {
        if( !textBox1.Text.All( c => char.IsUpper( c ) || char.IsDigit( c ) ) )
        {
            SetError();
            return false;
        }
        else
        {
            ClearError();
            return true;
        }
    }
}

Wednesday, July 3, 2013 5:26 PM

I've done this before with the code below. It's not very elegant but it worked for me.

    public partial class Form1 : Form
    {

        private bool isUpperOrNumber = false;
        private bool keyPressed = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            keyPressed = true;
            if (char.IsNumber(e.KeyChar) || char.IsUpper(e.KeyChar))
            {
                isUpperOrNumber = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (keyPressed && !isUpperOrNumber && textBox1.Text.Length > 0)
            {
                isUpperOrNumber = false;
                keyPressed = false;
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
                textBox1.SelectionStart = textBox1.Text.Length;
                textBox1.SelectionLength = 0;
            }
            else
            {
                isUpperOrNumber = false;
                keyPressed = false;
            }
        }
    }

Wednesday, July 3, 2013 8:17 PM

Will keypress work if the user copies and pastes the value into the textbook field?


Friday, July 5, 2013 7:16 AM

 TextBox.KeyPress += (s, e) =>   e.Handled = char.IsDigit(e.KeyChar) || char.IsUpper(e.KeyChar);

or

private void TextBox_KeyPressEvent( object sender, EventArgs e )

{

e.Handled = char.IsDigit(e.KeyChar) || char.IsUpper(e.KeyChar);

}


Saturday, July 6, 2013 9:12 PM

Would I need to check each character to be able to use the code you listed above?