Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 sent 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

like image 909
Bogdan Avatar asked Jul 14 '10 10:07

Bogdan


People also ask

Can you have multiple forms C#?

Once the application is started, the form show/hide strategy is simply multiple forms (non-MDI). There may be one or two modal forms.

How do I create a 2nd form in Visual Studio?

Add a new form In Visual Studio, find the Project Explorer pane. Right-click on the project and choose Add > Form (Windows Forms). In the Name box, type a name for your form, such as MyNewForm. Visual Studio will provide a default and unique name that you may use.


1 Answers

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.

like image 135
Adam Houldsworth Avatar answered Oct 25 '22 13:10

Adam Houldsworth