Click here to Skip to main content
6,595,444 members and growing! (17,515 online)
Email Password   helpLost your password?
Desktop Development » Smart Client » General     Intermediate License: The Code Project Open License (CPOL)

How to Get a Reference to Parent Form from a Component

By David Catherman

Components don't have a .Parent property like controls do. Getting a reference to the instance of the parent form the component was on is very difficult.
VB (VB 7.x, VB 8.0, VB 9.0), Windows, .NET (.NET 2.0, .NET 3.0, .NET 3.5), MobileSQL 2000, VS.NET2003, VS2005, VS2008, DBA, Dev
Version:2 (See All)
Posted:19 Oct 2007
Views:14,161
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.31 Rating: 2.18 out of 5
2 votes, 50.0%
1

2

3

4
2 votes, 50.0%
5

Introduction

I have been working on a custom component that inherits from and extends the BindingSource component (more detailed article in the works). I needed to get a reference to the instance of the parent form the component was on. This proved to be very difficult.

Background

Components don't have a .Parent property like controls do. They do have a container property, but it does not have a parent property either. Sometimes casting the container as a ContainerControl works (it does have a parent property), but in this case it won't cast correctly.

All Windows Designer forms add a container called "components" and all components are added to this container and show at the bottom of the screen in design mode. From inside the component, it is easy to get a reference to the container, but I could not get a reference to the parent form of the container?

I got some useful help from "nobugz" (Hans Passant – Microsoft MVP) on the Windows Form Forum on MSDN who found that the Error Provider component manages to get a reference to the parent form and exposes it in a property called "ContainerControl". The secret the Error Provider uses is to override the Site function of the component and capture IDesignerHost service.

The Code

It turns out that the solution is even simpler. All that is needed is to reference the Site property of the base component and get the designer host information from it. Here are the steps to achieve the goal:

  1. Create a new item in your Windows Project and use the "Component" template.
  2. Switch to "Code View" and add the following code:
    Imports System.ComponentModel.Design 
    … 
    Private _form As Form
    Private Sub GetFormInstance() ' called from constructor 
       Dim _host As IDesignerHost = Nothing 
       If MyBase.Site IsNot Nothing Then _host = _ 
       CType(MyBase.Site.GetService(GetType(IDesignerHost)), IDesignerHost) 
       If _host IsNot Nothing Then _form = CType(_host.RootComponent, Form) 
    End Sub
  3. Open the Designer code (you may have to toggle the "Show All Files" button at the top of the Solution Explorer and then expand the files under the component to see the file with the .Designer extension).
  4. At the end of the default constructor, add a call to the GetFormInstance function. The code should look like this:
    <System.Diagnostics.DebuggerNonUserCode()> _ 
    Public Sub New() 
    
       MyBase.New() 
    
       'This call is required by the Component Designer. 
       InitializeComponent() 
    
       'Added by developer 
       GetFormInstance() 
    
    End Sub
  5. Now switch back to the user partial class for the component and add whatever code is needed to manipulate the parent form.
  6. Create a Form in your Windows Application and add the custom component (it should be listed at the top of the Toolbox after you build).

That's it. It is fairly simple once you understand the magic that happens in the ISite service interface that allows applications to interact with the designer (Visual Studio in this case).

History

  • 19th October, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

David Catherman


Member
Dave Catherman is an application architect/lead developer for .NET applications. He has been developing database applications for many years and jumped on the .NET bandwagon early on. He is MCP certified in .NET Web Apps and Windows Apps and is pursuing MCSD.
Occupation: Web Developer
Location: United States United States

Other popular Smart Client articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralPossible solution in VB PinmemberRichard Turnbull20:11 5 Nov '08  
GeneralHow i solve this problem Pinmembergfx1817:03 1 Apr '08  
GeneralIt doesn't works (( Pinmembergfx1823:39 31 Mar '08  
GeneralRe: It doesn't works (( PinmemberDavid Catherman3:10 1 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Oct 2007
Editor: Deeksha Shenoy
Copyright 2007 by David Catherman
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project