Using multiple forms in C #

I am trying to create a small project that uses several forms (dialogs) for different states and starts in several problems. My dialogs are Login, Settings and Screen. When the application starts, the login form is displayed

Application.Run(new login()); 

from it, the user can open the configuration form or, if certain requirements are met, the display form.

Q1: how can I make the registration form inaccessible to the user when I open the settings form (I want the user to fill in the fields in the "Settings" form, then click the "Save" button to exit before he can do anything else in the Login form )

Q2: how can I hide the login form when the user opens the display form and displays it again when the user closes the display form.

for Q1: I do not have ideea, I just thought I could do the same thing as in Q2.

for Q2: I tried to submit a Dispaly form login form object to use the ShowDialog () method.

in the login form, I hide the form and show the display form as follows:

 this.Hide(); Display cat = new Display(ConString, idp, this); cat.ShowDialog(); 

in display form. I am trying to close the dialog when exiting and show the login form as shown below.

 private void Display_FormClosed(object sender, FormClosedEventArgs e) { this.Close(); this.l.ShowDialog(); } 

where l var is the Login object sent to the Display constructor, such as Login. the problem is that the display form does not close, and if the user clicks the button again, a new dialog will appear, and I want 1 instance of the form to be displayed.

thanks

+4
source share
3 answers

Q1 and Q2: when inside the login form code:

 using (SettingsForm frm = new SettingsForm()) { Hide(); frm.ShowDialog(this); Show(); } 

This is what you usually see when a form controls another form. ShowDialog will stop the selection of the parent form, you will see that your dialog flashes, and the system must make noise to indicate this.

ShowDialog also locks 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 its use.

+4
source
  • When switching to Settings from Login. In the login form, from where you go to the login, for example, when you click the click button, use the following ocde.

    Settings - new settings ();

     setting.show(); objLogin.close(); 
  • as mentioned above. with slight changes

+1
source

I would suggest creating a global login form object and use it in Display_FormClosed. ShowDialog () blocks the code and does not continue until the closed form is closed.

The login form waits for the Dislay form to close, and your display form calls ShowDialog in the login form.

+1
source

Source: https://habr.com/ru/post/1315644/


All Articles