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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/526540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:47:21  来源:igfitidea点击:

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

c#cursor-position

提问by

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

谢谢

采纳答案by Matt Hamilton

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

无论是否选择了任何文本,SelectionStart属性都表示插入符号所在文本的索引。所以你可以使用String.Insert来注入一些文本,像这样:

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

回答by David Morton

You want to check the SelectionStartproperty of the TextBox.

你要检查SelectionStart的属性TextBox

回答by masfenix

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

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

回答by masfenix

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

谢谢

回答by MaDDoG

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 网站找到了这个解决方案。

回答by Patrick

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

回答by Shafiq-Ur-Rehman

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

回答by Charles F

To get the caret position when you click the mouse within the text of a TextBox, use the TextBoxMouseDownevent. Create a point using X and Y properties of the MouseEventArgs. The TextBoxhas 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.

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

回答by Fragment

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

这是我的工作实现,只允许输入数字,并恢复最后一个有效的输入文本位置:

Xaml:

Xml:

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