FindForm() returns nothing

M

Mark Peters

I have a control class that inherits from ComboBox. The intention of the constructor is to grab a reference to the BaseForm class of the form containing the custom control and populate its members using cached data from a "ClientCache" dataset in the base form. The constructor:

Public Sub New()

Me.Name = "cboItems"

Dim bf As BaseForm = CType(Me.FindForm(), BaseForm)

Me.DataSource = bf.ClientCache.Tables("MyItems")

End Sub

The code always dies on line 3 because Me.FindForm() always returns Nothing, even though the combobox control is being used by a windows form. (All our forms inherit from BaseForm, which inherits Windows.Form.)

VS.NET help indicates that FindForm "retrieves the form that the control is on". But from all indications this is not the case.

Thoughts?

TIA.

Mark
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Alessandro,

Windows Forms controls don't have OnLoad. This is form's event (method).

Mark, your method returns *null* because in the constructor the control is
not part of the form's control hierarchy yet. What FindForm method does is
to go down the paren chain until it finds the form. Since the control is not
added to a form yet it cannot find any form. I general the order of creating
and adding the cotntrol to a container is:
1. Creating the control -> this invokes the constructor.
2. Set some properties and hookup events
3. <container>.Controls.Add(ctrl), where <container> could be a form or
other control.

Only after step 3 is finished you can call sucessfully FindForm method and
expect that it might return something different than *null*.

You should probably move your code in Control.OnParentChanged method, or
hook control's ParentChanged event. However, keep in mind that even there
the FindForm method can fail. If you have situation like: like create a
panel, add control to the panel (this will fire the ParentChanged event) add
the panel to the form. You can see if you call FindForm on the ParentChanged
event the form is not know yet.
I don't know if you can find any reliable place where you can do what you
want to do. My suggestion is to revise your design and expose some method
for initiation the databinding process. Let the programmer using your
control chose when and where is the right moment to call this method.


HTH
Stoitcho Goutsev (100) [C# MVP]
 
A

Alessandro Pessotto

Sure they do.

See UserControl.OnLoad in the online help or just override onload in
the editor.
 
H

Herfried K. Wagner [MVP]

Alessandro Pessotto said:
See UserControl.OnLoad in the online help or just override onload in
the editor.

A class inheriting from 'ComboBox' is not a usercontrol.
 
A

Alessandro Pessotto

How about you override the control's OnHandleCreated and get the form
there?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top