Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi

 

I am developing an application which has to start up in different forms depending on which kind of user starts the application.

 

I have a startup class that identifies the user and starts up the correct form.

 

I start the form using:

 

Application.Run(frmA)

 

or

 

Application.Run(frmB)

 

Depending on the user.

 

This works ok, but the users are allowed to manually switch to the other form when they want to and this is where the problems start. By using the "Application.Run(frm)" I have tied the Applications lifetime to this one form and this creates all sorts of problems.

 

What should I do? I have played arround with the idea of creating an Appliacation thread which is not tied to any of the two forms and runs as long as the application does. Can this be done and is it good solution?

Posted
Well I don't know about an application thread, but why not try using a main form that is not visible (runs in the background) and do a call to your class to determine which form the main form should open. When a user switches, close the form and then show the other form. In other words, if I run the app, the main form determines that I should have frmA to start so the main form shows it. When I want to switch, the code can close frmA and then the main form will call to show frmB, I guess you can use an addhandler to handle the closing of either form to show the other form immediately. But just an idea.
Posted

That definitely could be a way to go about it and i might end up doing this. But it's a bit of a dirty solution isn't it? To have an invisible form that really isn't a form because the only thing it does is to control the other forms.

 

any other suggestions to my little problem :-)

  • *Experts*
Posted

You have the right idea - using Application.Run() will create a running thread that stays alive until you use Application.Exit(). When you call Exit is up to you. One solution might be to have every form register itself into some static class/array/hashtable on load and remove itself on unload. When unloading, simply see if it's the last form and have it issue Application.Exit. You can wrap all that up in a base form (the registering, unregistering, and Application.Exit) if it works for you (you don't already have a base form).

 

As for creating/showing the first or subsequent forms, you simply do something like:

Form startupForm = null;
if(myProperty == true)
{
   startupForm = new Form1();
}
else
{
   startupForm = new Form2();
}
startupForm.Show();
Application.Run();

Hopefully that will spark some ideas...

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...