James May
James May

Reputation:

How do I find the position of a cursor in a text box? C#

I have a standard WinForms TextBox and I want to insert text at the cursor's position in the text. How can I get the cursor's position?

Thanks

Upvotes: 63

Views: 107783

Answers (9)

masfenix
masfenix

Reputation: 8016

You have to keep the SelectionStart property in a variable, and then when you press the button, shift the focus back to the TextBox. Then set the SelectionStart property to the one in the variable.

Upvotes: 2

Charles F
Charles F

Reputation: 31

To get the caret position when you click the mouse within the text of a TextBox, use the TextBox MouseDown event. Create a point using X and Y properties of the MouseEventArgs. The TextBox has a method called GetCharIndexFromPosition(point). Pass it the point and it returns the caret position. This works if you are using the mouse to determine where you want to insert the new text.

Upvotes: 2

Fragment
Fragment

Reputation: 1585

Here is my working realization, alowing to type only digits, with restoring last valid typing text position:

Xaml:

<TextBox
      Name="myTextBox" 
      TextChanged="OnMyTextBoxTyping" />

Code behind:

private void OnMyTextBoxTyping(object sender, EventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(myTextBox.Text, @"^[0-9]+$"))
    {
        var currentPosition = myTextBox.SelectionStart;
        myTextBox.Text = new string(myTextBox.Text.Where(c => (char.IsDigit(c))).ToArray());
        myTextBox.SelectionStart = currentPosition > 0 ? currentPosition - 1 : currentPosition;
    }
}

Upvotes: 4

Shafiq-Ur-Rehman
Shafiq-Ur-Rehman

Reputation: 340

int cursorPosition = textBox1.SelectionStart;
//it will extract your current cursor position where ever it is
//textBox1 is name of your text box. you can use one
//which is being used by you in your form

Upvotes: 1

Matt Hamilton
Matt Hamilton

Reputation: 204259

Regardless of whether any text is selected, the SelectionStart property represents the index into the text where you caret sits. So you can use String.Insert to inject some text, like this:

myTextBox.Text = myTextBox.Text.Insert(myTextBox.SelectionStart, "Hello world");

Upvotes: 96

David Morton
David Morton

Reputation: 16505

You want to check the SelectionStart property of the TextBox.

Upvotes: 16

Patrick
Patrick

Reputation: 51

All you have to do is this:

Double click the item (button, label, whatever) that will insert text into the document at the cursor. Then type this in:

richTextBox.SelectedText = "whatevertextyouwantinserted";

Upvotes: 5

MaDDoG
MaDDoG

Reputation: 260

James, it's pretty inefficient that you need to replace the whole string when you only want to insert some text at the cursor position.

A better solution would be:

textBoxSt1.SelectedText = ComboBoxWildCard.SelectedItem.ToString();

When you have nothing selected, that will insert the new text at the cursor position. If you have something selected, this will replace your selected text with the text you want to insert.

I found this solution from the eggheadcafe site.

Upvotes: 8

James May
James May

Reputation:

On what event would you suggest I record the variable? Leave?

Currently I have:

private void comboBoxWildCard_SelectedIndexChanged(object sender, EventArgs e)
{
    textBoxSt1.Focus();
    textBoxSt1.Text.Insert(intCursorPos, comboBoxWildCard.SelectedItem.ToString());

}

private void textBoxSt1_Leave(object sender, EventArgs e)
{
    intCursorPos = textBoxSt1.SelectionStart;
}

Recording on the Leave event is working but the text doesn't get inserted, am I missing something?

UPDATE: I needed textBoxSt1.Text =

textBoxSt1.Text = textBoxSt1.Text.Insert(intCursorPos, comboBoxWildCard.SelectedItem.ToString());

Thanks all.

Thanks

Upvotes: 2

Related Questions