【问题标题】:Current Line For Visual Studio MacrosVisual Studio 宏的当前行
【发布时间】:2010-03-29 13:47:58
【问题描述】:

如何从宏中读取当前行(光标所在的位置)的文本?

我要使用这样的功能:

 Public Sub AddTextToChangeLogFile()
    Dim textOnACurrentLine As ???
    textOnACurrentLine = ???

    If textOnACurrentLine.Text <> String.Empty Then
        Dim sw As New StreamWriter("C:\###\Changes.txt", True)
        sw.WriteLine(textOnACurrentLine + ". file: " + DTE.ActiveDocument.Name)
        sw.Close()
    End If
End Sub

【问题讨论】:

    标签: .net visual-studio winforms macros


    【解决方案1】:

    你可以使用类似的东西:

    Dim textOnACurrentLine As String
    DTE.ActiveDocument.Selection.StartOfLine(0)
    DTE.ActiveDocument.Selection.EndOfLine(True)
    textOnACurrentLine = DTE.ActiveDocument.Selection.Text
    

    【讨论】:

    • 这具有保持选中行的副作用。更好的选择是使用 na EditPoint
    【解决方案2】:

    从 Selection 中获取行索引并使用 EditPoint,如下所示:

    TextSelection text_selection = (TextSelection)m_DTE.ActiveDocument.Selection;
    int line_index = text_selection.ActivePoint.Line;
    TextDocument text_doc = (TextDocument)m_DTE.ActiveDocument.Object("");
    EditPoint edit_point = text_doc.CreateEditPoint();
    string line = edit_point.GetLines(line_index, line_index+1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-28
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      相关资源
      最近更新 更多