Visual Studio宏的当前行

时间:2010-03-29 13:47:59

标签: .net visual-studio winforms macros

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

我将使用这样的功能:

 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

2 个答案:

答案 0 :(得分:4)

您可以使用以下内容:

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

答案 1 :(得分:3)

从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);