I’m an ASP.NET developer who has been assigned a small Winforms application task. I’m doing the following:
- I have a form with login fields and button
- After successful login I want to provide the main function of the app.
- At the moment I’m using two forms – one for login (form1) and one for the main function (form2) – making form2 visible and form1 invisible when I swap between them.
I feel sure I should be doing everything on the single form. What is the normal / best approach for doing this?
Thanks,
Richard.
I think that most common approach in Windows world is using 2 dialogs. For example login to Skype, Steam, Remote Desktop, SQL server (using SQL Management Studio), etc.
Windows users also got used to common behavior of login dialog (here I mean that clicking “Cancel” button will close the application) and there is no other disturbance like application’s main menu or other controls.
I suggest showing Login form in your Main form’s FormLoad event. Use ShowDialog method and check return value to determine if Login form was closed with success.
For this to work you need to
in your “Ok” button’s click handler set LoginForm.DialogResult property to DialogResult.OK (or set DialogResult property directly on “Ok” button using Object Inspector)
using object inspector set DialogResult for your Cancel button to DialogResult.Cancel
using object inspector set your LoginForm’s AcceptButton and CancelButton properties to appropriate buttons
Code in MainForm:
Code in LoginForm: