如何才能得到它们的线都在他们选定的文本? 例如: “替代文字”

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

我怎样才能像代码:

For Each line as string(or integer) in textbox1."SelectedLines"
  'Do something here for each line
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

其他提示

我认为你正在寻找的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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top