.NET Compact Framework - modal Form without ShowDialog - c#

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.

Related

Prevent click of Form 1 when Form 2 is open in C# [duplicate]

This question already has answers here:
How to prevent clicks outside the current form?
(2 answers)
Closed 1 year ago.
I am creating an application where I have a Form 1 (Main Form). Inside this main form there is a button that opens up a different and smaller form (Form 2). I don't want to hide Form 1 when showing Form 2 but because of the size of Form 2, the user is able to click Form 1 hiding Form 2 with its size (not hidden like Form2.hide();) however this is something I don't want to do. I want to prohibit the user to click Form 1 if Form 2 is currently open. Is there any event or function I can use to do this? I have seen in it in other applications but I don't even know how to look for it.
In addition to using the ShowDialog method as suggested in question's comments, you can disable the form itself if you want for example the user be able to switch between forms and view or copy some text.
Form.ShowDialog Method: Shows the form as a modal dialog box.
Here is a definition:
Wikipedia: A modal window creates a mode that disables the main window but keeps it visible, with the modal window as a child window in front of it. Users must interact with the modal window before they can return to the parent application. This avoids interrupting the workflow on the main window. Modal windows are sometimes called heavy windows or modal dialogs because they often display a dialog box.
If you don't want to use a modal form, you can initialize the form2 instance like that:
form2.FormClosed += (_s, _e) => this.Enabled = true;
Thus now you can call:
this.Enabled = false;
form2.Show();
You can also check the ShowInTaskBar property of the form2.
Be careful not to add the lambda event handler several times to the same instance: if form2 is just hidden on close, only one FormClosed += is needed, but is required for every instance of a form you want to manage this way.
If you need to disable only certain controls, use them instead of this:
private void SetControlsEnabled(bool state)
{
myControl1.Enabled = state;
myControl2.Enabled = state;
myPanelHavingSomeControls = state; // this changes all inners too
...
}
form2.FormClosed += (_s, _e) => SetControlsEnabled(true);
SetControlsEnabled(false);
form2.Show();
This is the exact use case for a modal form, which disables the parent form and provides visual clues from the OS to tell the user of that relationship. Something along the lines of this will do:
//Code in the event handler that opens the child form
using (Form2 childForm = new Form2())
{
//Put any initialization code here
childForm.ShowDialog(this);
//Any cleanup or using some return value from the form after it closed
}

Difference between form.show and form.Activate

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.

ShowDialog in mdi

I need to open with Showdialog() in mdi form because I need to stop code until the mdichild finish.
The structure of program is this, I open a mdichild in onCreate of this I instead the class and in the constructor I open de SearchForm
There's the code:
frm_bsq_persona busqueda_persona = new frm_bsq_persona();
busqueda_persona.MdiParent = this.MdiParent.MdiParent;
busqueda_persona.Show();
Thank you very much.
MDI child as dialog form (MDI modal workaround)
http://www.codeproject.com/Articles/48436/MDI-child-as-dialog-form-MDI-modal-workaround
It works by disabling all of the controls on the parent form, and providing you with an EventReceiver method to receive the DialogResult from the dialog.
There are some caveats. State of the parent form is not preserved perfectly, since all controls are re-enabled on return. If you want that, you'll have to save the state of the parent form (or maybe just those controls that are already disabled) before creating the dialog, and restore the state when the Event Receiver is called.

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.

Categories

Resources