【问题标题】:Getting the current line number using Visual Studio Macros?使用 Visual Studio 宏获取当前行号?
【发布时间】:2014-01-02 20:40:37
【问题描述】:

所以我查看了其他用户的问题,但无法具体找到我要查找的内容。我想做的很简单。我正在编写一个 Visual Studio 宏,并试图获取 TextSelection 所在的当前行号。就是这样,我的问题很简单。您如何获得当前选择所在的行号?任何帮助是极大的赞赏!

让任何阅读本文的人都清楚,我正在使用 VB 并正在编写 Visual Studio 宏。。 p>

【问题讨论】:

    标签: vb.net visual-studio macros visual-studio-macros


    【解决方案1】:

    请记住,TextSelection 可以跨越多行,因此可能存在一系列行。

    查看TextSelection 的文档(即我尚未对此进行测试),您应该能够执行以下操作:

    Dim mySelection As EnvDTE.TextSelection = ' however you get the active selection
    mySelection.TopPoint.Line ' gets the line of the top of the selection
    

    如果你想根据光标所在的位置(选择的顶部或底部)获取它,你可以试试这个:

    mySelection.ActivePoint.Line
    

    看起来 TextRanges 也可能有用,但听起来它仅用于框选择,因此可能不适用。

    mySelection.TextRanges.Item(0).Line
    

    【讨论】:

      【解决方案2】:

      可能有比这更好的方法,但我想到的第一个方法是这样的。

      首先,确保文件的最后一行是“xyz”

       Dim linenumber As Integer = 1
       dim mystring as string = ""
      
       Using myfile As New IO.StreamReader("C:/myfile")
      
               mystring = myfile.readline()
               while mystring <> "xyz"
      
                    linenumber += 1
                    messagebox.Show(mystring & " is on line " & linenumber)
               end while
       End Using
      

      所以如果 C:/myfile 的内容看起来像这样......

      馅饼

      然后你会得到输出......

      “我在 1 号线”

      “爱在第2行”

      “派在第 3 行”

      【讨论】:

      • 这实际上并没有解决如何在 Visual Studio 编辑器窗格中获取当前 TextSelection 行的问题。
      猜你喜欢
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多