【问题标题】:VB.NET equivalent of CommandBars [duplicate]VB.NET 等效的 CommandBars [重复]
【发布时间】:2017-05-10 15:20:40
【问题描述】:

我在 VBA 中有这段代码(以简化版本显示):

Sub TheMenu()
    Dim Obj As CommandBar
    Set Obj = Application.CommandBars.Add(Position:=msoBarPopup, MenuBar:=False, Temporary:=True)
    Obj.Controls.Add(Type:=msoControlButton).Caption = "Button1"
    Obj.Controls.Add(Type:=msoControlButton).Caption = "Button2"
    Obj.ShowPopup
End Sub

我希望在 VB.NET 中做一些等效的东西(意思是“看起来相似并且用途相似”,我不需要更多)。你知道有什么方法吗?

我在 VS2015 中使用,一个使用 .NET 框架 4.6.1 的“Windows 窗体应用程序”项目。

【问题讨论】:

  • 我已经成功地使用了与 VSTO 几乎完全相同的方法,除了语法上的变化。即没有SetType:=MsoControlType.msoControlButton 等...(其实我用的是Controls.Add(1),我不记得是否有特定原因)。
  • 所有这些类型在我的视觉工作室中都是未知的......你使用了什么导入?
  • Microsoft.Office.CoreMicrosoft.Office.InteropMicrosoft.Office.Interop.Excel。但是该项目是通过 VSTO 创建的,如果这些是自动添加的,则 IDR。几年前就这样做了,但后来“回到”VBA :)。我还在项目中看到Microsoft.Office.Tools.CommonMicrosoft.Office.Tools.Excel 和其他一些东西,我猜是 VSTO 添加了它们,因为该项目是作为 Workbook 项目创建的。
  • 投反对票。根本不清楚的是你打算做什么。 VBA 代码嵌入在 Excel 中运行。您是在询问是否在 VB.NET 中(在 VSTO/Excel 解决方案中)进行等效操作,还是在询问“在用 VB.NET 编写的 {insert UI framework here} 应用程序中具有类似于上下文菜单的内容”? - 根据您是在制作 VSTO/Excel 解决方案、WinForms 应用程序还是 WPF 应用程序,答案WILDLY会有所不同。
  • @A.S.H 这些导入似乎都没有实现任何目标......我的代码是否在您这边编译(显然除了“集合”)

标签: vb.net vba winforms excel


【解决方案1】:

您可以在此处按照以下示例进行操作:

Public Class Connect
   Implements Extensibility.IDTExtensibility2
   Implements IDTCommandTarget

   Private Const MY_COMMAND_NAME As String = "MyCommand"

   Private applicationObject As EnvDTE.DTE
   Private addInInstance As EnvDTE.AddIn
   Private myStandardCommandBarControl As CommandBarControl
   Private myToolsCommandBarControl As CommandBarControl
   Private myCodeWindowCommandBarControl As CommandBarControl
   Private myTemporaryToolbar As CommandBar
   Private myTemporaryCommandBarPopup As CommandBarPopup

   Public Sub OnConnection(ByVal application As Object, ByVal connectMode _
      As Extensibility.ext_ConnectMode, ByVal addInInst As Object, _
      ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection

      Dim myCommand As Command
      Dim standardCommandBar As CommandBar
      Dim menuCommandBar As CommandBar
      Dim toolsCommandBar As CommandBar
      Dim codeCommandBar As CommandBar
      Dim toolsCommandBarControl As CommandBarControl
      Dim myCommandBarButton As CommandBarButton
      Dim position As Integer

      Try

         applicationObject = CType(application, EnvDTE.DTE)
         addInInstance = CType(addInInst, EnvDTE.AddIn)

         Select Case connectMode

            Case ext_ConnectMode.ext_cm_AfterStartup, ext_ConnectMode.ext_cm_Startup

               ' Try to retrieve the command, just in case it was already created
               Try
                  myCommand = applicationObject.Commands.Item(addInInstance.ProgID & "." & "MyCommand")
               Catch
               End Try

               ' Add the command if it does not exists
               If myCommand Is Nothing Then

                  myCommand = applicationObject.Commands.AddNamedCommand(addInInstance, _
                     "MyCommand", "MyCommand", "Executes the command for MyAddin", True, 59, Nothing, _
                     vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusEnabled)

               End If

               ' Retrieve some built-in command bars
               standardCommandBar = applicationObject.CommandBars.Item("Standard")
               menuCommandBar = applicationObject.CommandBars.Item("MenuBar")
               toolsCommandBar = applicationObject.CommandBars.Item("Tools")
               codeCommandBar = applicationObject.CommandBars.Item("Code Window")

               ' Add a button to the built-in "Standard" toolbar
               myStandardCommandBarControl = myCommand.AddControl(standardCommandBar, _
                  standardCommandBar.Controls.Count + 1)

               myStandardCommandBarControl.Caption = MY_COMMAND_NAME

               ' Change the button style, which must be done casting the control to a button
               myCommandBarButton = DirectCast(myStandardCommandBarControl, CommandBarButton)
               myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon

               ' Add a button to the built-in "Tools" menu
               myToolsCommandBarControl = myCommand.AddControl(toolsCommandBar, toolsCommandBar.Controls.Count + 1)
               myToolsCommandBarControl.Caption = MY_COMMAND_NAME

               ' Add a button to the built-in "Code Window" context menu
               myCodeWindowCommandBarControl = myCommand.AddControl(codeCommandBar, codeCommandBar.Controls.Count + 1)
               myCodeWindowCommandBarControl.Caption = MY_COMMAND_NAME

               ' Add a new toolbar with a button on it
               myTemporaryToolbar = applicationObject.CommandBars.Add("MyTemporaryToolbar", _
                  MsoBarPosition.msoBarTop, System.Type.Missing, True)

               ' Change the button style, which must be done casting the control to a button
               myCommandBarButton = DirectCast(myCommand.AddControl(myTemporaryToolbar), CommandBarButton)
               myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon

               ' Make visible the toolbar
               myTemporaryToolbar.Visible = True

               ' Calculate the position of a new command bar popup by the "Tools" menu
               toolsCommandBarControl = DirectCast(toolsCommandBar.Parent, CommandBarControl)
               position = toolsCommandBarControl.Index + 1

               ' Add a new command bar popup with a button on it
               myTemporaryCommandBarPopup = DirectCast(menuCommandBar.Controls.Add( _
                  MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, _
                  position, True), CommandBarPopup)

               myTemporaryCommandBarPopup.CommandBar.Name = "MyTemporaryCommandBarPopup"
               myTemporaryCommandBarPopup.Caption = "My menu"
               myCommand.AddControl(myTemporaryCommandBarPopup.CommandBar)
               myTemporaryCommandBarPopup.Visible = True

         End Select

      Catch e As System.Exception
         System.Windows.Forms.MessageBox.Show(e.ToString)
      End Try

   End Sub

   Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
      ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection

      Try

         If Not (myStandardCommandBarControl Is Nothing) Then
            myStandardCommandBarControl.Delete()
         End If

         If Not (myCodeWindowCommandBarControl Is Nothing) Then
            myCodeWindowCommandBarControl.Delete()
         End If

         If Not (myToolsCommandBarControl Is Nothing) Then
            myToolsCommandBarControl.Delete()
         End If

         If Not (myTemporaryToolbar Is Nothing) Then
            myTemporaryToolbar.Delete()
         End If

         If Not (myTemporaryCommandBarPopup Is Nothing) Then
            myTemporaryCommandBarPopup.Delete()
         End If

      Catch e As System.Exception
         System.Windows.Forms.MessageBox.Show(e.ToString)
      End Try

   End Sub

   Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnStartupComplete(ByRef custom As System.Array) Implements _
      Extensibility.IDTExtensibility2.OnStartupComplete
   End Sub

   Public Sub Exec(ByVal cmdName As String, ByVal executeOption As vsCommandExecOption, _
      ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec

      handled = False

      If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then

         If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then

            handled = True
            System.Windows.Forms.MessageBox.Show("Command executed.")

         End If

      End If

   End Sub

   Public Sub QueryStatus(ByVal cmdName As String, ByVal neededText As vsCommandStatusTextWanted, _
      ByRef statusOption As vsCommandStatus, ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus

      If neededText = EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then

         If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
            statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _
               vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
         Else
            statusOption = vsCommandStatus.vsCommandStatusUnsupported
         End If

      End If

   End Sub

End Class

【讨论】:

  • CommandBar 在我的编辑器中不是已知类型。也许有一个“进口”要添加?谢谢!
  • 命令栏和工具栏从 Visual Studio .NET IDE 的加载项变为。你在用吗?
  • 如果回答,我使用 Visual Studio 2015
  • 当然...好吧,这个例子适用于旧版本,(这个 [Microsoft Visual Studio .NET 2002 Enterprise Developer, Microsoft Visual Studio .NET 2002 Professional Edition, Microsoft Visual Studio .NET 2003 Enterprise架构师,Microsoft Visual Studio .NET 2003 Enterprise Developer,Microsoft Visual Studio .NET 2003 Professional Edition])
猜你喜欢
  • 2012-10-08
  • 2011-01-29
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 2016-04-25
相关资源
最近更新 更多