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

发布于 2024-08-19 16:48:21 字数 307 浏览 9 评论 0原文

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

所选行为 1、2,3 和 4(0 为第一行)

我怎样才能得到这样的代码:

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

谢谢

how can I get the lines which are have selected text in them?
For example:
alt text

The selected lines would be 1, 2,3 and 4 (0 being the first line)

How can I get to code like:

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-08-26 16:48:21

我认为您正在寻找 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

I think you're looking for the SelectedText property.
(in C#)

foreach(string line in textBox1.SelectedText.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
{
    //dostuffhere
}

(in my attempt at 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
甜警司 2024-08-26 16:48:21

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

Taking you literally, you need to find the line numbers, even though only parts of line 1 and 4 are selected. Do that as follows:

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文