1
I am simulating an electronic urn in C#. To limit data entry, I decided to create a "keyboard" from 0 to 9, for the insertion of votes. However, I’m having trouble limiting the amount of characters in the textbox, even setting the maximum size to 2 characters, for example, when using buttons the maximum size of the textbox is ignored by buttons, which add text infinitely, The same doesn’t happen when I use the computer keyboard. Is there any way to fix this?
Here’s an example of the code I’m using and a print of what my form looks like at the moment:
private void btnUm_Click(object sender, EventArgs e)
{
txtNum.Text += "1";
}
private void btnDois_Click(object sender, EventArgs e)
{
txtNum.Text+="2";
}
private void btnTres_Click(object sender, EventArgs e)
{
txtNum.Text += "3";
}
private void btnQuatro_Click(object sender, EventArgs e)
{
txtNum.Text += "4";
}
private void btnCinco_Click(object sender, EventArgs e)
{
txtNum.Text += "5";
}
private void btnSeis_Click(object sender, EventArgs e)
{
txtNum.Text += "6";
}
private void btnSete_Click(object sender, EventArgs e)
{
txtNum.Text += "7";
}
private void btnOito_Click(object sender, EventArgs e)
{
txtNum.Text += "8";
}
private void btnNove_Click(object sender, EventArgs e)
{
txtNum.Text += "9";
}
private void btnZero_Click(object sender, EventArgs e)
{
txtNum.Text += "0";
}
You can use the event
TextBox_textChanged
, so whenever a digit is inserted in thetextBox
you do the size check.– Tuxpilgrim