Multiple forms show/load best practice

R

RxRick

vb.NET CF newbie question.. long-time VB6 & eVB user, hence the
problem :)

What is the best way to show/close forms in a multiple form project?
ShowDialog works sometimes when Show doesn't, at other times Show
works when ShowDialog doesn't. I tried a half-dozen different
combinations, at this point, I am thoroughly confused.

My program starts with Sub Main, it reads a registry value, if program
is registered, [main] is loaded, if not, then [splash] is loaded
first, then [main]. From [main] user may go directly to [frm_2] or to
[frm_1] first, then to [frm_2]. The program then flows sequentially
to [frm_3] then [frm_4] (and back). Maybe better explained by a cheesy
diagram (that doesn't look good using proportional fonts):

Sub Main-->( OR )
| |
| [splash]
V |
( OR )---->[main]--->( OR )
| |
( OR ) |
| |
V V
[frm_1]----->[frm_2]---->[frm_3]---->[frm_4]

The user can then backtrack through the forms to [main]. These are
complex forms with multiple displays of data, multiple inputs and
multiple options, there is no way to compress everything down to 1 or
2 forms. And I despised the eVB way of putting frames on top of frames
on top of forms.

Sub Main code:
If boolReg Then
Application.Run(frmSplash)
Else
Application.Run(frmMain)
End If

frmSplash code to load frmMain:
' Declare new frmMain
Dim newForm As New frmMain
' Show doesn't work - have to use ShowDialog !?
newForm.ShowDialog()
newForm.Dispose()
' This doesn't actually close the form !?
' Splash form still stays in memory
Me.Close()

frmMain code to load frm_2:
Dim newForm As New frm_2
newForm.Owner = Me
newForm.ShowDialog()
newForm.Dispose()

It gets uglier when loading [frm_1] from [main], then [frm_2] from
[frm_1]
frmMain code to load frm_1
Dim newForm As New frm_1
newForm.Owner = Me
' Have to use Show, because it's not a dialog !?
newForm.Show()
frm_1 code to load frm_2
Dim newForm As New frmModel
' Setting owner here erases the global variables!?
' But without setting owner, it adds yet another open form to
the "stack"
newForm.Show()
Me.Close()

So, it's an ugly conglomerated mess that makes no sense but (somehow)
works most of the time. Problem is, it leaves forms open with no
owner, so when switching to and from other applications, unowned
screens will go blank.

I've read 3 books, and still don't get it. Some of the solutions in
these books are not supported in the compact version of .NET, and the
CF book examples are too simple.

If somebody could give me an example, or point me to a code sample
posted elsewhere, or a good book or tutorial that explains forms in
the real CF world, I would be forever grateful. (Sorry for the wordy
post)
 
C

Chris Tacke, eMVP

You've certainly made it way more complex than it needs to be.

Forst delegate a Form as the one that holds the message pump. Probably
MainFOrm. When it's closed, the app ends. So the app should start with
App.Run(new MainForm).

When *it* loads, decide whether to show a splash (via ShowDialog) or not.

At that point I'd have a Stack<Form> member variable in MainForm.Use this to
load each child and show them with Show. When you load one, push it onto
the stack, when you close one, pop it off and display the next one back.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


RxRick said:
vb.NET CF newbie question.. long-time VB6 & eVB user, hence the
problem :)

What is the best way to show/close forms in a multiple form project?
ShowDialog works sometimes when Show doesn't, at other times Show
works when ShowDialog doesn't. I tried a half-dozen different
combinations, at this point, I am thoroughly confused.

My program starts with Sub Main, it reads a registry value, if program
is registered, [main] is loaded, if not, then [splash] is loaded
first, then [main]. From [main] user may go directly to [frm_2] or to
[frm_1] first, then to [frm_2]. The program then flows sequentially
to [frm_3] then [frm_4] (and back). Maybe better explained by a cheesy
diagram (that doesn't look good using proportional fonts):

Sub Main-->( OR )
| |
| [splash]
V |
( OR )---->[main]--->( OR )
| |
( OR ) |
| |
V V
[frm_1]----->[frm_2]---->[frm_3]---->[frm_4]

The user can then backtrack through the forms to [main]. These are
complex forms with multiple displays of data, multiple inputs and
multiple options, there is no way to compress everything down to 1 or
2 forms. And I despised the eVB way of putting frames on top of frames
on top of forms.

Sub Main code:
If boolReg Then
Application.Run(frmSplash)
Else
Application.Run(frmMain)
End If

frmSplash code to load frmMain:
' Declare new frmMain
Dim newForm As New frmMain
' Show doesn't work - have to use ShowDialog !?
newForm.ShowDialog()
newForm.Dispose()
' This doesn't actually close the form !?
' Splash form still stays in memory
Me.Close()

frmMain code to load frm_2:
Dim newForm As New frm_2
newForm.Owner = Me
newForm.ShowDialog()
newForm.Dispose()

It gets uglier when loading [frm_1] from [main], then [frm_2] from
[frm_1]
frmMain code to load frm_1
Dim newForm As New frm_1
newForm.Owner = Me
' Have to use Show, because it's not a dialog !?
newForm.Show()
frm_1 code to load frm_2
Dim newForm As New frmModel
' Setting owner here erases the global variables!?
' But without setting owner, it adds yet another open form to
the "stack"
newForm.Show()
Me.Close()

So, it's an ugly conglomerated mess that makes no sense but (somehow)
works most of the time. Problem is, it leaves forms open with no
owner, so when switching to and from other applications, unowned
screens will go blank.

I've read 3 books, and still don't get it. Some of the solutions in
these books are not supported in the compact version of .NET, and the
CF book examples are too simple.

If somebody could give me an example, or point me to a code sample
posted elsewhere, or a good book or tutorial that explains forms in
the real CF world, I would be forever grateful. (Sorry for the wordy
post)
 
N

Nebo

This is something I have debated as well, lets face it, for the most
part the UI paradign for windows mobile is SDI. I find if I load
second forms I have found that its confusing in windows mobile. I
recently built a application called NeboPad (tab based tab editor) to
test a concept of having one form that displays a control as a form.
So for each view, I have a control that is loaded into the one form.
This way I never lose context of the task the user is doing at
present. Even though WM supports multitasking, the user cannot really
do to many things at one time. So my application leverages this.
Personally, I think it makes it easier to navigate..... one task at a
time and you always know where you are at.

Nebo
http://sites.google.com/site/nebowiki/
 
P

Peter Wucherer

Hi,

maybe this function can show you the way. It shows a new form on the
top on the CE device.
It's C# for a Windows CE project, but in Basic it should be the same:

private Form currentActiveForm = null;

/// <summary>
/// Show the new form, close the older one
/// </summary>
/// <param name="newForm">new form to show</param>
private void ShowNewForm(Form newForm)
{
if (currentActiveForm != null)
{
currentActiveForm.Close();
}

currentActiveForm = newForm;

// add now the new form as control of this main form
#if WindowsCE
Controls.Add(newForm);
#else
this.AddOwnedForm(newForm);
#endif

// set the new form width, height, position
newForm.Top = 0;
newForm.Left = 0;
newForm.Width = Width;
newForm.Height = Height;
#if WindowsCE
#else
newForm.TopLevel = false;
newForm.Parent = this;
#endif

newForm.Show();
}


vb.NET CF newbie question.. long-time VB6 & eVB user, hence the
problem :)

What is the best way to show/close forms in a multiple form project?
ShowDialog works sometimes when Show doesn't, at other times Show
works when ShowDialog doesn't. I tried a half-dozen different
combinations, at this point, I am thoroughly confused.

My program starts with Sub Main, it reads a registry value, if program
is registered, [main] is loaded, if not, then [splash] is loaded
first, then [main]. From [main] user may go directly to [frm_2] or to
[frm_1] first, then to [frm_2]. The program then flows sequentially
to [frm_3] then [frm_4] (and back). Maybe better explained by a cheesy
diagram (that doesn't look good using proportional fonts):

Sub Main-->( OR )
| |
| [splash]
V |
( OR )---->[main]--->( OR )
| |
( OR ) |
| |
V V
[frm_1]----->[frm_2]---->[frm_3]---->[frm_4]

The user can then backtrack through the forms to [main]. These are
complex forms with multiple displays of data, multiple inputs and
multiple options, there is no way to compress everything down to 1 or
2 forms. And I despised the eVB way of putting frames on top of frames
on top of forms.

Sub Main code:
If boolReg Then
Application.Run(frmSplash)
Else
Application.Run(frmMain)
End If

frmSplash code to load frmMain:
' Declare new frmMain
Dim newForm As New frmMain
' Show doesn't work - have to use ShowDialog !?
newForm.ShowDialog()
newForm.Dispose()
' This doesn't actually close the form !?
' Splash form still stays in memory
Me.Close()

frmMain code to load frm_2:
Dim newForm As New frm_2
newForm.Owner = Me
newForm.ShowDialog()
newForm.Dispose()

It gets uglier when loading [frm_1] from [main], then [frm_2] from
[frm_1]
frmMain code to load frm_1
Dim newForm As New frm_1
newForm.Owner = Me
' Have to use Show, because it's not a dialog !?
newForm.Show()
frm_1 code to load frm_2
Dim newForm As New frmModel
' Setting owner here erases the global variables!?
' But without setting owner, it adds yet another open form to
the "stack"
newForm.Show()
Me.Close()

So, it's an ugly conglomerated mess that makes no sense but (somehow)
works most of the time. Problem is, it leaves forms open with no
owner, so when switching to and from other applications, unowned
screens will go blank.

I've read 3 books, and still don't get it. Some of the solutions in
these books are not supported in the compact version of .NET, and the
CF book examples are too simple.

If somebody could give me an example, or point me to a code sample
posted elsewhere, or a good book or tutorial that explains forms in
the real CF world, I would be forever grateful. (Sorry for the wordy
post)
 

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