Add-ins - Enumerating Selected ControlsGet List of Controls Selected on a Form | | In an add-in, how do I get a list of the controls selected by the developer in the active forms designer? I also would like to copy the selected controls to the clipboard. Can I do that? This article shows you how to do both of these functions.
I will use several objects from the System.ComponentModel namespace for the code in this article. First, I create an instance of IDesignerHost interface. This provides an interface for controlling components (controls) on the active forms designer from the add-in. To see more documentation on the IDesignerHost interface click here.
Next, I will use the host object to create an ISelectionService interface to access the selection of controls. The ISelectionService provides an interface for a forms designer to select components. To see more documentation on the ISelectionService, click here.. Assuming that the developer has selected at least one control, on the form, the code will create an array of the controls names, and finally copy the selected controls to the clipboard.
Figure 1 shows the skeleton of a Class that copies controls and code behind the controls to the clipboard. This class is taken from NetCommander available at this site. The method, CopySelectedControls, is designed to show you only how to enumerate and copy the controls.
Imports System.Windows.Forms
Imports EnvDTE
Imports Extensibility
Imports System.Text
Imports System.ComponentModel
Imports System.ComponentModel.Design
Public Class CCopyControlsAndCode
Private oVB As EnvDTE.DTE ' applicationObject of the IDE
Friend Function CopySelectedControls( _
ByRef oFrm As frmCopyControlsAndCode) _
As Boolean
'` Copy code for all selected events.
'` 1) Get list of the selected events by creating
'' and using a IDesignerHost object.
'' Copy the selected controls to the clipboard.
Dim aComps() As String
Dim j As Integer
Dim IC As IComponent
Dim c As Component
Dim iCmpCount As Integer
Try
' get a list of the selected components
' create an instance of the forms designer object
Dim fdHost As IDesignerHost
fdHost = CType(oVB.ActiveWindow.Object, IDesignerHost)
' get an ISelectionService interface to acces the selected controls
Dim sel As ISelectionService
sel = CType(fdHost.GetService(Type.GetType _
("System.ComponentModel.Design.ISelectionService,System")), _
System.ComponentModel.Design.ISelectionService)
If sel.GetSelectedComponents.Count = 0 Then
MsgBox("No controls selected", vbExclamation)
Exit Function
End If
Dim cmp As Component
ReDim aComps(sel.GetSelectedComponents.Count - 1)
' build array of control names
For Each cmp In sel.GetSelectedComponents
aComps(i) = cmp.Site.Name
Next
' aComps() now has the list of control names
' now destroy the designer object as we no
' longer need it.
cmp = Nothing
sel = Nothing
fdHost = Nothing
' copy the controls to the clipboard
oVB.ExecuteCommand("Edit.Copy")
Return True
Catch ex As System.Exception
MsgBox(ex.ToString())
Return False
End Try
End Function
End Class
|
Top of Page
|