473,544 Members | 263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Count Characters on a Textbox (but while typing).

6 New Member
Please I would appreciate if you can help me with the correct code to countdown characters left in a TextBox.
Here is my code but doesn't work on real time, I mean, when I type in TxtBox2.

TextBox3 is where I put the counter.
TextBox2 is where it's the text.


Private Sub TextBox3_Change ()
Me.TextBox3 = 250 - Len(Me.TextBox2 .Text) & " Characters Left"
If Len(Me.TextBox2 .Text) = 250 Then
MsgBox "No more characters allowed!"
End If
End Sub


Thanks in advance.
Nov 27 '14 #1
11 3887
Ajay Bhalala
119 New Member
I am not sure, but I think you can use timer control for count the characters while typing.
Nov 27 '14 #2
6 New Member
In other forum I've just read this but not sure if it will work:


Me.LblCharsLeft .Caption = 255 - Len(Trim(Me.Tex tBox)) & " characters left"


I need a counter on real time, so when I type in TextBox 2 need in someplace a counter that tells how many character left in TextBox2.

Thanks again.
Nov 27 '14 #3
Ajay Bhalala
119 New Member
I am sorry. You don't have to use the timer control.

You can use the following code...

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  3.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  4.         Dim a As Object
  5.         a = Len(Me.TextBox1.Text)
  6.         Label1.Text = (250 - a) & "Characters Left"
  7.     End Sub
  8. End Class
For use the above code You don't need the counter. Just simply write the above code(bloded text only) in the textchanged event of textbox in which you write the text.

I have use the one textbox and one label. I have display the characters left in the label.
Nov 27 '14 #4
Ajay Bhalala
119 New Member
I have use the one textbox in which user have to enter the characters and one label which displays the how many characters left.

Before you write this code, you must set the "maxlength" property of textbox to 250 or any value you want.

I have tried it. It works. If you write 1 character, in label text will be displayed "249 characters left"
Nov 27 '14 #5
6 New Member
Hi Ajay,
Thank you so much for your quick answer, but it didn't work.
I'm not very expertise with VBA and I don't understand what means all you put in the line #3 in brackets.

As I said, my text box where I have to write the text, it's called "textbox2".
If I can put the counter in the top right of the box, much nicer, otherwise let me know the best option.

When I ran your code this message appeared:
"Run time error 91
Object variable or with block variable not set"

Thanks again.
Nov 27 '14 #6
Ajay Bhalala
119 New Member
You mean to say, you are writing the text in "TextBox2" right?
Nov 27 '14 #7
6 New Member
Please give me the details on Label as well. Because I have added a Label1 and didn't work.

Thank you Ajay.
Nov 27 '14 #8
6 New Member
This is what I copied from your advice:

Private Sub TextBox2_Change ()
Dim a As Object
a = Len(Me.TextBox2 .Text)
Label.Text = (250 - a) & "Characters Left"
End Sub

Private Sub Label_Click()
Label.Text = (250 - a) & "Characters Left"
End Sub


and it's not working.
Nov 27 '14 #9
Ajay Bhalala
119 New Member
First you create a new project.
Then take a windows form.
Then take a one textbox in this form.
Then take a one label.

Select the textbox. The property of textbox will display.
Find the maxlength property. and write the value "250".

Double click on the textbox.

Code editor window will open. and looks like below

Public Class Form1

Private Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox1.TextCh anged

End Sub
End Class

You have to write the following code after the line "Private Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox1.TextCh anged"

Expand|Select|Wrap|Line Numbers
  1. Dim a As Object
  2.         a = Len(Me.TextBox1.Text)
  3.         Label1.Text = (250 - a) & " Characters Left"
Do as I directed. You can understand your problem and hope can understand what I want to say.

and don't change the name of the textbox and label.
Nov 27 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: faizankhan666 | last post by:
Complete homework Question is this: Create a program that uses a do-while loop to count the number of char. (not including whitespace) entered by the user.The count should end when it encounters a # character in the input. First of all i want to tell u that I am a bigenner.Just started C++ for a month. And that is what i have tried:
by: Jay | last post by:
I need to convert from a string a double that is followed by a scaling character (k means *1e3, M=*1e6, etc) then apply the scaling character. Example: "-1.345k #comment" I know roughly how to do this in C (using the %n in the sscanf format specifier to find the number of characters converted) , but how do I do it in C#? My problem is how...
by: Beany | last post by:
Hi, Can some please provide me with the code for counting records in a subform and displaying it in a textbox? Does this code go into the control source of the textbox? My subform is called 'Asset subform' within the form 'Form1'.. the textbox will be in the same form. Also im showing records in a subform by selecting a user from a...
by: Dean | last post by:
Hi, I have a table with non-unique identifiers. I need to take all the values with the same ID's and combine them into one field with a semicolon as a seperator. These values may exceed 255 characters. I then need to count the values in the cell and see if it adds up to 240 or more and then shorten that field by cutting off the excess and...
by: rajeshnrh74 | last post by:
Hi Gurus, I need one solution I'm using VB6, one of its Form contains a TextBox What i need is whatever characters I type in it that should be highlighted ex First when I type "a" -> "a" should be selected second when I type "b" -> "ab" should be selected third when I type "c" -> "abc" should be selected
by: cppman | last post by:
Hello, I am very new to C++ programming. I am trying to read a file with very large strings back to back, no commas or any other seperations between characters in a string and the size of the string is not known as well. I need to count how many characters totoal in a unknown size of a string. this is what I have so far: #include...
by: beary | last post by:
Since php seems to be able to do pretty much anything, is it possible for it to calculate the number of characters or words in a textarea as the user types? Eg. there is a 300 word limit, and as the user types, a box changes the number of words remaining to the user after each word is typed. Possible with php?
by: si_owen | last post by:
Hi Folks, I am trying to set up some code that will provide a character count for the text entered into a textbox on my form. I am coding in VB.Net using visual studio 2003 - web appliaction. I have sorted the VB code, which when execute via a command button it updates the label. Private Sub txtcomment_TextChanged(ByVal sender As...
by: AdamOnAccess | last post by:
Hi, While the user is typing into a text box, I need to display the length of the string they are typing. My code is... displayCountLabel.caption = len(me!fieldTheyAreTypingIn) Problem is that I can't seem to find an event that occurs while the user continuously types. I tried keydown and keyup and keypress. All of them fire on the first...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.