I am trying to validate a textbox on my form. I want to make sure I cover all possible things that go wrong such as: not entering anything and entering a number less than 5 digits. I set the textbox MaxLength=5, which works fine since if I do go over 5 digits, I don't even see them on the screen the program just keeps beeping since I already set the value to 5. But, I'm having trouble getting the messagebox to show if the user enters a number with less than 5 digits. I know this has something to do with the max length but I don't know how to fix this. Is it not possible to test a textbox for anything less than it's set max length?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace tester
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnEnter_Click(object sender, EventArgs e)
{
if (txtNumber.Text == "")
{
MessageBox.Show("Please enter your number ");
}
else if (txtNumber.MaxLength > 5)
MessageBox.Show("Please make sure account number is exactly 5 digits");
txtNumber.Focus();
}
}
}
*Note: I have seen various versions of this with using things like the digit method and keypress but I want to keep this as simple as possible and just use maxlength.