count lines of text

  • Thread starter Thread starter Paul Mars
  • Start date Start date
P

Paul Mars

I need to limit multiline textbox to 3 lines of text and if there are less
then 3 lines when leaving, add empty line holders. How can I do this??

Thanks,
paul
 
* "Paul Mars said:
I need to limit multiline textbox to 3 lines of text and if there are less
then 3 lines when leaving, add empty line holders. How can I do this??

'TextBox1.Lines.Length' will return the number of lines.
 
* "Paul Mars said:
What event should I put limit to 3 lines under??

Maybe in the 'TextChange' event. You can use an ErrorProvider to make
the user aware of invalid (longer) input.
 
I would count the number of characters that make up the three lines, place
that in the designer window as the max number of characters allowed, and
then on submit or whatever it is that you are doing count the number of
characters in the textbox and add characters as you see fit when it is less
than desired.
 
Hi Scorpion53061,
I would count the number of characters that make up the three lines, place
that in the designer window as the max number of characters allowed, and
then on submit or whatever it is that you are doing count the number of
characters in the textbox and add characters as you see fit when it is less
than desired.
Wouldn't that only work if the text box is using a non-proportional font for
the display (which one would seldom choose, as it looks like h*)?

-- Cindy
 
I want to stop the user, if s/he tries to start a 4th line. Not tell her/him
after the 4th and maybe more lines are entered.
 
yup.

Cindy M -WordMVP- said:
Hi Scorpion53061,

Wouldn't that only work if the text box is using a non-proportional font for
the display (which one would seldom choose, as it looks like h*)?

-- Cindy
 
Hi Paul,
I want to stop the user, if s/he tries to start a 4th line. Not tell her/him
after the 4th and maybe more lines are entered.

The answer from Herfried is in my eyes very correct.
Only what is a line, a line is a string in a textbox ended with the Enter
key (vbcrlf).

When you means visible lines in a textbox, than is that something else, you
can than thinking on measure the textlenght, but what you cannot measure as
far as I know is the part where the textbox gives a wordwrap.

The instruction would else be very simple something as
\\\
Private Sub TextBox1_TextChanged(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If (Me.TextBox1.Width * 3) < CInt(Graphics.FromHwnd(Me.Handle).MeasureString
_
(Me.TextBox1.Text, Me.TextBox1.Font).Width) - 1 then
' Do some action to tell the user that there are more than 3 lines
end if
end sub
///
But this fails because the fact there is a not measured LF (or CR I do not
know) in it.
And I did not see it either, because the textbox1.text gives a string back
without that.

Cor
 
Hi Paul,

While writing and sending the previous message I thought, maybe this is
possible and I think it reaches your wish (It is not ready just a peace of
example how you could do it, but not user friendly).

I hope this helps?

Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.WordWrap = False
End Sub
Private Sub TextBox1_TextChanged(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If Me.TextBox1.Lines.Length > 3 Then
MessageBox.Show("to many lines")
End If
Dim i As Integer
For i = 0 To TextBox1.Lines.Length - 1
If (Me.TextBox1.Width) <
CInt(Graphics.FromHwnd(Me.Handle).MeasureString _
(Me.TextBox1.Lines(i).ToString, Me.TextBox1.Font).Width) Then
MessageBox.Show("tolong")
End If
Next
End Sub
 
Herfried's answers works after "Enter" key AND the first character of the
forth line is keyed. At this point I can message the user and tab to the
next field, but I still have the forth line and one character in it. I can
not remove that last character, since this will trigger the event again.

P
 
Hi Paul,

Try it first yourself, and see also my other answer, if you not succeed,
message after a while here, than I will look to it tomorrow ok.

Cor
 
Sometimes 2nd to third triggers message. Sometimes each character on third
line triggers message. I understand what the code says, but it is not doing
that.
 
* "Paul Mars said:
I want to stop the user, if s/he tries to start a 4th line. Not tell her/him
after the 4th and maybe more lines are entered.

IMO that's not user-friendly. For example, if the user pastes some text
from the clipboard, he wants to edit it inside the textbox (for example,
removing lines). He cannot do that if the text in the textbox /must/
not be longer than n lines...
 
Hi Paul,
Herfried's answers works after "Enter" key AND the first character of the
forth line is keyed. At this point I can message the user and tab to the
next field, but I still have the forth line and one character in it. I can
not remove that last character, since this will trigger the event again.
In similar situations, I've known programmers to use a Boolean variable to
suppress the trigger event (i.e. it's set when the "Enter" code starts
executing, and if it has a certain value the event doesn't start this loop).

Couldn't you also use something like KeyPress to pick up when the Enter key
is used, and increment a variable? When it gets to 3, move the focus to the
next field (or whatever).

-- Cindy
 
Hi Paul,

For me it did work fine,

The only thing is that the user has to remove the last character now using
the backspace key and that it needs an textbox lostfocus event with the
same test to prevent that the user is leaving the textbox with errors in it.

Cor
 
Private Sub TextBox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(13) And TextBox1.Lines.Length >= 3 Then
e.Handled = True
Beep()
End If
End Sub
 
* "Jim Hughes said:
Private Sub TextBox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(13) And TextBox1.Lines.Length >= 3 Then
e.Handled = True
Beep()
End If
End Sub

Notice that this won't prevent the user from pasting longer text.
 
Back
Top