Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / Extensibility / July 2004

Tip: Looking for answers? Try searching our database.

How to detect selected control?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick Strahl [MVP] - 27 Jun 2004 03:11 GMT
Is there anyway using the Add-in model to detect the currently selected
control in the WinForm (or WebForm) designers?

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web

Carlos J. Quintero [MVP] - 28 Jun 2004 13:27 GMT
For Winforms you must have a System.Component.Model.Design.IDesignerHost
(retrieved from Window.Object) and then you request the ISelectionService:

 Dim objISelectionService As ISelectionService
 Dim objIServiceProvider As IServiceProvider

 Try
  objIServiceProvider = CType(objIDesignerHost, IServiceProvider)
  objISelectionService =
CType(objIServiceProvider.GetService(GetType(ISelectionService)),
ISelectionService)
 Catch objException As Exception
 End Try

Then the ISelectionService object has methods to set and get selected
components.

For WebForms it's another story. See the thread "Addin: Access ASPX Page
ProjectItem as Web.UI.Page object" in this group.

Signature

Carlos J. Quintero (Visual Developer - .NET MVP)

FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)

> Is there anyway using the Add-in model to detect the currently selected
> control in the WinForm (or WebForm) designers?
>
> +++ Rick ---
Rick Strahl [MVP] - 28 Jun 2004 21:28 GMT
Carlos,

Thanks for the pointers, but I'm having problems with this.

I'm trying to run this trhough a macro for the moment and here's what I got
so far:

Dim WinForm As EnvDTE.SelectedItem

WinForm = DTE.SelectedItems.Item(1)

Dim hostWin As IDesignerHost

hostWin = CType(DTE.ActiveWindow.Object, IDesignerHost)

Dim ServiceProvider As System.IServiceProvider

ServiceProvider = CType(hostWin, IServiceProvider)

Dim SelectionService As ISelectionService

' this fails

SelectionService = _

CType(ServiceProvider.GetService(GetType(ISelectionService)), _

ISelectionService)

Dim ctrl As Control

ctrl = SelectionService.PrimarySelection

MessageBox.Show(ctrl.Name)

Problem is trying to retrieve the SelectionService fails with an error that
QueryInterface failed on IServiceProvider. All the previous calls return COM
interface pointers, but it's hard to tell whether I'm looking at the right
thing or not since hte debugger can't show me the content. I know I'm
getting to the form document Ok, but after that I'm not sure...

I tried mucking around with IDesignerHost too and there are problems there
as well, so I'm guessing that it might not be returning the correct
reference to the form/document correctly in the first place. But I can't
figure out how to check that out since none of those interfaces return
anything that identifies what I'm looking at. Any ideas?

Here's what I'm trying to accomplish really: I need to get a reference to
the active control so that I can then use an external utlitity (via COM) to
retrieve and store a couple of property values (Help Topic Ids basically)
into it. From the VS.Net end of things there's no UI - all the UI is handled
via Automation of the COM object and the VS.Net code should only interact
between the active control and the external COM object.

I'm not quite sure what type of 'add-in' would make the best fit here. I
basically need 3 key bindings that trigger these operations. What is the
best fit for this sort of thing? Macros? An add-in seems like overkill.

Sorry for the ignorance - this topic is completely new for me and the
documentation and even what's available online is really scattered all over
the place and nearly incomprehensible until you piece it all together from
the various sources <g>...

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web

> For Winforms you must have a System.Component.Model.Design.IDesignerHost
> (retrieved from Window.Object) and then you request the ISelectionService:
[quoted text clipped - 20 lines]
> >
> > +++ Rick ---
Adam Friedman - 29 Jun 2004 06:38 GMT
If possible, try doing it from an add-in or some facility other than the
macro IDE.
I've seen all sorts of weird errors when dealing with services from the
macro IDE; the macro IDE is a separate process and I am unsure how managed
service objects are translated between processes...

> Carlos,
>
[quoted text clipped - 85 lines]
> > >
> > > +++ Rick ---
Carlos J. Quintero [MVP] - 29 Jun 2004 09:45 GMT
Hi Rick,

I have reproduced the problem with the macros IDE. It works from add-ins,
however. While add-ins in VS.NET are far from trivial, in your case is
simple since you don´t need UI, right? Below you have a minimal add-in that
creates a VS.NET command that when invoked shows a messagebox with the
number of controls selected in the active form, if any. As you know, you can
bind a keyboard shortcut to a command using Tools | Customize... menu,
Keyboard... button, and blah blah.

Imports Microsoft.Office.Core
Imports Extensibility
Imports System.Runtime.InteropServices
Imports EnvDTE

Module Constants
Friend Const PROGID As String = "MyAddin10.Connect" ' Change this value by
the one generated by the VS.NET wizard for add-ins.
End Module

<GuidAttribute("CB49BCCB-F6D5-4A04-92B2-4F5270624D57"),
ProgIdAttribute(PROGID)> _
Public Class Connect
Implements Extensibility.IDTExtensibility2
Implements EnvDTE.IDTCommandTarget

Private m_COMMAND_NAME As String = "MyCommand"

Private m_objDTE As EnvDTE.DTE
Private m_objAddIn As EnvDTE.AddIn

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 OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnDisconnection
End Sub

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 objCommand As Command

 m_objDTE = CType(application, EnvDTE.DTE)
 m_objAddIn = CType(addInInst, EnvDTE.AddIn)

 Try
  objCommand = m_objDTE.Commands.Item(PROGID & "." & m_COMMAND_NAME)
 Catch
 End Try

 If objCommand Is Nothing Then
  Try
   objCommand = m_objDTE.Commands.AddNamedCommand(m_objAddIn,
m_COMMAND_NAME, m_COMMAND_NAME, "", True, 59, Nothing,
vsCommandStatus.vsCommandStatusSupported Or
vsCommandStatus.vsCommandStatusEnabled)
  Catch e As System.Exception
   System.Windows.Forms.MessageBox.Show(e.ToString, PROGID,
MessageBoxButtons.OK, MessageBoxIcon.Error)
  End Try
 End If

End Sub

Public Sub Exec(ByVal CmdName As String, ByVal ExecuteOption As
EnvDTE.vsCommandExecOption, ByRef VariantIn As Object, ByRef VariantOut As
Object, ByRef handled As Boolean) Implements EnvDTE.IDTCommandTarget.Exec

 Dim objIDesignerHost As System.ComponentModel.Design.IDesignerHost
 Dim objIServiceProvider As System.IServiceProvider
 Dim objISelectionService As System.ComponentModel.Design.ISelectionService

 If CmdName = PROGID & "." & m_COMMAND_NAME Then

  If Not (m_objDTE.ActiveWindow Is Nothing) Then

   If Not (m_objDTE.ActiveWindow.Object Is Nothing) Then

    If TypeOf m_objDTE.ActiveWindow.Object Is
System.ComponentModel.Design.IDesignerHost Then

     objIDesignerHost = DirectCast(m_objDTE.ActiveWindow.Object,
System.ComponentModel.Design.IDesignerHost)

     objIServiceProvider = DirectCast(objIDesignerHost,
System.IServiceProvider)

     objISelectionService =
DirectCast(objIServiceProvider.GetService(GetType(System.ComponentModel.Desi
gn.ISelectionService)), System.ComponentModel.Design.ISelectionService)

     MessageBox.Show(objISelectionService.SelectionCount.ToString & "
control(s) selected")

    End If

   End If

  End If

 End If

End Sub

Public Sub QueryStatus(ByVal CmdName As String, ByVal NeededText As
EnvDTE.vsCommandStatusTextWanted, ByRef StatusOption As
EnvDTE.vsCommandStatus, ByRef CommandText As Object) Implements
EnvDTE.IDTCommandTarget.QueryStatus

 If CmdName = PROGID & "." & m_COMMAND_NAME Then
  StatusOption = vsCommandStatus.vsCommandStatusSupported Or
vsCommandStatus.vsCommandStatusEnabled
 End If

End Sub

End Class

Signature

Carlos J. Quintero (Visual Developer - .NET MVP)

FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)

> Carlos,
>
[quoted text clipped - 60 lines]
>
> +++ Rick ---
"Ed Dore [MSFT]" - 30 Jun 2004 18:20 GMT
Hi Rick,

You cannot retrieve the IDesignerHost interface from a macro. UI elements
like system.windows.control objects cannot be remoted across processes. As
Adam pointed out the macroing environment is actually in a different
process. You'll need to implement an addin or vsip package to get access to
the IDesignerHost interface.

Sincerely,
Ed Dore [MSFT]

This post is 'AS IS' with no warranties, and confers no rights.
Rick Strahl [MVP] - 01 Jul 2004 06:47 GMT
Thanks Ed,

I did finally break down and do it as an Add in and that did work. I got a
lot of reading up to do it looks like though, but I'm sure I'll be back with
more questions once I get up to speed a little mroe. For now I got the
basics working for what I need...

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web

> Hi Rick,
>
[quoted text clipped - 8 lines]
>
> This post is 'AS IS' with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.