Visual studio 要粘贴的Visual Studio宏类似于String.Format
例如,我希望能够剪切/复制像“{0}”这样的字符串 然后我想选择一些代码,比如“Hello,World”,然后调用一个宏,它将导致“Hello,World” 你怎么能这么做 更新:我为什么要这样做 我可以只做一个宏或快捷方式,向选择中添加一些特定的内容,如标记。然而,我的想法是在飞行中创建任何类型的“环绕”粘贴行为 我经常粘贴字段或属性列表。所以我从别的地方得到Visual studio 要粘贴的Visual Studio宏类似于String.Format,visual-studio,macros,Visual Studio,Macros,例如,我希望能够剪切/复制像“{0}”这样的字符串 然后我想选择一些代码,比如“Hello,World”,然后调用一个宏,它将导致“Hello,World” 你怎么能这么做 更新:我为什么要这样做 我可以只做一个宏或快捷方式,向选择中添加一些特定的内容,如标记。然而,我的想法是在飞行中创建任何类型的“环绕”粘贴行为 我经常粘贴字段或属性列表。所以我从别的地方得到 PersonID FirstName LastName 作为一个例子,我知道我想把它们设置为 FieldName = dataRow
PersonID
FirstName
LastName
作为一个例子,我知道我想把它们设置为
FieldName = dataRow("FieldName").Value
使用我的magic宏,我可以选择以下选项并按CTRL+C将其放入剪贴板:
{0} = dataRow("{0}").Value
然后我所要做的就是一行一行地应用我的魔法粘贴。定义一个宏,在选定的文本周围添加“strong”标记不是更好吗?然后,您可以将其指定为Ctrl+B或其他类型 必须同时选择文本块和调用宏两次对我来说太难了 (也许你需要解释一下你为什么要这样做)有趣的小项目
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module StringFormatModule
Private clipText As String
Public Property ClipboardText() As String
Get
RunThread(AddressOf GetClipboardText)
Return clipText
End Get
Set(ByVal value As String)
clipText = value
RunThread(AddressOf CopyToClipboard)
End Set
End Property
Private Function RunThread(ByVal fct As Threading.ThreadStart)
Dim thread As New Threading.Thread(fct)
thread.ApartmentState = Threading.ApartmentState.STA
thread.Start()
thread.Join()
End Function
Private Sub GetClipboardText()
clipText = My.Computer.Clipboard.GetText()
End Sub
Private Sub CopyToClipboard()
My.Computer.Clipboard.SetText(clipText)
End Sub
Sub FormatSelectedTextWithCopiedText()
Dim formatString As String
formatString = ClipboardText
Dim token As String
Dim selectedText As TextSelection
selectedText = DTE.ActiveDocument.Selection
token = selectedText.Text
selectedText.Text = String.Format(formatString, token)
End Sub
End Module
我借用了剪贴板代码
这确实有效。我在一个文本文件上测试了它,将格式化字符串复制到剪贴板(ctrl-c),突出显示要格式化的文本,然后运行宏(我只是在宏资源管理器中双击它,但您可以创建一个键盘快捷键)。我使用&,而不是{0}。将宏指定给Ctrl+Q,您就可以全部设置好了
' Wraps the current selection with the specified text. Use the & character as the anchor for the selected text.
Public Sub WrapSelection()
Dim selection As TextSelection = DirectCast(DTE.ActiveDocument.Selection, TextSelection)
DTE.UndoContext.Open("Wrap Selection")
Try
Dim sInput As String = InputBox("Wrap(&&, state)")
If Len(sInput) > 0 Then
Dim sContent As String = selection.Text
Dim iStart As Integer = InStr(sInput, "&") - 1
Dim iEnd As Integer = InStrRev(sInput, "&")
selection.Insert(sInput.Substring(0, iStart) + sContent + sInput.Substring(iEnd), vsInsertFlags.vsInsertFlagsContainNewText)
'selection.Insert(sInput.Substring(iEnd), vsInsertFlags.vsInsertFlagsInsertAtEnd)
End If
Catch ex As Exception
DTE.UndoContext.SetAborted()
MsgBox(ex.Message)
Finally
'If an error occured, then need to make sure that the undo context is cleaned up.
'Otherwise, the editor can be left in a perpetual undo context
DTE.UndoContext.Close()
End Try
End Sub
@AMissico,这不是WriteMyEntireProgramForMe.com,它是这样的,不完整的代码片段展示了如何做一些事情的基本原理,没有错误处理和其他各种各样的“良好程序员实践”是标准,留给读者/提问者作为练习。