1

I'm having a problem with the character count in a textbox.

If e.KeyChar = ChrW(13) Then tsCount.Text = "Character Count: " & tbText.Text.Count - 2

Basically I want to prevent the character count going up by 2 when the user creates a new line in the textbox. I've tried that code above, but didnt really work.

Also, in my program the user cannot create a new line without having any text in the textbox by using the code below:

If tbText.Text = String.Empty And e.KeyChar = ChrW(13) Then e.Handled = True

tsCount is a label, and tbText is my textbox. Is it possible to prevent it by going up by 2?

Greg
  • 55
  • 1
  • 1
  • 8
  • 1
    In a Windows environment, a new line consists of two characters (Cr + Lf; carriage return + line feed). Also see this question: http://stackoverflow.com/questions/1552749/difference-between-cr-lf-lf-and-cr-line-break-types – Styxxy Nov 01 '14 at 18:22

1 Answers1

1

You can remove all new line characters by using String.Replace(String, String) before performing the measurement of length.

Dim newText As String = tbText.Text.Replace(vbNewLine, String.Empty)
tsCount.Text = newText.Count
spongebob
  • 8,370
  • 15
  • 50
  • 83