Forms Flow

H

Hector Santos

I need this flow:

Main form is display. You select something, it displays another 2nd
form and the mainform is hidden. When the 2nd form is closed, it
shows the hidden main form again.

Do I need to keep a global public instance. In other words, I see in
the program.cs auto generated code it has:

namespace wcNavigatorCS
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new wcNavConnectSitesForm());
}
}
}

The wcNavConnectionSitesForm is the main form. So I change this to so
that it creates the new as a public instance?

What I did, probably clumsy, in the 2nd form FormClosing event, is this:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
FormCollection fl = Application.OpenForms;
foreach (Form f in fl)
{
if (f.Name == "wcNavConnectSitesForm")
{
f.Show();
break;
}
}
}

Seems to work. Any problem with this method? It avoids having to
change auto-generated code like program.cs, but is that ok too?

Thanks
 
P

Peter Duniho

Hector said:
I need this flow:

Main form is display. You select something, it displays another 2nd
form and the mainform is hidden. When the 2nd form is closed, it shows
the hidden main form again.

Do I need to keep a global public instance. [...]

No. If you don't need the second form to explicitly know about the
first, there's no need to cause it to. Likewise, if there's not a
scenario where you literally must get at the form instance via some
static property somewhere, you don't need to add one for this particular
purpose.

Instead, do this:

void MethodInForm1ThatShowsForm2()
{
Form form2 = new Form2();

form2.FormClosed += (sender, e) => Visible = true;

Visible = false;
form2.Show();
}

That simply subscribes an event handler (in the form of an anonymous
method declared using a lambda expression) to the second form's
FormClosed event that will set the first form's Visible property back to
"true" when the second form closes.

Then the first form is hidden, and the second form is shown.

You can write variations on that theme to handle other similar
scenarios, depending on your exact needs.

Pete
 
K

kndg

I need this flow:

Main form is display. You select something, it displays another 2nd form
and the mainform is hidden. When the 2nd form is closed, it shows the
hidden main form again.

I think Mr Mike already provide you some hints on your previous post.
Here is my code snippet,

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.FormClosed += form2_FormClosed;
form2.Show();
Hide();
}

private void form2_FormClosed(object sender, FormClosedEventArgs e)
{
Show();
}
}
 
H

Hector Santos

Peter said:
Hector said:
I need this flow:

Main form is display. You select something, it displays another 2nd
form and the mainform is hidden. When the 2nd form is closed, it
shows the hidden main form again.

Do I need to keep a global public instance. [...]

No. If you don't need the second form to explicitly know about the
first, there's no need to cause it to. Likewise, if there's not a
scenario where you literally must get at the form instance via some
static property somewhere, you don't need to add one for this particular
purpose.

Instead, do this:

void MethodInForm1ThatShowsForm2()
{
Form form2 = new Form2();

form2.FormClosed += (sender, e) => Visible = true;

Visible = false;
form2.Show();
}

That simply subscribes an event handler (in the form of an anonymous
method declared using a lambda expression) to the second form's
FormClosed event that will set the first form's Visible property back to
"true" when the second form closes.

Then the first form is hidden, and the second form is shown.


Ok, nice method, would of never considered it, but then again, I will
catch up. :)
You can write variations on that theme to handle other similar
scenarios, depending on your exact needs.


Overall, the EXE starts up

1) MainForm which is a dialup or telnet host entry connection
list view.

2) Selection begins a dialup/telnet and once connected the
MainForm is hidden and the site toolbar "Controller" comes up
with 7 buttons. A view of this is shown here:

http://www.winserver.com/public/wcnavigator.wct

3) Each button will launch currently an MFC EXE which I am
making .NET DLL forms. They need to be dynamically
loaded. They are independent of each other using a
generic virtual comm interface.

So there can have 7 separate windows open at the same time
(or more if the site has extra custom client buttons which
are added to the toolbar controller.

4) When the toolbar controller is closed or user disconnects
from the site, all the windows must be closed and the
MainForm site selector appears again.

So the controller needs to be aware of the windows open and close
then. I need to have it done generically.

I will definitely close the automation behind anonymous method.

Currently I was using this:

private void wcnavMainForm_FormClosing(object sender,
FormClosingEventArgs e)
{

FormCollection fl = Application.OpenForms;
Form formReshow = null;
foreach (Form f in fl)
{
Debug.WriteLine("{0}", f.Name.ToString());
if (f.Name == "wcNavConnectSitesForm")
{
formReshow = f;
continue;
}
if (f.Name != "wcnavMainForm")
{
f.Close(); << causes GPF in next loop
continue;
}
}
if (formReshow != null)
{
formReshow.Show();
}
}

Which is ok once that GPF is figured out, but silly to hang f "string"
tags. I will definitely will explore the approach you provided and go
from there. In short, each DLL will have a registration concept
because it will serve to keep user window data (settings) consolidated
save/read by the controller, but I may made that independent as well.

Right now I just need to get the basic wiring in .NET.

Thanks
 
H

Hector Santos

hmmm, peter, I am using VS2005. Is this code you provide for VS2010?

Peter said:
Hector said:
I need this flow:

Main form is display. You select something, it displays another 2nd
form and the mainform is hidden. When the 2nd form is closed, it
shows the hidden main form again.

Do I need to keep a global public instance. [...]

No. If you don't need the second form to explicitly know about the
first, there's no need to cause it to. Likewise, if there's not a
scenario where you literally must get at the form instance via some
static property somewhere, you don't need to add one for this particular
purpose.

Instead, do this:

void MethodInForm1ThatShowsForm2()
{
Form form2 = new Form2();

form2.FormClosed += (sender, e) => Visible = true;

Visible = false;
form2.Show();
}

That simply subscribes an event handler (in the form of an anonymous
method declared using a lambda expression) to the second form's
FormClosed event that will set the first form's Visible property back to
"true" when the second form closes.

Then the first form is hidden, and the second form is shown.

You can write variations on that theme to handle other similar
scenarios, depending on your exact needs.

Pete
 
P

Peter Duniho

Hector said:
hmmm, peter, I am using VS2005. Is this code you provide for VS2010?

No, but it does only work for 2008 or later (C# 3.0).

Other variations that are basically the same include the code that
"kndg" posted after my post (which uses a named method instead of an
anonymous method, and uses the Show() and Hide() methods instead of the
Visible property), and the code below (which uses a different way of
declaring an anonymous method):

void MethodInForm1ThatShowsForm2()
{
Form form2 = new Form2();

form2.FormClosed +=
delegate(object sender, FormClosedEventArgs e)
{
Visible = true;
});

Visible = false;
form2.Show();
}

That should work fine in VS2005, as would the code from "kndg".

Pete
 
H

Hector Santos

Peter said:
No, but it does only work for 2008 or later (C# 3.0).

Other variations that are basically the same include the code that
"kndg" posted after my post (which uses a named method instead of an
anonymous method, and uses the Show() and Hide() methods instead of the
Visible property), and the code below (which uses a different way of
declaring an anonymous method):


Yes, I got it to work usng Mr. kndg method.
void MethodInForm1ThatShowsForm2()
{
Form form2 = new Form2();

form2.FormClosed +=
delegate(object sender, FormClosedEventArgs e)
{
Visible = true;
});

Visible = false;
form2.Show();
}

That should work fine in VS2005, as would the code from "kndg".

Pete


Ok, why the concatenation? In case there was already a event for
FormClosed?

Is this a stacked concept? if so, does .NET call each one?

Other side issues?

Where are the pragmas in C# like in C/C++. I like to use if[def]
conditional compiles:

#if 0
#else
#endif

or

#ifdef XXXXXXXXX
#endif

To quickly enable/disable alternative logic, generally when testing ideas.
 
P

Peter Duniho

Hector said:
[...]
Ok, why the concatenation? In case there was already a event for
FormClosed?

No. That is the standard syntax for subscribing to an event.

http://msdn.microsoft.com/en-us/library/awbftdfh.aspx
Is this a stacked concept? if so, does .NET call each one?

An event is supposed to be a list of subscribers. This is usually
implemented implicitly by the compiler with the Delegate base type,
which automatically can concatenate two existing delegate instances into
a new delegate instance that refers to all of the invocation targets for
the two existing ones.

http://msdn.microsoft.com/en-us/library/ms173171.aspx
Other side issues?

Where are the pragmas in C# like in C/C++.

#pragma and other compiler directives are documented here:

http://msdn.microsoft.com/en-us/library/ed8yd1ha.aspx
I like to use if[def] conditional compiles:

#if 0
#else
#endif

or

#ifdef XXXXXXXXX
#endif

To quickly enable/disable alternative logic, generally when testing ideas.

C# only has "is defined" or "is not defined", tested with the #if statement.

http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

You should probably become familiar with the following sections of the
MSDN documentation:

http://msdn.microsoft.com/en-us/library/67ef8sbd.aspx (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/618ayhy6.aspx (C# Reference)
http://msdn.microsoft.com/en-us/library/ms229335.aspx (.NET Framework
Class Library)

There are actually lots of other useful areas of the documentation, but
the above will likely wind up being the source for answers to the bulk
of your initial questions.

Pete
 

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

Similar Threads


Top