如何获取其中包含选定文本的行?例如:
选定的行将是 1、2、3 和 4(0 是第一行)
我怎样才能得到这样的代码:
For Each line as string(or integer) in textbox1."SelectedLines"
'Do something here for each line
Next
谢谢
我认为您正在寻找 SelectedText 属性。(在 C# 中)
foreach(string line in textBox1.SelectedText.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
{
//dostuffhere
}
(在我尝试 VB 时)
Dim splitter(1) As String
splitter(0) = Environment.NewLine
For Each y As String In TextBox1.SelectedText.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
//do stuff here
Next
从字面上看,您需要找到行号,即使只选择了第 1 行和第 4 行的部分内容。执行以下操作:
If RichTextBox1.SelectionLength > 0 Then
Dim firstLine As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim lastLine As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength)
For line As Integer = firstLine To lastLine
Dim txt = RichTextBox1.Lines(line)
' do something...
Next
End If