Dim selection As TextSelection = DTE.ActiveDocument.Selection'定义 TextSelection 对象
selection.StartOfLine()'移动到当前光标所在行的起始位置
Dim keyword = selection.Text.Trim()'取得整行文本
'获取当前选择首点所在行
Dim endLine As Integer
endLine = selection.TopPoint.Line
selection.NewLine()'插入一行空白行
selection.LineUp()'将光标移回到新空白行
selection.GotoLine(startLine) '将光标位置跳至某行的起始位置
selection.MoveToLineAndOffset(startLine,10 )'将光标跳至某行的起始位置+10个字符处
selection.LineDown(True, 18) '将选择位置向下移18行,true表示将选择范围进行扩展,选择的起始位置不变,终止位置下移18行
selection.CharRight(True,5) ’与上面类似,将选择范围向右扩展5个字符
'VB语法输入多段文本只能用" & Chr(13) & _ 连接的方法
'在选择范围中进行文本替换
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentSelection
DTE.Find.FindWhat = "dvxxxdv"
DTE.Find.ReplaceWith = keyword
DTE.Find.Execute()
DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument '定义搜索范围,注意:默认是vsFindTargetCurrentDocumentSelection,只在当前选中文本中查找,vsFindTargetCurrentDocument表示全文查找
DTE.Find.Backwards = True '向上还是向下,默认居然是向上
DTE.Find.Action = vsFindAction.vsFindActionFind
DTE.Find.FindWhat = "{"
DTE.Find.Execute()
MsgBox(selection.Text.Trim())'弹出提示框
实例:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module SkModule
'sk 2014.8.27 快速输入string.Format
Sub SQLFormat()
ActiveDocument.Selection.Text = "string.Format(@""select "",AisLogical.curUser);"
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.CharLeft(False, 23)
End Sub
'sk 2016.1.8将DataGridview控件加上两个事件替代selectionchanged事件
Sub DvAddFunc()
Dim selection As TextSelection = DTE.ActiveDocument.Selection
'选择整行,注意此时光标焦点落在下一行开始处
selection.SelectLine()
'取得整行文本
Dim keyword = selection.Text.Trim()
selection.Text = ""
'插入一行空白行
selection.NewLine()
'将光标移回到新空白行
selection.LineUp()
Dim startLine As Integer
'获取selection末端行号
startLine = selection.BottomPoint.Line
'VB语法输入多段文本只能用" & Chr(13) & _连接的方法
'注意在输入过程中VS环境可能会自动将某些看似语法错误的表达式替换成貌似正确的表达式,可能跟插入文本原文不一致
selection.Text = "//将下面两行剪切到该Dv控件所在窗体的Designer.cs文件中的InitializeComponent()函数末尾" & Chr(13) & _
"this.dvxxxdv.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dvxxxdv_CellClick);" & Chr(13) & _
"this.dvxxxdv.KeyUp += new System.Windows.Forms.KeyEventHandler(this.dvxxxdv_KeyUp);" & Chr(13) & _
"public void zMySelectionChanged_dv()" & Chr(13) & _
"{" & Chr(13) & _
"if (dvxxxdv.SelectedRows.Count == 0) return;" & Chr(13) & _
"}" & Chr(13) & _
"private void dvxxxdv_CellClick(object sender, DataGridViewCellEventArgs e)" & Chr(13) & _
"{" & Chr(13) & _
"if (e.RowIndex < 0) return;//如果不是单击列表头" & Chr(13) & _
"zMySelectionChanged_dv();" & Chr(13) & _
"}" & Chr(13) & _
"" & Chr(13) & _
"private void dvxxxdv_KeyUp(object sender, KeyEventArgs e)" & Chr(13) & _
"{" & Chr(13) & _
"if (e.KeyData == Keys.Down || e.KeyData == Keys.Up)" & Chr(13) & _
"zMySelectionChanged_dv(); " & Chr(13) & _
"}" & Chr(13)
'将选择起始位置重新设回文本插入前的位置
selection.GotoLine(startLine)
selection.LineDown(True, 18)
'在选择范围中进行文本替换
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentSelection
DTE.Find.FindWhat = "dvxxxdv"
DTE.Find.ReplaceWith = keyword
DTE.Find.Execute()
End Sub
'sk 2016.1.9将选择的范围加上#region结构
Sub AddRegion()
Dim selection As TextSelection = DTE.ActiveDocument.Selection '定义TextSelection 对象
Dim startLine As Integer
startLine = selection.TopPoint.Line
Dim endLine As Integer
endLine = selection.BottomPoint.Line
selection.GotoLine(startLine) '将光标位置跳至某行的起始位置
selection.Text = "#region" + Chr(13)
selection.GotoLine(endLine + 1) '将光标位置跳至某行的起始位置
selection.EndOfLine()
selection.NewLine()
selection.Text = "#endregion"
End Sub
'sk 2016.1.10将当前光标所在模块加上#region结构
Sub RegionOnBlock()
Dim selection As TextSelection = DTE.ActiveDocument.Selection '定义TextSelection 对象
Dim startline As Integer
startline = -1
Dim endline As Integer
endline = -1
Dim currentline As Integer = selection.ActivePoint.Line
selection.EndOfDocument() '将选择行移到最末一行
Dim documentlines As Integer = selection.ActivePoint.Line '整篇文档总行数
selection.GotoLine(currentline) '此时光标会停留在行首,如果加上,true参数整行将选择,选择的起始位置保持在行首和末尾
Dim kuohaoleft = 0 '从初始位置到当前位置共出现过几次{
selection.SelectLine()
selection.CharLeft(True)
Dim tx As String = selection.Text.Trim()
If (tx = "{") Then
kuohaoleft = 1
startline = selection.ActivePoint.Line
ElseIf (tx.IndexOf("{") >= 0) Then
MsgBox("光标所在行的'{'没有单独占一行,请重新选择行")
Return
Else
Do While kuohaoleft <= 0
'以下行为上移整行选择,选择的起始位置在行首和末尾,如果没有第句,选择末端会停留在下一行,也可用selection.GotoLine(xxline,true)实现
selection.LineUp()
selection.SelectLine()
selection.CharLeft(True)
tx = selection.Text.Trim()
If (tx = "{") Then
kuohaoleft += 1
ElseIf (tx = "}") Then
kuohaoleft -= 1
End If
If (selection.ActivePoint.Line = 1) Then Exit Do
Loop
If (kuohaoleft = 1) Then
startline = selection.ActivePoint.Line
Else
MsgBox("从当前位置以上没有出现单独一行的'{'")
Return
End If
End If
'此时光标停留在初始位置往上真实的第一个{处
kuohaoleft = 1 '其实运行到这里肯定kuohaoleft=1
Do While (kuohaoleft > 0)
selection.LineDown()
selection.SelectLine()
selection.CharLeft(True)
tx = selection.Text.Trim()
If (tx = "{") Then
kuohaoleft += 1
ElseIf (tx = "}") Then
kuohaoleft -= 1
End If
If (selection.ActivePoint.Line = documentlines) Then Exit Do
Loop
If (kuohaoleft = 0) Then
endline = selection.ActivePoint.Line
Else
MsgBox("从当前位置以下没有出现单独一行的'}'")
Return
End If
If (endline > startline And startline > 0) Then
selection.GotoLine(startline)
selection.EndOfLine()
selection.Text = Chr(13) + "#region"
selection.GotoLine(endline)
selection.EndOfLine()
selection.Text = Chr(13) + "#endregion"
'将光标放置到起始{行的末尾
selection.GotoLine(startline)
selection.EndOfLine()
End If
End Sub
End Module