Home All Groups Group Topic Archive Search About

Insert text into textbox at caret?

Author
26 Jun 2006 4:16 PM
Ben R.
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

Author
26 Jun 2006 5:07 PM
Terry
Look at the 'SelectionStart' property.
--
Terry


Show quoteHide quote
"Ben R." wrote:

> 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
Author
26 Jun 2006 5:50 PM
Ben R.
Thanks!

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

worked beautifully...

-Ben

Show quoteHide quote
"Terry" wrote:

> Look at the 'SelectionStart' property.
> --
> Terry
>
>
> "Ben R." wrote:
>
> > 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
Author
26 Jun 2006 7:41 PM
Herfried K. Wagner [MVP]
"Ben R." <benr@newsgroup.nospam> schrieb:
> 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"
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
29 Jun 2006 12:23 PM
Ben R.
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "Ben R." <benr@newsgroup.nospam> schrieb:
> > 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"
> ///
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>