Results 1 to 5 of 5

Thread: [RESOLVED] How can a component recognize the Form it is on?

  1. #1

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Resolved [RESOLVED] How can a component recognize the Form it is on?

    I'm trying to make a Toolbox component which the user can drag onto a form in the VS Designer. The component has to recognize the Form it is on, in order to handle certain Form events. I would prefer to do it without requiring the user to add any code to the form. Does anyone know a way the component can get the identity of the form?

    BB
    Last edited by boops boops; May 1st, 2010 at 09:04 AM.

  2. #2
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    Re: Adding a component

    Did you even try This.

    After a little search i found http://www.codeproject.com/KB/vb/ser...omponents.aspx
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  3. #3

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How can a component recognize the Form it is on?

    Quote Originally Posted by Pc_Not_Mac View Post
    Did you even try This.
    I never post a question to the forum without doing extensive searching first.

    I find that article hard to understand but I can't see how it is relevant to my question. Perhaps my title was too unspecific. I have now edited it.

    I was hoping the component's Container or Site property would be set when it is dragged onto a form, but that does not happen: clearly Forms do not implement IContainer by default. I can give the component a browsable Form property like this:
    Code:
       <Browsable(True)>
        Private WithEvents _Form As New Form
        Public WriteOnly Property Form As Form
            Set(ByVal value As Form)
                _Form = value
            End Set
           End Property
    Then the user can select any of the project's forms in the component's Properties window. But that ought be redundant because the user has already dragged the component from the Toolbox onto a specific form. So I wonder if there is some way I can avoid that step?

    BB
    Last edited by boops boops; May 1st, 2010 at 09:06 AM.

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: How can a component recognize the Form it is on?

    It is definitly possible, and I think it was via the properties you mention, possibly indirectly. I'll have a look at it, I'm sure I did this before.

    EDIT
    I found this in the meantime, check it out
    http://stackoverflow.com/questions/3...ts-parent-form

    EDIT2
    VB code translated:
    vb.net Code:
    1. Imports System.ComponentModel.Design
    2.  
    3. Public Class TestComponent
    4.     Inherits System.ComponentModel.Component
    5.  
    6.     Private _ContainerControl As ContainerControl
    7.     Public Property ContainerControl() As ContainerControl
    8.         Get
    9.             Return _ContainerControl
    10.         End Get
    11.         Set(ByVal value As ContainerControl)
    12.             _ContainerControl = value
    13.  
    14.             'Testing purposes:
    15.             MessageBox.Show(value.ToString())
    16.         End Set
    17.     End Property
    18.  
    19.     Public Overrides Property Site As System.ComponentModel.ISite
    20.         Get
    21.             Return MyBase.Site
    22.         End Get
    23.         Set(ByVal value As System.ComponentModel.ISite)
    24.             MyBase.Site = value
    25.  
    26.             If value Is Nothing Then Return
    27.  
    28.             Dim host = TryCast(value.GetService(GetType(IDesignerHost)), IDesignerHost)
    29.             If host IsNot Nothing Then
    30.                 Dim componentHost = host.RootComponent
    31.                 If TypeOf componentHost Is ContainerControl Then
    32.                     Me.ContainerControl = TryCast(componentHost, Windows.Forms.ContainerControl)
    33.                 End If
    34.             End If
    35.         End Set
    36.     End Property
    37.  
    38. End Class
    Seems to work just fine.

  5. #5

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How can a component recognize the Form it is on?

    Thanks a million Nick. I suspected there must be something in ComponentModel.Design but the information I found about it was far too complex for me to figure out what had to be done. The Properties method I was thinking of didn't work after all because the Designer instantiates non-visible components before the form, so it still had to be done in code.

    Your solution is exactly what is needed. Your reputation deserves ++ but that stupid spread-it-around rule prevents it. Again thanks, BB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width