Hidden main form, use of delegates etc

  • Thread starter Stephen Haeney via .NET 247
  • Start date
S

Stephen Haeney via .NET 247

Rather than developing an application with numerous frames andhiding/showing them as required, I decided to investigatestarting my Winform application via a class and using delegatesto decide which form to show as the main form.

Below is a code snipet

class PrerequisiteInstaller
{
private static bool exitApplication = false;

private delegate void OnDialogResultDelegate( );
private static OnDialogResultDelegate currentDelegate = null;
private static System.Drawing.Point formLocation;

public static void Main()
{
currentDelegate = newOnDialogResultDelegate(CheckPrerequesites);
formLocation = new System.Drawing.Point(20,20);
while( exitApplication == false )
{
currentDelegate();
}
System.Diagnostics.Debug.WriteLine("Application exiting");
}

private static void CheckPrerequesites()
{
frmCheckPrerequesites firstForm = newfrmCheckPrerequesites();
firstForm.Location = formLocation;
if ( firstForm.ShowDialog() ==System.Windows.Forms.DialogResult.OK )
{
currentDelegate = newOnDialogResultDelegate(CheckServicePacks);
}
else
{
exitApplication = true;
}
formLocation = firstForm.Location;
}

private static void CheckServicePacks()
{
frmCheckServicePacks secondForm = new frmCheckServicePacks();
secondForm.Location = formLocation;
if ( secondForm.ShowDialog() ==System.Windows.Forms.DialogResult.OK )
{
// Move to next screen
}
else
{
System.Diagnostics.Debug.WriteLine("Must deal with thempressing EXIT!!!!!!! in this and all other screens");
currentDelegate = newOnDialogResultDelegate(CheckPrerequesites);
}
formLocation = secondForm.Location;
}
}

Using this mechanism, you have to set the window start positionto manual and then save/set it for each new window, or the callto ShowDialog will move the next window to be displayed.

The only problem with this approach is that it seems slow - thewindows flicker between button presses on the forms. Of course,I could implement this using a giant switch statement, based onthe current "state" of the application, but using delegatesseems far more elegant.

Does anyone have any idea why this approach is slow? or is therean easier way to do this?

Thanks.
 
H

Helen W

I use a similar solution using delegates to control the flow of pages in a
wizard dialog. Each page is actually a usercontrol displayed on the form. My
User Process Interface class uses delegates to create/destroy the
usercontrols as required.

An immediate workaround for you would be to change to that approach - have
just one placeholder form and re-create your other forms as usercontrols.



Rather than developing an application with numerous frames and
hiding/showing them as required, I decided to investigate starting my
Winform application via a class and using delegates to decide which form to
show as the main form.

Below is a code snipet

class PrerequisiteInstaller
{
private static bool exitApplication = false;

private delegate void OnDialogResultDelegate( );
private static OnDialogResultDelegate currentDelegate = null;
private static System.Drawing.Point formLocation;

public static void Main()
{
currentDelegate = new OnDialogResultDelegate(CheckPrerequesites);
formLocation = new System.Drawing.Point(20,20);
while( exitApplication == false )
{
currentDelegate();
}
System.Diagnostics.Debug.WriteLine("Application exiting");
}

private static void CheckPrerequesites()
{
frmCheckPrerequesites firstForm = new frmCheckPrerequesites();
firstForm.Location = formLocation;
if ( firstForm.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{
currentDelegate = new OnDialogResultDelegate(CheckServicePacks);
}
else
{
exitApplication = true;
}
formLocation = firstForm.Location;
}

private static void CheckServicePacks()
{
frmCheckServicePacks secondForm = new frmCheckServicePacks();
secondForm.Location = formLocation;
if ( secondForm.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{
// Move to next screen
}
else
{
System.Diagnostics.Debug.WriteLine("Must deal with them pressing EXIT!!!!!!!
in this and all other screens");
currentDelegate = new OnDialogResultDelegate(CheckPrerequesites);
}
formLocation = secondForm.Location;
}
}

Using this mechanism, you have to set the window start position to manual
and then save/set it for each new window, or the call to ShowDialog will
move the next window to be displayed.

The only problem with this approach is that it seems slow - the windows
flicker between button presses on the forms. Of course, I could implement
this using a giant switch statement, based on the current "state" of the
application, but using delegates seems far more elegant.

Does anyone have any idea why this approach is slow? or is there an easier
way to do this?

Thanks.
 

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