如何在多行文本框C#中获取选择的开始和结束索引

how to get start and end index of selection in multiline textbox C#

我有一个文本框,我想知道值的开始和结束索引,用户有 selected/Highlighted。

like,**here is** my textbox and its is some kind of data in the textbox.

粗体 "here is" 被用户选中。所以,我想要h和s的索引值。

可以从 属性 TextBox.SelectionStart 中检索起始索引。结束索引值可以计算为 TextBox.SelectionStart + TextBox.SelectionLength.

int selectionIndexStart = textBox1.SelectionStart;
int selectionIndexEnd = textBox1.SelectionStart + textBox1.SelectionLength;

其中 textBox1 的类型为 TextBox