Insert string into TextBox

  • Thread starter Thread starter Allan Bredahl
  • Start date Start date
A

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
 
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";
 
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.
 
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.
 
rhaazy said:
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
 
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
 
 > 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 :)
 
Ignacio Machin ( .NET/ C# MVP ) wrote:





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
 
message
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.
 
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.
 
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;
 
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.
 
Jeff Johnson said:
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.
 
<script type="text/javascript">
function setCaret (textObj) {
if (textObj.createTextRange) {
textObj.caretPos = document.selection.createRange().duplicate();
}
}
function insertAtCaret (textObj, textFeildValue) {
if(document.all){
if (textObj.createTextRange && textObj.caretPos) {
var caretPos = textObj.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) ==
' ' ?textFeildValue + ' ' : textFeildValue;
}else{
textObj.value = textFeildValue;
}
}else{
if(textObj.setSelectionRange){
var rangeStart = textObj.selectionStart;
var rangeEnd = textObj.selectionEnd;
var tempStr1 = textObj.value.substring(0,rangeStart);
var tempStr2 = textObj.value.substring(rangeEnd);
textObj.value = tempStr1 + textFeildValue + tempStr2;
}else{
alert("This version of Mozilla based browser does not support
setSelectionRange");
}
}
}

</script>

<form id="form1" action="" onsubmit="" method="post"
enctype="text/plain">
<p>
<textarea name="tarea" rows="" cols=""
style="width:300px;height:120px;"
onselect="setCaret(this);"
onclick="setCaret(this);"
onkeyup="setCaret(this);" >
Click in this textObj anywhere and then press the button below.
It will insert the text at the caret position.</textarea>
<br/><br/>
<input type="text" name="textfield" style="width:220px;"
value="Mozilla 1.4"/>
<br/>
<input type="button" value="insert at caret"

onclick="insertAtCaret(this.form.tarea,this.form.textfield.value);"
id="Button1"/>
</p>
</form>
 
i need the mouse to be focused at the end of inserted tag

The code works fine to insert text into text area, but i need to focus the mouse at the end of inserted tag.

thanks
 
Back
Top