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!

Insert line where cursor is, not at bottom 1

Status
Not open for further replies.

tekrobg

Programmer
Oct 12, 2004
42
US
I have a text box that automatically puts a bullet and space (- ) at the beginning of the next line when the user hits enter. It works fine, but now I discovered that if the user inputs many lines of data, then decides to add some data in the middle of all that data and the cursor is placed at the beginning or end of a line and hits enter, the (- ) bullet is put at the bottom of the text box instead of where the cursor is. I want it to be able to insert the bullet (- ) on a new line where the cursor is.

Here's my code:

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
e.Handled = True
TextBox7.AppendText(ControlChars.CrLf)
TextBox7.AppendText("- ")
End If
End Sub
/vb

Thanks for any help.
 
There must be a better way but this seems to work...it basically inserts the crlf and hyphen at the caret point (selectionstart property)

Dim l_iCursorPos As Integer = TextBox1.SelectionStart
If e.KeyChar = Convert.ToChar(13) Then
e.Handled = True
With TextBox1
.Text = .Text.Substring(0, l_iCursorPos) & vbCrLf & "-" & .Text.Substring(l_iCursorPos)
.SelectionStart = l_iCursorPos + 3
End With

End If
 
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
TextBox7.SelectedText = ControlChars.CrLf & _
"- "
End If
End Sub


Compare Code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top