Home All Groups Group Topic Archive Search About

Insert string into TextBox

Author
5 Dec 2008 9:00 PM
Allan Bredahl
Hi all

I want to insert at string into the text of a TextBox at the cursor
position.


How do you do that witout using the clipboard paste function

Thanks in advance

Allan Bredahl

Author
5 Dec 2008 9:24 PM
rhaazy
On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:
> Hi all
>
> I want to insert at string into the text of a TextBox at the cursor
> position.
>
> How do you do that witout using the clipboard paste function
>
> Thanks in advance
>
> Allan Bredahl

textBox1.Text = "WHAT IS SO HARD ABOUT THIS THAT YOU CAN'T FIGURE IT
OUT YOURSELF";
Are all your drivers up to date? click for free checkup

Author
5 Dec 2008 9:35 PM
proxyuser
"rhaazy" <rha***@gmail.com> wrote in message
news:d32e9004-03c8-46c5-a2aa-ba103dafaa38@w24g2000prd.googlegroups.com...
On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:

>textBox1.Text = "WHAT IS SO HARD ABOUT THIS THAT YOU CAN'T FIGURE IT
>OUT YOURSELF";

Ironically, you've completely failed to understand his relatively simple
question.
Author
6 Dec 2008 2:22 AM
Arne_Vajhøj
rhaazy wrote:
> On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:
>> I want to insert at string into the text of a TextBox at the cursor
>> position.
>>
>> How do you do that witout using the clipboard paste function
>
> textBox1.Text = "WHAT IS SO HARD ABOUT THIS THAT YOU CAN'T FIGURE IT
> OUT YOURSELF";

I think:

textBox1.Text = "I don't understand what 'at the cursor position' means";

is much better !

Arne
Author
6 Dec 2008 1:16 PM
Ignacio Machin ( .NET/ C# MVP )
On Dec 5, 9:22 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
Show quoteHide quote
> rhaazy wrote:
> > On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:
> >> I want to insert at string into the text of a TextBox at the cursor
> >> position.
>
> >> How do you do that witout using the clipboard paste function
>
>  > textBox1.Text = "WHAT IS SO HARD ABOUT THIS THAT YOU CAN'T FIGURE IT
>  > OUT YOURSELF";
>
> I think:
>
> textBox1.Text = "I don't understand what 'at the cursor position' means";
>
> is much better !
>
> Arne


And as  useless :)
Author
6 Dec 2008 3:31 PM
Arne_Vajhøj
Ignacio Machin ( .NET/ C# MVP ) wrote:
Show quoteHide quote
> On Dec 5, 9:22 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
>> rhaazy wrote:
>>> On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:
>>>> I want to insert at string into the text of a TextBox at the cursor
>>>> position.
>>>> How do you do that witout using the clipboard paste function
>>  > textBox1.Text = "WHAT IS SO HARD ABOUT THIS THAT YOU CAN'T FIGURE IT
>>  > OUT YOURSELF";
>>
>> I think:
>>
>> textBox1.Text = "I don't understand what 'at the cursor position' means";
>>
>> is much better !
>
> And as  useless :)

Just as useless for the original poster.

But rhaazy could benefit from it.

Arne
Author
6 Dec 2008 5:19 PM
Ignacio Machin ( .NET/ C# MVP )
On Dec 6, 10:31 am, Arne Vajhøj <a***@vajhoej.dk> wrote:
Show quoteHide quote
> Ignacio Machin ( .NET/ C# MVP ) wrote:
>
>
>
> > On Dec 5, 9:22 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
> >> rhaazy wrote:
> >>> On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:
> >>>> I want to insert at string into the text of a TextBox at the cursor
> >>>> position.
> >>>> How do you do that witout using the clipboard paste function
> >>  > textBox1.Text = "WHAT IS SO HARD ABOUT THIS THAT YOU CAN'T FIGURE IT
> >>  > OUT YOURSELF";
>
> >> I think:
>
> >> textBox1.Text = "I don't understand what 'at the cursor position' means";
>
> >> is much better !
>
> > And as  useless :)
>
> Just as useless for the original poster.
>
> But rhaazy could benefit from it.
>
> Arne

IMO the OP had a valid question. Maybe it was simple but not unheard
of around here
Author
6 Dec 2008 6:04 PM
Arne_Vajhøj
Ignacio Machin ( .NET/ C# MVP ) wrote:
Show quoteHide quote
> On Dec 6, 10:31 am, Arne Vajhøj <a***@vajhoej.dk> wrote:
>> Ignacio Machin ( .NET/ C# MVP ) wrote:
>>> On Dec 5, 9:22 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
>>>> rhaazy wrote:
>>>>> On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:
>>>>>> I want to insert at string into the text of a TextBox at the cursor
>>>>>> position.
>>>>>> How do you do that witout using the clipboard paste function
>>>>  > textBox1.Text = "WHAT IS SO HARD ABOUT THIS THAT YOU CAN'T FIGURE IT
>>>>  > OUT YOURSELF";
>>>> I think:
>>>> textBox1.Text = "I don't understand what 'at the cursor position' means";
>>>> is much better !
>>> And as  useless :)
>> Just as useless for the original poster.
>>
>> But rhaazy could benefit from it.
>
> IMO the OP had a valid question. Maybe it was simple but not unheard
> of around here

The original posters question was fine.

Arne
Author
5 Dec 2008 9:25 PM
Jeff Johnson
"Allan Bredahl" <al***@bredahl.org> wrote in message
news:%23V6G8xxVJHA.4024@TK2MSFTNGP03.phx.gbl...

> I want to insert at string into the text of a TextBox at the cursor
> position.
>
>
> How do you do that witout using the clipboard paste function

Look at the SelectedText property.
Author
6 Dec 2008 3:10 AM
Ignacio Machin ( .NET/ C# MVP )
On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote:
> Hi all
>
> I want to insert at string into the text of a TextBox at the cursor
> position.
>
> How do you do that witout using the clipboard paste function
>
> Thanks in advance
>
> Allan Bredahl

Textbox.SelectionStart shoudl do it
Author
8 Dec 2008 2:35 PM
Jeff Johnson
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
message
news:4668cd5c-f1e7-425d-8eb1-e1bb8638f7c1@u14g2000yqg.googlegroups.com...

>> I want to insert at string into the text of a TextBox at the cursor
>> position.
>
>> How do you do that witout using the clipboard paste function

>Textbox.SelectionStart shoudl do it

No, he shouldn't have to mess with SelectionStart at all, because he doesn't
want to set a NEW cursor position, he wants to use the CURRENT cursor
position.
Author
8 Dec 2008 6:02 PM
Bogdan
int position = TextBox1.SelectionStart;

is how you get the cursor position in a text box.
You don't set the SelectionStart, you retrieve the value.

Show quoteHide quote
"Jeff Johnson" wrote:

> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
> message
> news:4668cd5c-f1e7-425d-8eb1-e1bb8638f7c1@u14g2000yqg.googlegroups.com...
>
> >> I want to insert at string into the text of a TextBox at the cursor
> >> position.
> >
> >> How do you do that witout using the clipboard paste function
>
> >Textbox.SelectionStart shoudl do it
>
> No, he shouldn't have to mess with SelectionStart at all, because he doesn't
> want to set a NEW cursor position, he wants to use the CURRENT cursor
> position.
>
>
>
Author
8 Dec 2008 6:21 PM
Bogdan
Here is the complete code.

        int pos = TextBox1.SelectionStart;
        string textToInsert = "-Text to Insert-";

        string newText =  TextBox1.Text.Substring (0, pos) +
                                   textToInsert +
                                   TextBox1.Text.Substring(pos,
txtFirstName.Text.Length - pos);

        TextBox1.Text = newText;



Show quoteHide quote
"Bogdan" wrote:

> int position = TextBox1.SelectionStart;
>
> is how you get the cursor position in a text box.
> You don't set the SelectionStart, you retrieve the value.
>
> "Jeff Johnson" wrote:
>
> > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in
> > message
> > news:4668cd5c-f1e7-425d-8eb1-e1bb8638f7c1@u14g2000yqg.googlegroups.com...
> >
> > >> I want to insert at string into the text of a TextBox at the cursor
> > >> position.
> > >
> > >> How do you do that witout using the clipboard paste function
> >
> > >Textbox.SelectionStart shoudl do it
> >
> > No, he shouldn't have to mess with SelectionStart at all, because he doesn't
> > want to set a NEW cursor position, he wants to use the CURRENT cursor
> > position.
> >
> >
> >
Author
8 Dec 2008 6:58 PM
Jeff Johnson
Show quote Hide quote
"Bogdan" <Bog***@discussions.microsoft.com> wrote in message
news:D8BFAC18-B11A-4CB8-AF55-CFE3FC9364E4@microsoft.com...

> Here is the complete code.
>
>        int pos = TextBox1.SelectionStart;
>        string textToInsert = "-Text to Insert-";
>
>        string newText =  TextBox1.Text.Substring (0, pos) +
>                                   textToInsert +
>                                   TextBox1.Text.Substring(pos,
> txtFirstName.Text.Length - pos);
>
>        TextBox1.Text = newText;

It is completely UNNECESSARY code*. The SelectedText property is all you
need. The only time you would need to use code like above is if you wanted
to handle the situation of text already being selected in the control and
you didn't want to overwrite it. Then you'd have to decide whether you
wanted the new text inserted at the beginning of the selection or after it.

But if you DO want selected text to be replaced, then consider these two
examples:

1) The text box contains "The quick brown fox" and the word "brown" is
selected (highlighted, if you prefer).

textBox1.SelectedText = "red"

The text box now contains "The quick red fox".

2) The text box contains the same text but nothing is selected. The cursor
(or in proper Windows terminology, the caret) is right before the "b" in
"brown."

textBox1.SelectedText = "red"

The text box now contains "The quick redbrown fox". It's that simple.

For reference, under the covers, the EM_REPLACESEL message is being sent to
the control.



*I'm not saying it's wrong; it's just the looooong way.
Author
9 Dec 2008 2:46 PM
Bogdan
Show quote Hide quote
"Jeff Johnson" wrote:

> "Bogdan" <Bog***@discussions.microsoft.com> wrote in message
> news:D8BFAC18-B11A-4CB8-AF55-CFE3FC9364E4@microsoft.com...
>
> > Here is the complete code.
> >
> >        int pos = TextBox1.SelectionStart;
> >        string textToInsert = "-Text to Insert-";
> >
> >        string newText =  TextBox1.Text.Substring (0, pos) +
> >                                   textToInsert +
> >                                   TextBox1.Text.Substring(pos,
> > txtFirstName.Text.Length - pos);
> >
> >        TextBox1.Text = newText;
>
> It is completely UNNECESSARY code*. The SelectedText property is all you
> need. The only time you would need to use code like above is if you wanted
> to handle the situation of text already being selected in the control and
> you didn't want to overwrite it. Then you'd have to decide whether you
> wanted the new text inserted at the beginning of the selection or after it.
>
> But if you DO want selected text to be replaced, then consider these two
> examples:
>
> 1) The text box contains "The quick brown fox" and the word "brown" is
> selected (highlighted, if you prefer).
>
> textBox1.SelectedText = "red"
>
> The text box now contains "The quick red fox".
>
> 2) The text box contains the same text but nothing is selected. The cursor
> (or in proper Windows terminology, the caret) is right before the "b" in
> "brown."
>
> textBox1.SelectedText = "red"
>
> The text box now contains "The quick redbrown fox". It's that simple.
>
> For reference, under the covers, the EM_REPLACESEL message is being sent to
> the control.
>
>
>
> *I'm not saying it's wrong; it's just the looooong way.
>
>
>
Wow.  See, this is why people should repley to these posts.  Not only are
you helping someone, but there's aalways a chance you can learn somethineg. 
I learned something.

I guess I will be using the short way from now on.

Thanks Jeff.

Bookmark and Share