tags:

views:

1273

answers:

5

How to paste text in textbox current cursor in window form ? not textbox1 += string

A: 
 textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, "Whatever");
Aziz
why inserted cursor move to begin textbox?
monkey_boys
how can cursor focus in insert after insert text
monkey_boys
set `textBox1.SelectionStart` to whatever position you want.
Aziz
+1  A: 
        var insertText = "Text";
        var selectionIndex = textBox1.SelectionStart;
        textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText);
        textBox1.SelectionStart = selectionIndex + insertText.Length;
MNZ
Two things: first of all, is that a direct code sample? In C#, the keyword var isn't used to create a variable, instead precise datatypes are used. Thus, you would use *string insertText = "Text"; int selectionIndex = textBox1.SelectionStart;*. 2nd, you don't need to define as textBox1.SelectionStart as its own variable, you can just call it directly, but all this does is eliminate one line of code, so not very important. – Maximz2005 0 secs ago
Maxim Zaslavsky
`var` is new in C# 3.5 (or something), it's a sort of automatic type inference, saving you some typing. See http://msdn.microsoft.com/en-us/library/bb383973.aspx
Svend
wow! i never knew that! so cool!
Maxim Zaslavsky
Anyway I don't see a point in using var when string will suffice.
devoured elysium
yeah, but it's another way, it's your choice, you can use it or not. ;)
MNZ
A: 

The best way to accomplish this is to use the TextBox.Text.Insert(int indexSelectionStart, string text). What this method does is insert text into the TextBox at the index you specify - it uses string string.insert(int startIndex, string value) as TextBox.Text is a string which we are going to insert text into at a specific point. You wish to insert text where the cursor/selector is, and to find that index, we can use TextBox.SelectionStart.

Let's say that your TextBox is named textBox1. This is what the code may look like, presuming that the text you wish to insert is stored in the string named strInsert.

string strInsert = "I am inserting this text.";
textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, strInsert);
Maxim Zaslavsky
A: 

This ensures that the cursor is at some position within the textbox, then inserts the text wherever the cursor is located.

        if (textBox1.CaretIndex <= 0)
        {

               textBox1.Focus();
  textBox1.Text = textBox1.Text.Insert(
                textBox1.CaretIndex, "Whatever");
        }
        else
        {
            textBox1.Text = textBox1.Text.Insert(
                textBox1.CaretIndex, "Whatever");
        }
A: 

The simple way would be

textBox1.Paste();

That replaces the current selection with the contents of the clipboard.

If you need to do it manually then it's a bit more work. Remember if you're "pasting" then you are "replacing" the current selection if there is one. So you need to handle that first. You'll need to save SelectionStart if you had a selection as removing the text will screw it up.

string newText = "your text";

int start = textBox1.SelectionStart;

bool haveSelection = textBox1.SelectionLength > 0;

string text = (haveSelection) ? textBox1.Text.Remove(start,textBox1.SelectionLength) : textBox1.Text;

textBox1.Text = text.Insert(start,newText);

if(haveSelection)
{
    textBox1.SelectionStart = start;
    textBox1.SelectionLength = newText.Length;
}

After you're done you'll want to invalidate the control to force it to redraw.

textBox1.Invalidate();
Mike