对于选定行中的每一行(vb.net)

时间:2010-01-28 22:07:07

标签: .net vb.net textbox lines selected

如何获取其中包含所选文本的行? 例如: alt text

选定的行将是1,2,3和4(0是第一行)

我怎样才能获得如下代码:

For Each line as string(or integer) in textbox1."SelectedLines"
  'Do something here for each line
Next

由于

2 个答案:

答案 0 :(得分:3)

我认为您正在寻找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 :(得分:2)

从字面上看,您需要找到行号,即使只选择了第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