Reputation: 179
I want to display dialog, that ask user to select directory, and if everything is ok the main form will be displayed, otherwise program will close, and i want to make it gently, but i thing my solution is not.
In Program class (Program.cs) i defined method which calls dialog, and depending of its result Main method will call form, or not.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (DisplayFolderDialog())
Application.Run(new addonInstallerForm());
}
Is there better way to achieve this?
Upvotes: 2
Views: 1588
Reputation: 941237
It is okayish, you usually get away with it. There are however two usability problems with this approach:
For a split second, your program does not have a window that can receive the focus. Right after the dialog closes, it can take some time before your addonInstallerForm window appears. That can be a problem, the operating system needs to give another window the focus. If it is not a split second, but takes several seconds for the main window to appear, usually because of overhead due to finding assemblies and disk and jitting code, then Windows decides it ought to give the focus to another app. And your main window disappears behind the window of that other app. Greatly confuzzling the user of course, he might even think that your program crashed.
Unless you do something about it explicitly, your main window will appear on the screen in a different position and with a different size than the dialog. Not great, that tends to disorient the user as well, although not nearly as badly as the disappearing window problem of course.
These are problems that are somewhat hard to solve because you use ShowDialog(). It is of course not actually necessary to use a dialog, it merely makes it easier to write the code. Note how most programs you use everyday don't do this. They have for example a File + Open command. Or they have a wizard-like UI, moving you from one window to another.
The most common reason to reach for ShowDialog() is to work around the problem that Winforms automatically terminates the program when the main window is closed. Simple to solve with this code. Lets you create a wizard-style UI with different forms. Another very cheap way to create a wizard is to use a TabControl without visible tabs. You can still use a dialog, just expose an event that you fire just before you close the dialog. Etcetera.
Upvotes: 3
Reputation: 5866
I always do my applications like you. I have a login page that comes before the main form. I think it is one of the best way. You should stick with it. here is an example of my Login logic
static void Main()
{
Application.SetCompatibleTextRenderingDefault(false);
Application.EnableVisualStyles();
DialogResult result;
using (var loginForm = new frmLogin())
{
result = loginForm.ShowDialog();
}
if (result == DialogResult.OK)
{
// login was successful
Application.Run(new Form1());
}
}
Upvotes: 1