如何在文本框中找到光标的位置? C#

时间:2022-09-28 16:34:53

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?

我有一个标准的WinForms TextBox,我想在文本中的光标位置插入文本。我怎样才能获得光标的位置?

Thanks

9 个解决方案

#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:

无论是否选择任何文本,SelectionStart属性都表示您所关注文本的索引。所以你可以使用String.Insert注入一些文本,如下所示:

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

#2


You want to check the SelectionStart property of the TextBox.

您想要检查TextBox的SelectionStart属性。

#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.

James,当你只想在光标位置插入一些文本时,需要替换整个字符串是非常低效的。

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.

我从eggheadcafe网站找到了这个解决方案。

#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


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?

在Leave事件上录制工作但文本没有插入,我错过了什么?

UPDATE: I needed textBoxSt1.Text =

更新:我需要textBoxSt1.Text =

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

Thanks all.

Thanks

#6


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

#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


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.

您必须将SelectionStart属性保留在变量中,然后在按下按钮时将焦点移回TextBox。然后将SelectionStart属性设置为变量中的属性。

#9


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.

要在TextBox文本中单击鼠标时获取插入位置,请使用TextBox MouseDown事件。使用MouseEventArgs的X和Y属性创建一个点。 TextBox有一个名为GetCharIndexFromPosition(point)的方法。将它传递给它并返回插入位置。如果您使用鼠标确定要插入新文本的位置,则此方法有效。

#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:

无论是否选择任何文本,SelectionStart属性都表示您所关注文本的索引。所以你可以使用String.Insert注入一些文本,如下所示:

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

#2


You want to check the SelectionStart property of the TextBox.

您想要检查TextBox的SelectionStart属性。

#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.

James,当你只想在光标位置插入一些文本时,需要替换整个字符串是非常低效的。

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.

我从eggheadcafe网站找到了这个解决方案。

#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


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?

在Leave事件上录制工作但文本没有插入,我错过了什么?

UPDATE: I needed textBoxSt1.Text =

更新:我需要textBoxSt1.Text =

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

Thanks all.

Thanks

#6


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

#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


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.

您必须将SelectionStart属性保留在变量中,然后在按下按钮时将焦点移回TextBox。然后将SelectionStart属性设置为变量中的属性。

#9


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.

要在TextBox文本中单击鼠标时获取插入位置,请使用TextBox MouseDown事件。使用MouseEventArgs的X和Y属性创建一个点。 TextBox有一个名为GetCharIndexFromPosition(point)的方法。将它传递给它并返回插入位置。如果您使用鼠标确定要插入新文本的位置,则此方法有效。