ShowDialog exiting on certain events - c#

I have a main form, but once I show the main form, I use ShowDialog to pop up my login form, and I hide the main form. Once the user logs in, I just close the login form and everything works fine, the main form shows up.
However.. when I exit the login form, it still shows the main form, because, obviously, it's a dialog. How can I prevent the user from reaching the main form when they exit the login form?
Thanks.

I see such options:
don't allow exiting the login dialog (unless together with closing along the whole application)
if user exists the login dialog disable the whole main form (set Enabled = false on it's crucial controls and the whole form). Probably except the main menu when you can exit the application or invoke the login dialog again

Use the result of the ShowDialog() to determine if the user has logged in or not. If not, just close the application, otherwise continue the application.
Another but less nice solution: set a Main-form property in your login-form and set it when you open the login form. In the closing event of the login-form you can also close the main form.
//the property in the login-form
public YourMainForm MainForm{ get; set; }
//setting the property from the main-form
var login = new LoginForm();
login.MainForm= this;
//closing of the mainform
MainForm.Close();

You don't need to hide the form. just add the controls to a panel and make it invisible.
panel1.visible=false;

Related

Exit application by clicking button on mainform when already pop up form is running

Form2 is user confirmation window,i want to close the application by stop button on mainform,but currently form2 is active and there is no dependency between form2 and mainform.How to close application ?
You must be opening Form2 using ShowDialog() method. Instead of that use Show() method and on MainForm button's click event call Application.Exit() method
You can use...
// for WinForms application
if (System.Windows.Forms.Application.MessageLoop)
{
System.Windows.Forms.Application.Exit();
}
// for Console application
System.Environment.Exit(1);
So your problem is the parent window will not have focus/input until the modal window (the confirmation dialog) has finished.
So, to avoid this, just make the confirmation dialog using a custom window (instead of a common dialog / msgbox) and show it. This way if the user wants to click your main form they can (the confirmation will be on background, covered by the main window because of this).
it looks like you're showing the dialog as ShowDialog() or showing a built in MessageBox which is also a ShowDialog that needs to be closed in order for focus to return back to main window. So what you should do is create a custom Dialog and use dialog.Show() method. This will allow you to interact with your main window while the dialog is open and you can click on a button to close your application regardless of the dialog being open or not. Following code should do the closing work for you
Application.Current.ShutDown();

Which form event should I use to pop up a login form?

Before the user makes use of the main form, I want to pester them to enter their username, password, and siteNumber.
Should I invoke a ShowDialog on the login form from the Main form's Load(), Activated(), GotFocus(), or Validate() events? Or should I just invoke the login form from the main form's constructor?
This is a Windows CE app, and those are the only options I have, event-wise (there is no "Shown" event).
Have you considered prompting via ShowDialog before you even get to the main form?
Put the code right in the Main() someplace before the Application.Run(new MainForm()).
Works fine, I've done this before.

Place form on top and disable others behind it

I have a Main Form and a Other Form.
The Main Form is always open, and at some time it will launch Other Form.
I tried:
form.TopMost = true;
But this only places the form on top. The Form behind (Main Form) still can be accessible.
How can I get the same behavior like when I do OpenFileDialog, and disable the main form behind it?
(Thanks in advance)
You need to make your form modal. To do this, use ShowDialog instead of Show to display it (the same way you do with a dialog).
Also, note that forms shown with ShowDialog are not actually closed and disposed when you click their Close button, so you should dispose them manually. The usual way to handle their lifetime is to use a using construct:
using (var form = new SomeForm())
{
form.ShowDialog();
// do stuff after the dialog is closed
}
form.showdialog(); where form is the the top form to be launched.so while lauching the top form just add form.showdialog()

Form does not load after DialogBox

I have a dialog form:
_myLogin = new LoginPage(_myDriver);
_myLogin.ShowDialog();
and then I want to load another form
new RegularUser().Show();
Why dosn't my second form load?
Or you can simply call new RegularUser().Show(); in the load method of your LoginPage form.
Because your are showing your first form as a Modal Dialog which must be closed or hidden before the user can continue working with the rest of the application, like a MessageBox.
You can instead use:
_myLogin.Show();
to display it as a Modeless form.
From the link:
If a form is displayed as modal, the code following the ShowDialog method is not executed until the dialog box is closed. However, when a form is shown as modeless, the code following the Show method is executed immediately after the form is displayed.
I believe the second form doesn't load because you've not closed first Modal dialog form.
ShowDialog() displays Modal dialog window
MSDN, Form.ShowDialog Method:
When this method is called, the code following it is not executed
until after the dialog box is closed.
Try out using Show() method instead, so WIndow would be shown and all code after Show() call will be executed as well.
PS: Considering your form names, may be Modal dialog is the right decision for LoginForm? SO user see LoginForm, enter credentials, and only after the successfull authorization would be able access RegularUser form, I'm not sure about this because just assuming having only form names, but anyway Modal dialogs are excatly for such cases.

.NET Compact Framework - modal Form without ShowDialog

Is there a way to show a modal form without ShowDialog metod calling? By showing a modal form I mean that the Form overlays the current Form and prevent user input to the bacground Form. The Form that is to be shown is a MessageBox style form that is not fullscreen.
Thanks
Dominik
I would assume that you could set the "dialog" form to stay on top (TopMost property) and then disable the main form in order to prevent input.
This is only a partial solution as the main form will still be able to be focused, closed etc. You will need to handle all these cases yourself in order to prevent them.
Is there any particular reason you don't want to use ShowDialog?
You could try to do this:
MyForm frm = new MyForm(); // this would be your modeless dialog
frm->show(this); // "this" being the instance that invokes it.

Categories

Resources