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.
Related
I would like to know the difference between form.show() and form.activate().
I have multiple forms that already opened and i would like to active my form that is behind another form which is the best way to call my desired form
form.show() or form.activate()?
The method Show() displays the form to the user.
The method Activate() brings the form to the front (it gives the form focus).
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Activate();
}
}
The above code will show form2 by calling form2.Show(); but form1 will be in front of form2 (in focus) because of the this.Activate(); call.
See MSDN documentation:
Show()
Activate()
From the documentation
Form.Activate Method ()
Activating a form brings it to the front if this is the active
application, or it flashes the window caption if this is not the
active application. The form must be visible for this method to have
any effect. To determine the active form in an application, use the
ActiveForm property or the ActiveMdiChild property if your forms are
in a Multiple-document interface (MDI) application.
Form.Show Method
Showing the control is equivalent to setting the Visible property to
true. After the Show method is called, the Visible property returns a
value of true until the Hide method is called.
Answer
I have multiple forms that already opened and i would like to active
my form that is behind another form which is the best way to call my
desired form form.show() or form.activate()?
If your form is already open Activate it probably the one you want
Tip : If you ever wonder what a .net method does, just go and type it into google, usually the help is the first thing that shows up, plus a myriad of other questions and answers
form.activate() activates the form, which means if you have input elements (such as text boxes), it will focus to that particular form regardless of any other form out there. Eg. If you have shown form 1,2 and 3. And if you activate form 2, the form 2 will get focused to the user.
If you use form.show() it will just display/show the form to the user. Thus the activate() is gets the highest priority in terms of user engagement.
As per msdn Form.Activate() &
Form.Show()
Activate() :-
Activating a form brings it to the front if this is the active application, or it flashes the window caption if this is not the active application. The form must be visible for this method to have any effect.
Show() :-
You can use this method to display a non-modal form. When you use this method, the Owner property of the form is set to owner. The non-modal form can use the Owner property to get information about the owning form. Calling this method is identical to setting the Owner property of the non-modal and then calling the Show() method.
Showing the form is equivalent to setting the Visible property to true. After the Show method is called, the Visible property returns a value of true until the Hide method is called.
you can visit above links for more information
Now if you make your purpose more clear we may help in you in "Specific Way"
After reading the edit "Activate" is best , and also my answer is exactly identical to #TheGeneral
Form.Show() creates a new example of a form, while Form.Activate() brings an already existing form in the foreground.
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();
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;
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()
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.