.NET Design Advice - best way to navigate between WinForms?

G

Guest

I'm building an multiform Windows application that I anticipate will grow to
contain many forms. I would appreciate some "best practices" advice on the
cleanest way to call one form from another.

Up to now, I've called one form from another using the following syntax to
call one form from another. From my first form, the code looks like this:

frmMyForm2 frm = new frmMyForm2(this);

This is great, except that I have to specifiy a custom constructor in
frmMyForm2 to catch the passed parameter. The constructor would look like
this:

public frmMyForm2(frmMyForm1 frm)
{
InitializeComponent();
this.Show();
frm.Hide();
}

This isn't so bad if only one form is calling frmMyForm2. However, if this
form is central to the app there could easily be eight or ten forms calling
it. Rather than having to include eight or ten custom constructors in
frmMyForm2, is there some method of capturing the first form in the parameter
as a generic type so I only have to code one constructor?

Any advise or suggestions would be much appreciated. Thanks!

Andre
 
B

Bob Grommes

Why not make the caller responsible for hiding itself?

frmMyForm2 frm = new frmMyForm2(this);
this.Hide();
frm.Show();

The only reason I can see to pass a reference to the other form is if there
are other more elaborate things you want to do to communicate back to the
original form later. Perhaps you want to do more than these Hide() and
Show() calls?

--Bob
 
K

Kuba Florczyk

Maybe you think about some manager?
For example some singleton:

FormsMgr frmMgr = FormsMgr.Instance;
frmMgr.Add(Form someForm, string someStrongName);

frmMgr[string strongName].Show();
frmMgr[string strongName].Hide();

That should help you.

kuba florczyk
 
J

james

Why not use a tab control instead? You do not have to show the tabs. I
actually use a Frame / Box control and then just swap in and out the
contents per the user selection from a menu just like the way Outlook does
it.

JIM
 

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