473,383 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Question about character counting

Hello Everyone,

I'm building an application and need to keep track of the amount of
text in a textbox to make sure the user knows how many characters they
have left to type. I've set the textbox's max length to 120 and that
works fine. Now, I have a label that displays characters remaining in
the textbox. For each keypress I am decrementing the counter (and
label value) by one and when the user presses the backspace key, I am
incrementing it by one. The problem is that when the user presses a
key combination (like shift+letter to capitialize something) it is
considered two kepresses (which is right, but unwanted for counting
reasons).

Can someone tell me how to NOT COUNT the pressing of modification keys
like shift, alt, etc? Here is the code I'm using:

Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Back And labelCharactersRemaining.Text <
120 Then
labelCharactersRemaining.Text =
labelCharactersRemaining.Text + 1
Else
labelCharactersRemaining.Text =
labelCharactersRemaining.Text - 1
End If
End Sub

I am sure it's something simple. I just can't figure it out.

Thanks!
Anthony Papillion
Jun 27 '08 #1
4 1257
Anthony,

Why counting as in my idea this gives you the same result?

\\\
myTextBox.Text.Lenght
///

Cor
Jun 27 '08 #2
On Mon, 14 Apr 2008 21:01:09 -0700 (PDT), "Anthony P."
<pa*******@gmail.comwrote:
>Hello Everyone,

I'm building an application and need to keep track of the amount of
text in a textbox to make sure the user knows how many characters they
have left to type. I've set the textbox's max length to 120 and that
works fine. Now, I have a label that displays characters remaining in
the textbox. For each keypress I am decrementing the counter (and
label value) by one and when the user presses the backspace key, I am
incrementing it by one. The problem is that when the user presses a
key combination (like shift+letter to capitialize something) it is
considered two kepresses (which is right, but unwanted for counting
reasons).

Can someone tell me how to NOT COUNT the pressing of modification keys
like shift, alt, etc? Here is the code I'm using:

Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Back And labelCharactersRemaining.Text <
120 Then
labelCharactersRemaining.Text =
labelCharactersRemaining.Text + 1
Else
labelCharactersRemaining.Text =
labelCharactersRemaining.Text - 1
End If
End Sub

I am sure it's something simple. I just can't figure it out.

Thanks!
Anthony Papillion
Don't try to count keystrokes, just use the number of chars in the
textbox.

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs)
Handles TextBox2.TextChanged
labelCharactersRemaining.Text = (MaxChars -
TextBox2.Text.Length).ToString()
End Sub

It appears that you are running with Option Strict Off (because you
are adding and subtracting a numeric value from the textbox Text
property). That is a bad idea because it can hide bugs.
Jun 27 '08 #3
Thanks to everyone who replied. I'd completely forgotten about
the .length property. Used it and it works flawlessly (of course).

Anthony
Jun 27 '08 #4
You probably figured this out already. Just keep it simple and get the
length of the string in the box on each keypress. Don't overcomplicate
programming. It will save you ulcers!

Mike

On Mon, 14 Apr 2008 21:01:09 -0700 (PDT), in
microsoft.public.dotnet.languages.vb "Anthony P." <pa*******@gmail.com>
wrote:
>Hello Everyone,

I'm building an application and need to keep track of the amount of
text in a textbox to make sure the user knows how many characters they
have left to type. I've set the textbox's max length to 120 and that
works fine. Now, I have a label that displays characters remaining in
the textbox. For each keypress I am decrementing the counter (and
label value) by one and when the user presses the backspace key, I am
incrementing it by one. The problem is that when the user presses a
key combination (like shift+letter to capitialize something) it is
considered two kepresses (which is right, but unwanted for counting
reasons).

Can someone tell me how to NOT COUNT the pressing of modification keys
like shift, alt, etc? Here is the code I'm using:

Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Back And labelCharactersRemaining.Text <
120 Then
labelCharactersRemaining.Text =
labelCharactersRemaining.Text + 1
Else
labelCharactersRemaining.Text =
labelCharactersRemaining.Text - 1
End If
End Sub

I am sure it's something simple. I just can't figure it out.

Thanks!
Anthony Papillion
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

74
by: Michael | last post by:
As if we needed another string reversal question. I have a problem with the following code, that I believe should work. int StringReverse(char* psz) { char *p = psz; char *q = psz +...
26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
8
by: Brent Lievers | last post by:
Greetings, I have a question about parsing a fixed-width integer from a string. Lines of data are being read from a file having a very strict column-delimited format. In my example below,...
41
by: newby2c | last post by:
Greetings all, Here is another incredibly silly question. I feel foolish asking it, but i've no other choice. I was going through K&R2 and at section 1.5.2 (Character Counting), I decided to try...
5
by: Matt | last post by:
Alright, so I'm a little confused here...what exactly does this do? I've run it and it doesn't display anything, so I've typed some things into it, to see if it'd do something then, but to no...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.