Text Max Length Programmatically | Tested on WinPhone APP






4.50/5 (4 votes)
Choose your text maximum length programmatically and show a message whenever the length = the maximum length
Introduction
I know that the title is a bit weird because you can do such a thing without the need to write a line of code, just by going to the properties related to the TextBox
and change the MaxLength
to whatever you want.
Honestly, if you are a user (a lazy one like me, haha) I prefer a message shows and tells me that I wrote the maximum number of characters I am allowed to write, I don't want to spend time thinking why I am not able to add more characters anymore, and this is the point of my tip/trick.
Using the Code
First, if you are thinking that it's a condition that will show a message if the length = the maxlength, then you are right.
I just used simple codes to make it so clear and easy to be understandable.
Public Variables
A string s
that will contain the text which is written in the TextBox
.
The code is as follows:
public string s;
Public s As String
Public Constants
An integer
called max
, it's the maximum number of characters which you are able to add to the TextBox
.
The code is as follows:
public const int max = 5; // You can write only 5 chars in this case
Public Const max As Integer = 5 'You can write only 5 chars in this case
Methods
There is only one method:
count
: The method that will count the characters and show the message when the condition istrue
.
Code
The count
method's code is so simple, it's just a condition to check the length of the text:
public void count()
{
// variable l = the length of the TextBox
int l = T.Text.Length;
c.Text = l + "/" + max; // It will show you the changing of length
if (l == max)
{ s = T.Text; }
else if (l > max)
{ MessageBox.Show("You cannot add more characters .", "Error", MessageBoxButton.OK);
T.Text = s; T.Select(l, l + 1); }
}
Public Sub count()
Dim l As Integer = T.Text.Length()
c.Text = l.ToString + "/" + max.ToString
If l = max Then
s = T.Text
ElseIf l > max Then
MessageBox.Show("You cannot add more characters .", "Error", MessageBoxButton.OK)
T.Text = s
T.Select(l, l + 1)
End If
End Sub
Now after we did create our count
method, we must call it from an event that repeats quickly. Let's go with TextChanged
event.
The code is as follows:
private void T_TextChanged(object sender, TextChangedEventArgs e)
{
count();
}
Private Sub T_TextChanged(sender As Object, e As TextChangedEventArgs)
count()
End Sub
Now, when you execute your APP and start writing whatever you want, the label will be counting your text's length and comparing it with the const
we made from the beginning (max
).
Finally, when the text's length value is the same as the maximum length we did choose, a message will appear:
Notices
- You can change the
MaxLength
to whatever you want. - You can use these algorithms on the type of project you like (WPF, ASP.NET, WinForms, WinPhone, etc.)
- The label & the alert message presented in this project are good enough for users to watch over and take care about the length of their text.
- It's better than using the basic
MaxLength
property.