获取文本在textbox中location的范围
时间: 2024-04-09 08:33:25 浏览: 27
要获取文本在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。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)