c# - 如何对文本进行限制?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";
}
解决方案
推荐阅读
- python-3.x - 从 3D 或 2D 点云生成数字
- ios - 无法制作 iOS 版本
- javascript - 按另一个对象排序树结构对象
- amazon-ec2 - 如果标签中存在冒号,ansible 无法模板化
- java - 如何使用 POI 更新特定单元格而不修改同一行的任何其他数据?
- python - 从 pyspark 中的 json 列表中提取值
- mysql - 我想链接phpmyadmin数据库中不同表中的两行
- apache-superset - 在哪里可以找到事件日志中事件的文档?
- visual-studio-code - vs-code:如何从命令行打开当前 WSL 文件夹 *as wsl*?
- junit - 我可以在我的测试类中覆盖 quarkus application.properties 值吗?