|
-
May 1st, 2010, 07:13 AM
#1
[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.
-
May 1st, 2010, 07:58 AM
#2
Frenzied Member
-
May 1st, 2010, 08:59 AM
#3
Re: How can a component recognize the Form it is on?
 Originally Posted by Pc_Not_Mac
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.
-
May 1st, 2010, 01:37 PM
#4
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:
Imports System.ComponentModel.Design Public Class TestComponent Inherits System.ComponentModel.Component Private _ContainerControl As ContainerControl Public Property ContainerControl() As ContainerControl Get Return _ContainerControl End Get Set(ByVal value As ContainerControl) _ContainerControl = value 'Testing purposes: MessageBox.Show(value.ToString()) End Set End Property Public Overrides Property Site As System.ComponentModel.ISite Get Return MyBase.Site End Get Set(ByVal value As System.ComponentModel.ISite) MyBase.Site = value If value Is Nothing Then Return Dim host = TryCast(value.GetService(GetType(IDesignerHost)), IDesignerHost) If host IsNot Nothing Then Dim componentHost = host.RootComponent If TypeOf componentHost Is ContainerControl Then Me.ContainerControl = TryCast(componentHost, Windows.Forms.ContainerControl) End If End If End Set End Property End Class
Seems to work just fine.
Last edited by NickThissen; May 1st, 2010 at 01:46 PM.
-
May 1st, 2010, 02:12 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|