0

I am using Visual Studio 2019 Community. I developed an application and later I decide to insert a login form. I developed the login form however I couldn´t make login form initialize BEFORE main form.

How can I do that?

  • Does this answer your question? [How to setup the starting form of a winforms project in Visual Studio 2010](https://stackoverflow.com/questions/6981045/how-to-setup-the-starting-form-of-a-winforms-project-in-visual-studio-2010) – Luke Vo Dec 01 '21 at 20:39
  • 1
    @LukeVo - I don't think that is a duplicate. The issue is making a login form start first and then be able to close it and only show the main form, and if you close the form that opens the app the whole app shuts down. – Enigmativity Dec 01 '21 at 20:47
  • 2
    I've voted to re-open. I think the OP's issue is clear enough for anyone who has done winforms dev. – Enigmativity Dec 01 '21 at 20:47
  • 1
    It has been solved here https://stackoverflow.com/questions/21973274/how-do-i-show-a-secondary-form-before-the-main-form – I_Al-thamary Dec 01 '21 at 20:51
  • 1
    Add an event handler for MainForm Form Loading. In this event handler show your Login Dialog: `using (LoginDialog dlg = new LoginDialog()) {...}` Between the brackets you set the properties of your login dlg, and end with: `var dlgResult = dlg.ShowDialog(this);` This will show the login dialog before your main dialog shows. People have to finish the dialog, and you can return a DlgResult, which you can interpret in your main form event handler: `swhich (dlgResult)` if Ok, then read login data, if not, call `this.Close();` to close the application. – Harald Coppoolse Dec 01 '21 at 20:52
  • Use `Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new LoginForm());` – I_Al-thamary Dec 01 '21 at 20:55
  • 1
    @I_Al-thamary I liked your link more. When LoginForm closes the main thread is killed. This method can work, with more care but a novice may have trouble with it – djv Dec 01 '21 at 20:58
  • @I_Al-thamary - The link you've given does not show how to keep the main form alive after the login form closes. – Enigmativity Dec 01 '21 at 21:49
  • @HaraldCoppoolse - You should post your answer in the duplicate question. I don't think any of the answers in the duplicate are as good as yours. The duplicate seems quite terrible really. – Enigmativity Dec 01 '21 at 21:51
  • Just show your login form within main() in program.cs via ShowDialog() and capture the result. If good, continue on to the Application.Run() line with the main form within. Otherwise just let the app quit when it hits the end of main()... – Idle_Mind Dec 02 '21 at 00:51
  • @Enigmativity he needs to show it after login or as he likes – I_Al-thamary Dec 02 '21 at 04:23
  • 1
    @I_Al-thamary - If the form in the `Application.Run(new Form())` is closed the whole app shuts down. The issue here is to solve that. – Enigmativity Dec 02 '21 at 06:13
  • @Enigmativity They can use in the LoginForm `this.hide(); new MainForm().ShowDialog();` – I_Al-thamary Dec 02 '21 at 12:01
  • These are different methods [How can I close a login form and show the main form without my application closing?](https://stackoverflow.com/questions/4759334/how-can-i-close-a-login-form-and-show-the-main-form-without-my-application-closi/4759407) – I_Al-thamary Dec 02 '21 at 12:05
  • 1
    @I_Al-thamary - That's a far better duplicate. – Enigmativity Dec 02 '21 at 12:09

0 Answers0