Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text upon Return in Multi-line Text Box 2

Status
Not open for further replies.

tekrobg

Programmer
Oct 12, 2004
42
US
I am new to programming and VB.net. In a multi-line text box, how can I have a hyphen (-) automatically put in at the beginning of a new line each type the user hits return. Basically, I'm using the hyphen as a bullet. I just want it to be automatic.

Thanks for any help.
 
in the keypressed event check to see if enter was pressed, if it was, then clear the keycode and add a controlchars.cr and "-" to the text box.

-Rick

----------------------
 
I tried the following, but it didn't work right.

vb
Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
If e.KeyChar = Convert.ToChar(13) Then
TextBox7.Text = "- "
End If
End Sub
/vb

I don't think I know what you mean when you said "clear the keycode." Also, the way I tried it replaced all the text I typed and put the cursor at the beginning of the line before the -.

Thanks for any help.
 
Code:
Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            TextBox1.Text &= "- " & ControlChars.CrLf
            TextBox1.SelectionStart = TextBox1.Text.Length
        End If
End Sub

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thanks. I altered it a bit to get exactly what I wanted, but it was your help that got me there.
 
I just noticed one more little flaw regarding the above code. I am using this in a multi-line text box. After the text box is full and needs a vertical scroll bar, then every time the user presses return, the vertical scroll bar scrolls up to the top. The cursor stays in the correct position at the bottom, but you can't see it and have to scroll down every time you press return. How do I keep the scroll bar at the bottom of the text box. I searched for a thread, but did not find a solution.

Thanks for more help.
 
I just modified Christiaan's code a little bit. This should make it stay at the bottom.

Code:
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
   If e.KeyChar = Convert.ToChar(13) Then
       e.Handled = True
       TextBox3.AppendText(ControlChars.CrLf)
       TextBox3.AppendText("-")
   End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top