Winforms - changing form content from one context to another
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 / bes开发者_如何学运维t 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:
private void MainForm_Load(object sender, EventArgs e)
{
LoginForm login = new LoginForm();
login.ShowDialog(this);
if (login.DialogResult != DialogResult.OK)
{
Close();
}
else
{
// Init your application ...
}
}
Code in LoginForm:
private void btnOk_Click(object sender, EventArgs e)
{
if (DoSomeLoginLogic())
DialogResult = DialogResult.OK; // setting this will close the form
}
IMHO, I wouldn't do it on one form, but you could have your login dialog being a modal dialog of your main app dialog. When the user then clicks login, you can just close the dialog.
Call it again when the user logs off.
A single form is the way to go, once it succeeds then the program can continue to the next form.
Create a single form exposing username/password fields with two buttons "Accept"/"Cancel".
Create a model class that handles login attempts and success behaviour.
public class LoginModel {
private LoginForm view;
public LoginModel() {
view = new LoginForm(this);
}
public void Show() {
view.Show();
}
public bool LoginAttempt(string user, string pass) {
// Process login attempt (call LDAP backend etc...).
}
}
In your LoginForm, you need to call LoginAttempt(string, string)
from the OnClick
event of the login button, this can then be put into a background thread if you wish. once the login attempt is successful you can call the Close
method.
In your model you will also need an event to tell the caller about the success failure.
An alternate approach can be achieved with reopening a form similar to the approach I used here in a sample application demonstrating prism (CWPF Samples), if you look at the late-night project.
精彩评论