我如何限制文本框字符?我的意思是,当我达到4个字符时,该程序将不会插入超过4个字符,并且如果我插入的字符少于4个字符,则该程序仅在单击“确认”时才会重置文本。图片:
代码:
namespace ATM_PLUS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox.PasswordChar = '*';
textBox.ReadOnly = true;
}
private void buttons_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox.Text += btn.Text;
}
private void ButtonConfirm_Click(object sender, EventArgs e)
{
if (textBox.Text == Globals.PIN)
{
Form2 openForm = new Form2();
openForm.Show();
Hide();
}
if (textBox.Text != Globals.PIN)
{
MessageBox.Show("Invalid Password, try again");
textBox.ResetText();
}
}
private void ButtonClear_Click(object sender, EventArgs e)
{
textBox.ResetText();
}
}
}
public class Globals
{
public static Random number = new Random();
public static int amount = number.Next(1, 9999);
public static string PIN = "5506";
}
我如何限制文本框字符
textBox.MaxLength = 4;
如果我输入的字符少于4个,则仅当我单击“确认”时程序才会重置文本
private void ButtonConfirm_Click(object sender, EventArgs e)
{
if(textBox.TextLength < 4) textBox.ResetText();
...
}