How do implement a carriage return in a multiline textbox, so that everytime the users enter text it starts on a new line
Printable View
How do implement a carriage return in a multiline textbox, so that everytime the users enter text it starts on a new line
At the end of each line add a 'vbCrLf'.
Eg:
If you didn't put the vbCrLf in there, the result would be:Code:Text1.Text = "Hello World!" & vbCrLf
Text1.Text = Text1.Text & "I'm going home!"
"Hello World!I'm going home!"
But since we use vbCrLf the result is:
"Hello World!
I'm going home!"
Hope it helps.
I think he wants to add the new lines when the user enters text, not when the code adds text. How are you going to define a point when a user starts to enter text? Is it every time the text box loses focus and gets it back again?
Every time that event occurs, you can use this statement:
That will add a new line to the end of the text.Code:Text1.Text = Text1.Text & VbNewline
Private Sub Command1_Click()
'if you use the focus event, everytime
'you get focus you add a new blank line
'the same with the button but I think the
'button is the lesser evil
Text1.SetFocus
Text1.Text = Text1.Text & vbCrLf
Text1.SelStart = Len(Text1.Text)
'
End Sub
Thanks guys, but this is a textbox that allows the user to enter comments. So every time they start typing they should be on a new line. Should look something like this.
boy
girl
child
so on
The question is: When do you want it to go to a new line? Every word? Every time the textbox loses focus?
It depends on the user, I don't know how long or short a comment could be.
I'm not sure what it is, either. :rolleyes:
But try this:
Code:Private Sub Text1_GotFocus()
If Not Right(Text1.Text, 2) = vbNewLine Then Text1.Text = Text1.Text & vbNewLine
Text1.SelStart = Len(Text1.Text)
End Sub
You could try using a listbox, Eg:
I hope this helps you, though I'm not sure exactly what you need.Code:'LB1 is your listbox control
'cmdAdd is a commandbutton to add a comment to the list.
Private Sub cmdAdd_Click()
Dim sResult As String
sResult = InputBox("Please enter a comment:", "Add Comment", "")
If sResult = "" Then Exit Sub
LB1.AddItem sResult
End Sub