【问题标题】:Visual Studio Macro to Paste similar to String.Format类似于 String.Format 粘贴的 Visual Studio 宏
【发布时间】:2009-03-19 22:08:30
【问题描述】:

例如,我希望能够剪切/复制像“{0}”这样的字符串。

然后我想选择一些代码,例如“Hello, World”,然后调用一个宏,这将导致“Hello, World”。

你怎么能这样?

更新:我为什么要这样做?

我可以制作一个宏或快捷方式来添加特定的东西,比如一个 标签到选择中。但是,我的想法是动态创建任何类型的“环绕”粘贴行为。

我经常粘贴字段或属性列表。所以我从其他地方得到

PersonID
FirstName
LastName

作为一个例子,我知道我想将它们设置为

FieldName = dataRow("FieldName").Value

使用我的魔术宏,我可以选择以下内容并按 CTRL+C 将其放入我的剪贴板:

{0} = dataRow("{0}").Value

那么我所要做的就是一行一行地涂上我的魔法膏。

【问题讨论】:

    标签: visual-studio macros


    【解决方案1】:

    有趣的小项目。

    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
    

    我借用了剪贴板代码from here

    这确实有效。我在文本文件上对其进行了测试,将格式字符串复制到剪贴板(ctrl-c),突出显示要格式化的文本,然后运行宏(我只是从宏资源管理器中双击它,但你可以制作键盘快捷键) .

    【讨论】:

    • @AMissico,这不是 WriteMyEntireProgramForMe.com,它是 SO,以及显示如何做某事的基础知识的不完整代码 sn-ps,没有错误处理和其他各种“良好的程序员实践”是常态,留给读者/提问者作为练习。
    【解决方案2】:

    定义一个在所选文本周围添加“强”标签的宏不是更好吗?然后你可以将它分配给 Ctrl+B 或其他。

    必须选择两个文本块并调用两次宏对我来说似乎太辛苦了。

    (也许你需要解释为什么你想这样做)

    【讨论】:

      【解决方案3】:

      我使用 & 而不是 {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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 2017-08-29
        • 1970-01-01
        • 2013-09-02
        • 2022-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多