首页 > 解决方案 > 如何对文本进行限制?C#

问题描述

如何限制文本框字符?我的意思是,当我达到 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";
}

标签: c#

解决方案


推荐阅读