![]() |
||||
Introduction This article explains how to control the state (enabled, disabled, invisible) of an add-in command. More Information Commands (EnvDTE.Command class) of an add-in are not visible elements (buttons are), so they can only be in one of two states: enabled or disabled. Buttons (CommandBarButton class) of an add-in are visible elements, that are created from commands using the Command.AddControl method. Therefore, a button can be in one of three states: enabled, disabled (but visible) or invisible. The state of a button is controlled through the state of the command that created it. A command can create buttons on several commandbars, such as on a toolbar and on a context menu, and the state of all those buttons are controlled by the command. That means that you don't have to use the CommandBarButton.Enabled property. Note: buttons can be also checked/unchecked if they are intended to show a status rather than to perform an action when clicked. To see how to create such buttons see HOWTO: Create a checked/unchecked command in a Visual Studio add-in. The state of a command is defined in the EnvDTE.vsCommandStatus enum, whose values must be combined as follows:
The vsCommandStatus.vsCommandStatusLatched value is used to create checked/unchecked buttons (see article above), the value vsCommandStatus.vsCommandStatusNinched value is rarely used and the vsCommandStatus.vsCommandStatusUnsupported value is used to denote that the command does not belong to the add-in. An add-in can be in one of two states:
The code below shows how to create an add-in with three permanent buttons on the "Standard" toolbar: one of the is always enabled, and the others are enabled only if a solution is loaded in the IDE; otherwise one of the buttons is disabled and the other is invisible. Unfortunately, there are a couple of bugs in the AddNamedCommand function and the code does not work as expected:
Imports System Imports Microsoft.VisualStudio.CommandBars Imports Extensibility Imports EnvDTE Imports EnvDTE80 Public Class Connect Implements Extensibility.IDTExtensibility2 Implements IDTCommandTarget Private Const STATUS_INVISIBLE As vsCommandStatus = _
vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusInvisible
Private Const STATUS_UNSUPPORTED As vsCommandStatus = vsCommandStatus.vsCommandStatusUnsupported
Private Const STATUS_DISABLED As vsCommandStatus = vsCommandStatus.vsCommandStatusSupported
Private Const STATUS_ENABLED As vsCommandStatus = _
vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusEnabled
Private Const NAME_INVISIBLE_WHEN_NO_SOLUTION_LOADED As String = "InvisibleWhenNoSolutionLoaded" Private Const NAME_DISABLED_WHEN_NO_SOLUTION_LOADED As String = "DisabledWhenNoSolutionLoaded" Private Const NAME_ALWAYS_ENABLED As String = "AlwaysEnabled" Private applicationObject As DTE Private addInInstance As AddIn Private myInvisibleWhenUnsupportedCommandBarControl As CommandBarControl Private myDisabledWhenUnsupportedCommandBarControl As CommandBarControl Private myAlwaysEnabledCommandBarControl As CommandBarControl 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 standardCommandBar As CommandBar
Dim commandBars As CommandBars
Dim myCommand As Command = Nothing
Dim commands As Commands
Try applicationObject = CType(application, DTE)
addInInstance = CType(addInInst, AddIn)
Select Case connectMode Case ext_ConnectMode.ext_cm_UISetup commandBars = DirectCast(applicationObject.CommandBars, CommandBars) standardCommandBar = commandBars.Item("Standard")
commands = applicationObject.Commands myCommand = commands.AddNamedCommand(addInInstance, _
NAME_INVISIBLE_WHEN_NO_SOLUTION_LOADED, NAME_INVISIBLE_WHEN_NO_SOLUTION_LOADED, "", True, _
59, New Object() {ContextGuids.vsContextGuidSolutionExists}, STATUS_INVISIBLE)
myInvisibleWhenUnsupportedCommandBarControl = DirectCast(myCommand.AddControl( _
standardCommandBar, 1), CommandBarControl)
myCommand = commands.AddNamedCommand(addInInstance, _
NAME_DISABLED_WHEN_NO_SOLUTION_LOADED, NAME_DISABLED_WHEN_NO_SOLUTION_LOADED, "", True, _
59, New Object() {ContextGuids.vsContextGuidSolutionExists}, STATUS_DISABLED)
myDisabledWhenUnsupportedCommandBarControl = DirectCast(myCommand.AddControl( _
standardCommandBar, 1), CommandBarControl)
myCommand = commands.AddNamedCommand(addInInstance, _
NAME_ALWAYS_ENABLED, NAME_ALWAYS_ENABLED, "", True, 59, Nothing, STATUS_ENABLED)
myAlwaysEnabledCommandBarControl = DirectCast(myCommand.AddControl( _
standardCommandBar, 1), CommandBarControl)
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
End Sub Public Sub OnStartupComplete(ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnStartupComplete
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 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.StartsWith(addInInstance.ProgID & ".") Then
handled = True
System.Windows.Forms.MessageBox.Show("Command " & cmdName & " 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 = vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then Select Case cmdName Case addInInstance.ProgID & "." & NAME_INVISIBLE_WHEN_NO_SOLUTION_LOADED If IsSolutionLoaded() Then
statusOption = STATUS_ENABLED
Else
statusOption = STATUS_UNSUPPORTED
End If
Case addInInstance.ProgID & "." & NAME_DISABLED_WHEN_NO_SOLUTION_LOADED If IsSolutionLoaded() Then
statusOption = STATUS_ENABLED
Else
statusOption = STATUS_DISABLED
End If
Case addInInstance.ProgID & "." & NAME_ALWAYS_ENABLED statusOption = STATUS_ENABLED End Select End If End Sub Private Function IsSolutionLoaded() As Boolean Dim result As Boolean = False If applicationObject.Solution.IsOpen Then
result = True
End If
Return result End Function End Class Related articles
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
| Copyright © 2000-2013 MZTools Software. All Rights Reserved. Legal Notice |