Switching Forms in a Multi-Form Application

  • Thread starter Bruce Schechter
  • Start date
B

Bruce Schechter

An acquaintance just asked the following excellent question, which I need
help to answer for him...
"What I would like to know is the best way to switch from one form to
another in a multiform application which allows me to close forms which I no
longer need, thereby minimizing my use of memory.

Assume that I have an application with ten forms labeled Form 1 through Form
10. Assume that when my application begins Main creates an instance of Form
1. Now when a user wants to switch from Form 1 to Form 2 the simplest method
is to create an instance of Form 2 which belongs to Form 1. One of the
negative side effects of this approach is that it means that we can't close
Form 1 as long as Form 2 is open.

Now let's move from Form 2 to Form 3. Again, one option is to create a Form
3 instance which belongs to Form 2. Once again we can't close Form 2 until
we close Form 3. From this you should see a pattern: Each new object belongs
to the last object and eventually we will have a chain of objects streching
all the way back to Main.

Even more depressing is what will happen when we return to a form we
previously visited. For example, what will happen if after creating an
instance of Form 10 (which belongs to Form 9) the user chooses to return to
Form 3. In our current example our application already has an instance of
Form 3 which belongs to Form 2. However, while Form 2 already holds a
reference to Form 3, how can we access that reference? I don't see how we
can. So now we must create a new instance of Form 3 which is owned by Form
10. Now keep jumping back and forth between different forms and what do we
have? A real mess in my opinion.

My previous solution with which I am not especially happy is to place a
series of "form switching" functions in a separate class which then acts as
the designated owner of all of my forms. When a user wants to switch from
Form 1 to Form 2, he calls a method in the designated owner class which
creates an instance of Form 2. When this method which does nothing more than
to call the constructor for Form 2 (and then display it) completes, Form 1
can close.

Using this approach I ran into two problems. The first was that in my case I
usually had quite a few parameters that I wanted to pass along to the new
form. This probably reflects the complexity of my application and is likely
just unavoidable. Recently I have been thinking that I probably want to
explore the use of static variables as an alternative to passing parameters
to the form switching method. Under this new approach, before Form 1
switches to Form 2, I can set Form 2's static variables based on information
collected on Form 1. Moreover, if there is any state information for Form 1
which I want to persist in case there is a return to Form 1, I can save it
in Form 1 static variables.

A second even greater problem is that in order to make Form 2 visible to the
user, my designated owner method has to call Form2.ShowDialog().
Unfortunately, this method does not complete until after Form 2 closes.
Therefore, my approach results in exactly the same house of cards which I
described above. I gave some brief thought to the idea of using static
constructors but a static constructor only is useful to initialize static
data members.

Perhaps one solution is to spawn a separate thread for each new form and /
or make an asynchronous call to the designated owner method constructing
each new form. That seems like an awfully complex solution.

One thing that certainly surprises me is that one would think that this
issue would be pretty common. Isn't there some recognized way of dealing
with this issue in an efficient manner. So far my research hasn't disclosed
any."

Can anyone propose a solution or a link to related documentation?

Cheers, and Thanks,

-- Bruce
 
X

Xin Huang [MSFT]

What about a TabControl based design? You can navigate to any tab at any
time without closing the others.

Hope this helps.

Regards,
Xin

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Visit http://windowsupdate.microsoft.com to get the latest
critical updates and service packs available for your computer's Windows
operating system.
 
B

Bruce Schechter

Hello Xin,
From a usability standpoint, your recommendation may be a very good one.
But the underlying technical question is still an important one that we want
to dig into. So, if anyone else can help, I'd really appreciate it.
cheers, Bruce
 
B

Bruce Schechter

Xin,

No, the project client specifically wants the ability to create a variety of
inter-related windows forms. They cannot be combined into an MDI
configuration. This project specification is not under my control.

A rough analogy would be the way Internet Explorer can spawn new IE windows
when links are clicked without shutting down the originating IE window. And
a key element of this analogy is that any IE window can be shut down by the
end user without affecting all other open windows. But my needs are
different from the IE analogy in that all the forms in my application need a
mechanism to share state information and communicate with one another, such
as having Form4 ask Form2 to come to the forground on demand.

Can you identify any BKMs or articles on this topic?

Cheers, Bruce
 
X

Xin Huang [MSFT]

MDI itself doesn't prevent the child forms from communicating with each
other. You can have a form manager (or the Main Frame can act as the form
manager) to coordinate the child form interactions. For example (pseudo
code):

void Form4_Button1Click()
{
IFormManager manager = Parent as IFormManager;
manager.BringFormToForeground(form2);
}

Regards,
Xin

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Visit http://windowsupdate.microsoft.com to get the latest
critical updates and service packs available for your computer's Windows
operating system.
 
B

Bruce Schechter

Hi Xin (and the Group)

I understand what you are saying about MDI. But perhaps I haven't
explained my question clearly. The customer who dictated the design
specification for the project specifically demanded a mult-window user
interface, and it simply cannot be MDI. (This is not for technical
reasons, but rather because of the client's personal preference.)

Further, I need inter-form messaging and some shared data between all the
windows (forms.)

And finally, I need the ability to randomly close any one of the windows
without causing another window to close. This must apply to the original
parent window as well as all those that are spawned after it.

Can anyone to point me to a good article or sample of such a scenario? Or,
is it even possible via the .NET framework?

Thanks,
Bruce
 
P

Patrick Vanden Driessche

Hi Bruce,

I think I get your problem ... IMHO, you will need a kind of Multi Top Level
Form interface

You can accomplish this by creating your own derived ApplicationContext
class.
I remember having seen a MultiSdiApplicationContext class in the Genghis
library a while ago. (http://www.sellsbrothers.com/tools/genghis/)
(the library comes with a sample app MultiSDI.. you can give that a try and
see if thats what you are looking for)

For your problem this class probable won't have all the functionality you
need as is... but you can get the idea of how you can manage the fact of
having different Top-Level forms. Using this type of ApplicationContext you
don't have the trouble of 'owning forms'.

For your personal messaging/shared data, I would suggest creating your own
'class' that holds all this shared information... You can easely create a
single shared instance of this and add a property or a special constructor
to each form in order to pass this information.

If you want to work with the ApplicationContext you will have to create a
Sub Main as project startup (if using C# you already have that one probable)
instead of a form. And launch the app by using Application.Run(
YourApplicationContextInstance).

Hope this helps you out.
Regards,

Patrick.


Bruce Schechter said:
Hi Xin (and the Group)

I understand what you are saying about MDI. But perhaps I haven't
explained my question clearly. The customer who dictated the design
specification for the project specifically demanded a mult-window user
interface, and it simply cannot be MDI. (This is not for technical
reasons, but rather because of the client's personal preference.)

Further, I need inter-form messaging and some shared data between all the
windows (forms.)

And finally, I need the ability to randomly close any one of the windows
without causing another window to close. This must apply to the original
parent window as well as all those that are spawned after it.

Can anyone to point me to a good article or sample of such a scenario? Or,
is it even possible via the .NET framework?

Thanks,
Bruce

"Xin Huang [MSFT]" said:
MDI itself doesn't prevent the child forms from communicating with each
other. You can have a form manager (or the Main Frame can act as the form
manager) to coordinate the child form interactions. For example (pseudo
code):

void Form4_Button1Click()
{
IFormManager manager = Parent as IFormManager;
manager.BringFormToForeground(form2);
}

Regards,
Xin

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? Visit http://windowsupdate.microsoft.com to get the latest
critical updates and service packs available for your computer's Windows
operating system.
 

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