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.
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.
In a multiple projects solution I had a startup WinForms project with 2 Forms, expecting a DialogResult whenever the child Form was shown to continue execution in my main Form.
var form2 = new ThisNameSpace.FormChild();
var dResult = form2.ShowDialog(this);
if(dResult == DialogResult.OK)
{
// Do some work
}
else
{
// Do other work
}
For some reason, I had to move the child Form in another project. I've referenced the new Project's dll in my main startup Form project but I'm now unable to return a dialog result. When my child Form closes the FormClosing event of the main Form is raised with CloseReason.None and the application exits. How can I work this around?
You should not raise FormClosing event yourself, and you should not write any custom code to just close the dialog.
Instead do this:
on your OK button in child form, set DialogResult property to OK
on your child form, set set Accept Button property to point to your OK button
that's all you need to close the window and correct DialogResult will be returned.
If you ever need to close it 'manually' (and this is rare for modal dialogs, i.e. those opened with ShowDialog instead of Show), use Close method.
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.
I have a dialog-based application.
I need to show a child window like this:
(1) First, the application's main dialog window will show up,
(2) then, a child dialog window will show up automatically on top of that.
You know, it is not enough to call the child window's ShowDialog() in the parent window's constructor or load event. Coz in those cases, the child window will appear first.
What should I do to achieve that?
Use can use the event Shown of your main dialog, to show the child in front of you main dialog. This event is only raised once, when the main dialog is shown the first time. Also you should use the Show() (not ShowDialog) method and then call BringToFront() of your child dialog.
private void OnShown(EventArgs e) {
ChildDialog child = new ChildDialog();
child.Show(this);
child.BringToFront();
}