Question about character counting

  • Thread starter Thread starter Anthony P.
  • Start date Start date
A

Anthony P.

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
 
Anthony,

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

\\\
myTextBox.Text.Lenght
///

Cor
 
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.
 
Thanks to everyone who replied. I'd completely forgotten about
the .length property. Used it and it works flawlessly (of course).

Anthony
 
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
 
Back
Top