Click here to Skip to main content
15,121,891 members
Articles / Mobile Apps
Posted 31 May 2015


3 bookmarked

Text Max Length Programmatically | Tested on WinPhone APP

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
31 May 2015CPOL2 min read
Choose your text maximum length programmatically and show a message whenever the length = the maximum length


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


There is only one method:

  • count: The method that will count the characters and show the message when the condition is true.


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)
Private Sub T_TextChanged(sender As Object, e As TextChangedEventArgs)
   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:


  • 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alaa Ben Fatma
France France
No Biography provided

Comments and Discussions

-- There are no messages in this forum --