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

笑着哭i 提交于 2019-11-26 09:37:14

问题


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


回答1:


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");



回答2:


You want to check the SelectionStart property of the TextBox.




回答3:


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.




回答4:


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";



回答5:


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;
    }
}



回答6:


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




回答7:


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



回答8:


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.




回答9:


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.



来源:https://stackoverflow.com/questions/526540/how-do-i-find-the-position-of-a-cursor-in-a-text-box-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!