Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / April 2007

Tip: Looking for answers? Try searching our database.

Referencing main app window from a custom control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter - 08 Apr 2007 03:19 GMT
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.
Peter - 08 Apr 2007 04:50 GMT
> 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
[quoted text clipped - 18 lines]
> 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.
Charlie Brown - 09 Apr 2007 17:01 GMT
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.
Cor Ligthert [MVP] - 08 Apr 2007 05:50 GMT
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

> Hi,
>
[quoted text clipped - 33 lines]
> seems kinda hacky... there should be an easier, IntelliSense-friendly way
> to do it.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.