Using Multiple Forms in c#
i'm trying to make a small project that uses multiple forms (dialogs) for different states and ran in a few problems. My dialogs are Login, Settings and Display. When application is started Login form is displayed
Application.Run(new login());
from it the user can open Settings form or, if certain requirements are met, the Display form.
Q1: how do i make Login form unavailable to user when Settings form is opened (i want the user to complete the fields in the Settings form, then click 'save' button to exit, before he can do anything else in Login form)
Q2: how do i hide the Login form when user opens the Display form and show it again when user closes the Display form.
for Q1: i have no ideea, i just thought i could do the same as in Q2.
for Q2: i tried to send the Login form object to the Dispaly form to use ShowDialog() method.
in Login form i hide the form and show the Display form like this:
this.Hide();
Display cat = new Display(ConString, idp, this);
cat.ShowDialog();
in Display form i try to close the dialog on exit and show the Login form like this
private void Display_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
this.l.ShowDialog();
}
where l var is the Login object se开发者_JAVA百科nt to Display constructor, of the type Login. the problem is that Display form is not closing and if user clicks display again a new dialog will show and i want max 1 instance of Display form.
thanks
Q1 & Q2: when inside the login form code:
using (SettingsForm frm = new SettingsForm())
{
Hide();
frm.ShowDialog(this);
Show();
}
This is what you would usually see when a form is in control of another form. ShowDialog
will stop the parent form from being selectable, you will see your dialog flash and the system should make a noise to indicate this.
ShowDialog
also blocks until the form closes, so it will wait until you close the form anyway. You can repeat this code for any form that spawns another form and needs to wait for it to be used.
When you switch to Settings from Login. On login form from where u are switching to login like some button click event use the following ocde.
Settings setting = new Settings();
setting.show(); objLogin.close();
same as above. with slight change
I would suggest creating a global Login form object, and using it in the Display_FormClosed. ShowDialog() blocks the code and doesn't continue until the form called is closed.
The login form is waiting for Dislay form to close, and your Display form calls ShowDialog on login form.
精彩评论