CJ7
CJ7

Reputation: 23275

How to insert text into TextBox at cursor point?

How can you insert text into a TextBox at the current cursor position?

Upvotes: 2

Views: 9241

Answers (4)

paul
paul

Reputation: 11

for some reason this site isn't letting me finish my code entry. Using my example will insert the text but it will be selected text after pasting it. To deselect the text and leave the cursor at the end of the pasted text you have to call a textbox1 click event like this. text1[underscore]click.

Upvotes: 0

paul
paul

Reputation: 11

If all you're trying to do is paste text at the current cursor location then do the following.

text1.seltext = "here's some text I'm pasting in" text1_click

The code above will paste your text at the current cursor position and then place the cursor at the end of the pasted text.

Upvotes: 1

CJ7
CJ7

Reputation: 23275

Text can be inserted at the current cursor position of a TextBox by setting SelText to a string:

TextBox1.SelText = "text to be inserted"

Upvotes: 4

salih0vicX
salih0vicX

Reputation: 1373

Let's assume your text box is named: txtTitle:

With txtTitle
   .SelStart = .SelLength 'SelStart will place cursor at the last selected character
End With

Example:

txtTitle.SelStart = 7 'This will place cursor after 7th character

EDIT: Just for the clarification: SelLength will return 0 if no character has been selected and than you can use SelStart to get current position. This is what you should test:

Dim iPos As Long
With txtTitle
  If .SelLength = 0 Then
    iPos = .SelStart
  Else
    iPos = .SelStart + .SelLength
  End If
  Debug.Print "The current cursor position in " & .Name & " is: " & iPos & " :-)"
End With

Upvotes: 1

Related Questions