Insert text into textbox at caret?

G

Guest

Hi there,

I'm designing a winforms app in VB.NET 2.0 and have a textbox control and
also some toolbar buttons. I want to have it such that when the user clicks a
toolbar button, some predefined text - such as the date - is inserted into
the textbox at the point where the caret is. (It's assumed that the user will
be typing and then click the toolbar button.) From my research the textbox
control doesn't seem to support any method of querying the position of the
cursor. I've found documentation on pulling in the function "GetCaretPos"
from user32.dll and using that, but those examples seem to return coordinates
of the caret and not a linear position in the text. Is there a good way
anyone can think of to achieve this?

Thanks...

-Ben
 
G

Guest

Thanks!

textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, "Test");

worked beautifully...

-Ben
 
H

Herfried K. Wagner [MVP]

Ben R. said:
I'm designing a winforms app in VB.NET 2.0 and have a textbox control and
also some toolbar buttons. I want to have it such that when the user
clicks a
toolbar button, some predefined text - such as the date - is inserted into
the textbox at the point where the caret is.

\\\
Me.TextBox1.SelectedText = "Hello World"
///
 
G

Guest

This is much easier than what I had devised using the selectionstart
property, which required me to do some math for where to insert and where to
place the cursor after. Thanks!


-Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top