How do I avoid writing in a textbox in c #?

0

What I want to do is that when I reach the limit of 10 characters that the user wrote on my little system I made an error and I did not write anymore, but what happens is that it only marks the error but every time I enter a new one number receives it and puts it in the textbox

This is the restriction code and it is in the textbox.

private void clave_TextChanged(object sender, EventArgs e)
        {
            if (clave.Text.Length>10) {
                MessageBox.Show("Error");
            }
        }
    
asked by David 28.09.2016 в 21:07
source

3 answers

5

Assigning the maximum length property of your TextBox to 10, there is no need to manipulate the Keypress or TextChange events for that purpose.

textBox1.MaxLength = 10;

    
answered by 28.09.2016 / 21:55
source
3

If it's an asp.Net TextBox, you can use the MaxLength property and tell it how many characters will be limited, something like this:

    <asp:TextBox runat="server" ID="text" MaxLength="10"></asp:TextBox>
    
answered by 28.09.2016 в 21:22
0

TextChanged is not the best event for your purpose, it is better that you use KeyDown and use Handle=true to control the click event.

But if your purpose is to limit the maximum number of characters in a TextBox you also have the property MaxLenght indicating the maximum number of characters it can contain.

Greetings,

    
answered by 28.09.2016 в 21:20