获取文本在textbox中location的范围
时间: 2024-04-09 07:33:25 浏览: 85
要获取文本在TextBox控件中的位置范围,可以使用TextBox控件的GetCharIndexFromPosition和GetLineFromCharIndex方法来实现。
首先,使用GetCharIndexFromPosition方法获取文本中指定位置的字符索引。可以将鼠标点击事件的坐标作为参数传递给该方法。
```csharp
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
int charIndex = textBox1.GetCharIndexFromPosition(e.Location);
// 使用charIndex进行需要的操作
}
```
然后,可以使用GetLineFromCharIndex方法获取该字符索引所在的行号。
```csharp
int lineIndex = textBox1.GetLineFromCharIndex(charIndex);
```
最后,可以使用TextBox控件的GetFirstCharIndexFromLine和GetLineLength方法获取该行文本的起始索引和长度。
```csharp
int lineStartIndex = textBox1.GetFirstCharIndexFromLine(lineIndex);
int lineLength = textBox1.GetLineLength(lineIndex);
```
这样,你就可以获取到文本在TextBox控件中的位置范围,即起始索引lineStartIndex和长度lineLength。
阅读全文
相关推荐


















