Insert text into textbox at caret?

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Thanks!

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

worked beautifully...

-Ben
 
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"
///
 
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
 
Back
Top