Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to manage multiple forms? best practice?

5 views
Skip to first unread message

Peter Nofelt

unread,
Feb 13, 2007, 1:50:32 AM2/13/07
to
Are there any best practices in terms of managing multiple forms and
switching between them? I ask, because my application may have 3 - 4
different forms that the user is constantly switching between based on
his request (not to say that he is explicitly requesting a form, he
just preforms an action and awaits the results which are displayed on
a separate form).


Also, is there any way to make sure the softkey menus the same across
all forms?

tamberg

unread,
Feb 13, 2007, 5:13:23 AM2/13/07
to
// Here's some code that does the first job. To show a Form f simply
call Browser.Show(f);

public sealed class Browser {

Browser () {}

static Form current;
static EventHandler exit = new EventHandler(Exit);

static void Exit (object o, EventArgs a) {
Application.Exit();
}

public static void Show (Form f) {
// Assert(f != null, 0);
if (current == null) {
current = f;
current.Closed += exit;
Application.Run(current);
} else {
// Assert(f != current, 0);
f.Closed += exit;
f.Show();
current.Closed -= exit;
current.Hide();
current = f;
}
}

}

// An application with two screens could look like this:

public delegate void Command ();

public sealed class HomeView {

HomeView () {}

static Form f;

public static event Command Edit;

static void OnEdit (object o, EventArgs a) {
if (Edit!= null) { Edit(); }
}

public static void Initialize () {
// Assert(f == null, 0);
f = new Form();
Button b = new Button();
b.Text = "Edit";
b.Click += new EventHandler(OnEdit);
f.Controls.Add(b);
f.Controls.Add(new Label());
}

public static void Activate () {
// Assert(f != null, 0);
Browser.Show(f);
}

}

public sealed class EditView {

EditView () {}

static Form f;

public static event Command Done;

static void OnDone (object o, EventArgs a) {
if (Done!= null) { Done(); }
}

public static void Initialize () {
// Assert(f == null, 0);
f = new Form();
Button b = new Button();
b.Text = "Done";
b.Click += new EventHandler(OnDone);
f.Controls.Add(b);
f.Controls.Add(new Label());
}

public static void Activate () {
// Assert(f != null, 0);
Browser.Show(f);
}

}

sealed class App {

static void Main () {
HomeView.Initialize();
EditView.Initialize();
HomeView.Edit += new Command(EditView.Activate);
EditView.Done += new Command(HomeView.Activate);
HomeView.Activate();
}

}

tamberg

unread,
Feb 13, 2007, 5:16:36 AM2/13/07
to
// Sorry, the lines f.Controls.Add(new Label()); are not neccessary.

tamberg

unread,
Feb 13, 2007, 5:20:48 AM2/13/07
to
// And Button should be MenuItem (for Smartphone).

Fabien

unread,
Feb 13, 2007, 3:41:21 PM2/13/07
to
Hi,

You can create a main Form with a back panel and add your others forms
to the panel when you need to show you form. Then you can use events
to communicate between the main form and the childs forms.

BR


Fabien Decret
Windows Embedded Consultant

ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/

0 new messages