|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Insert string into TextBoxHi 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 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> 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 OUT YOURSELF"; "rhaazy" <rha***@gmail.com> wrote in message Ironically, you've completely failed to understand his relatively simple 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"; question. rhaazy wrote:
> On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote: I think:>> 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"; textBox1.Text = "I don't understand what 'at the cursor position' means"; is much better ! Arne On Dec 5, 9:22 pm, Arne Vajhøj <a***@vajhoej.dk> wrote:
Show quoteHide quote > rhaazy wrote: And as useless :)> > 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 Ignacio Machin ( .NET/ C# MVP ) wrote:
Show quoteHide quote > On Dec 5, 9:22 pm, Arne Vajhøj <a***@vajhoej.dk> wrote: Just as useless for the original poster.>> 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 :) But rhaazy could benefit from it. Arne On Dec 6, 10:31 am, Arne Vajhøj <a***@vajhoej.dk> wrote:
Show quoteHide quote > Ignacio Machin ( .NET/ C# MVP ) wrote: IMO the OP had a valid question. Maybe it was simple but not unheard> > > > > 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 of around here Ignacio Machin ( .NET/ C# MVP ) wrote:
Show quoteHide quote > On Dec 6, 10:31 am, Arne Vajhøj <a***@vajhoej.dk> wrote: The original posters question was fine.>> 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 Arne "Allan Bredahl" <al***@bredahl.org> wrote in message Look at the SelectedText property.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 On Dec 5, 4:00 pm, "Allan Bredahl" <al***@bredahl.org> wrote: Textbox.SelectionStart shoudl do it> 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 "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac***@gmail.com> wrote in No, he shouldn't have to mess with SelectionStart at all, because he doesn't 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 want to set a NEW cursor position, he wants to use the CURRENT cursor position. 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. > > > 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. > > > > > >
Show quote
Hide quote
"Bogdan" <Bog***@discussions.microsoft.com> wrote in message It is completely UNNECESSARY code*. The SelectedText property is all you 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; 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.
Show quote
Hide quote
"Jeff Johnson" wrote: Wow. See, this is why people should repley to these posts. Not only are > "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. > > > 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.
Other interesting topics
Accessing file extended properties in .NET
Package access confusion Action of a button to a text box? Dealing with different types of objects in an ArrayList A little off topic For Microsoft Support Personnel - Where are you? vectors and maps in C# Threading with server What's a proper way to organize the packages? GridView Paging/Sorting without using the ObjectDataSource |
|||||||||||||||||||||||