Referencing main app window from a custom control


P

Peter

Hi,

I have a project with an MDI parent form (Main), which can contain any
number of MDI children (Document). The Document form contains several user
controls, designed as separate projects and referenced from the main
project. I originally had one user control doing some dataset loading on its
own, but now I need the same dataset initialized in the same way in two
different places, so I decided to put it in the MDI parent form.

Now, how do I get a reference to that form (named Main) from the control?
I've tried things like,

Idea 1)
Dim MainForm as Main
MainForm = Me.FindForm.MdiParent

but Main is not recognized as a valid type from within the control. I can
use

Dim MainForm as Form

but then it won't let me access the dataset because dsPaperColors is not a
member of System.Windows.Forms.Form. Obviously.

Idea 2)
I also tried adding a module with a property on it of type Main that the
startup form sets to Me on loading (kind of a fake global variable), but I
can't access that module from the control either.

Idea 3)
And I can't add a reference to the main project into the control, because
there is no DLL.

So how shall I go about this? I suppose I could just pass the main MDI
parent form as an argument to the constructor of the control, but that seems
kinda hacky... there should be an easier, IntelliSense-friendly way to do
it.
 
Ad

Advertisements

P

Peter

I have a project with an MDI parent form (Main), which can contain any
number of MDI children (Document). The Document form contains several user
controls, designed as separate projects and referenced from the main
project. I originally had one user control doing some dataset loading on
its own, but now I need the same dataset initialized in the same way in
two different places, so I decided to put it in the MDI parent form.

Now, how do I get a reference to that form (named Main) from the control?
I've tried things like,

Idea 1)
Dim MainForm as Main
MainForm = Me.FindForm.MdiParent

Idea 2)
I also tried adding a module with a property on it of type Main that the
startup form sets to Me on loading (kind of a fake global variable), but I
can't access that module from the control either.

Idea 3)
And I can't add a reference to the main project into the control, because
there is no DLL.


I have solved the problem... or at least gotten something to work...

I simply passed the control a reference to the dataset from the Document
form, using MdiParent.dsPaperColors. Perhaps a better option all around.
 
C

Cor Ligthert [MVP]

Peter,

This can be a long thread therefore I start it slowly.

Why a form, one of the goals is in OOP to leave the dataaccess (and even the
middle data hanling) for from the UI (User interface) Therefore a class or a
good written module would do probably a better job here. You can as well use
a module (or shared class) for often done things, and than to instanced
classes for seldon done things.

Just as start for a thaught discussion

Cor
 
Ad

Advertisements

C

Charlie Brown

I use the following quite often.

In you child form implement this in the class body.

Private WithEvents MyParentForm As Main

In your childs Load method create a reference.

MyParentForm = CType(Me.MdiParent, Main)

This works with Intellisense. If you need access to controls on the
Parent form, make their scope either Friend or Public.
 

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