Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Passing form handles between forms

I have 2 forms btoh of which are MDI children.

Form1 has a button in which i call Form 2
Form2 has a button in which i call Form 1

Now, the problem is that when i go to Form2 from Form1, and i click on the button in Form2, i need to go to the form that launched it and not a new instance of Form1.

How can i accomplish this. My original thought was to do it by  a property. But property's can be passed as ByRef and it has to be ByVal. The only solution seems to have  a Public Function in each form where i pass the variable "Me" as ByRef. On clicking the button, i call ShowForm.

Is there a better way of handling this?

Karthik
Tuesday, May 17, 2005

Essentially, no.

I always create a FormManager object to keep the code nice and tidy, but essentially the FormManager does the same thing -- it manages lists of references to form objects.

No matter how you do it, you've got to keep references to your open forms hanging around somewhere. You do it in the forms, I do it in a dedicated FormManager class -- but either way, you're passing around references to actual form instances.

Sgt.Sausage
Tuesday, May 17, 2005

Using a FormManager is a much better solution, even if it seems a small difference first.

Doing it in forms themselves creates a cyclical reference Form1->Form2->Form1..., with is a recipe for trouble. You have questions creating forms - wait till you try to close one. Reclaiming memory will be also difficult.

A simple hierarchical ownership with a dedicated controller class is just a better design.

Dmitri Chatokhine
Wednesday, May 18, 2005

*  Recent Topics

*  Fog Creek Home