I'm launching a form from another form. I want the parent form to be disposed before showing the subform, so I use:
this.Dispose();
form.ShowDialog();
The form is displayed, is this the correct way to do it? The form from is launched by a call to ShowDialog() from the main form. I want these forms to be closed before the main form is editable.
EDIT:
This is the Basic Process
Mainform>(Showdialog)>form1>(dispose+showDialog)>form2(dispose)>Mainform
If you want to ensure the parent is not visible before the child is shown, AND you want the parent to Close:
var f = new ChildForm();
this.Hide();
f.Show();
this.Close();
ShowDialog() means, "display this form and return to me after it's closed". That is not what you described you want to happen.
Based on your comment, it might make more sense to create and show form2 from your main form.
In the main form:
DialogResult form1Result;
using (var f = new Form1())
{
form1Result = f.ShowDialog();
}
if (form1Result == DialogResult.OK)
{
using (var f2 = new Form2())
{
f2.ShowDialog();
}
}
And in Form1 event where you originally spawned Form2
this.DialogResult = DialogResult.OK;
this.Close();
Calling Dispose on any object (including the current object known as this) is saying you have finished with that object, it is terminally finished with.
Better to Close the first dialogue and then show the second one.
If you are showing the child dialog modally (which you are in this case) then you can hide the parent dialog What you can do is hide the parent form, show the child form, and then close the parent form.
var form = new SomeForm();
this.Hide();
form.ShowDialog(); // the code will be blocked at this point until the child form closes
this.Close();
this.Dispose(); // i'm not sure if this is necessary anymore. check MSDN
If you are showing a form (not modally), you can hide the parent form, and listen to the child form's close event and then close the parent form in the handler
void childFormClosed(Event e)
{
this.Close();
}
var form = new SomeForm();
form.Closed += new EventHandler... // I usually rely on intellisense with this
this.Hide();
form.ShowDialog();
sorry, I think you cant do that, because when you call ShowDialog method, the child form becomes dependent on its parent form. so the call of dispose will be suspended until you finish with the child form.
I think you have to call the "Show" method and after that you can call the dispose method.
The lifecycle of the child form is depends on its parent lifecycle, that means, once you invoke the parent.close of the parent form, it will dispose all objects created by that instance.
You have to create the child independently from the parent.
Related
I have a MDI child (FormA) form that opens and displays a dataGridView.
The user has the ability to make changes to a record in the database by opening another form (FormB) that is outside of the MDI. Meaning that it is called with FormB.ShowDialog() rather than FormB.Show().
FormA has an activated event that I want to be thrown when the user returns from FormB. I do this to refresh the datagridview's datasource.
It seems that FormA never loses focus and I need to in order for the activated event to be thrown when the user returns. Is this by design? How can I achieve this functionality?
EDIT
FormB is called from FormA's MDI Parent. not FormA itself.
Why do you need Activated event at all? The FormB.ShowDialog() opens FormB in a modal state, so execution of the code in the calling form FormA is interrupted.
See the sample (to use in the FormA):
using (FormB frm = new FormB())
if (frm.ShowDialog(this) == DialogResult.OK)
{
// here FormB is closed by user.
}
EDIT
Since FormB is opened from the MDI parent, you can save the currently active form to variable and then pass the execution to it. Try this in the MDI parent form:
FormA saveForm = ActiveMdiChild as FormA;
using (FormB frm = new FormB())
if (frm.ShowDialog(this) == DialogResult.OK)
{
if (saveForm != null)
saveForm.UpdateDataGridView(); // Call the method of a previously active form.
}
You can also try the ActivateMdiChild method to activate a child form.
I have a Main form.I want to launch another form from it and launch another from the launched form.I want to ensure that the Main form is not editable when sub forms are displayed so i use showdialog()
Mainform>(Showdialog)>form1>(showDialog+dispose)>form2(dispose)>Mainform
From Mainform i call form2.ShowDialog() then from form2 i use the following code to launch another form
this.visible=false;
form3.showdialog();
this.dispose();
But there are some problems in this.Is there a better way to achieve what im looking for?
edit:more description
I have a Main form,User clicks a Button on Mainform>Form1 is lauched>User clicks a Button in Form1>Form 2 is lauched(diposing/hiding form1) after form2 is closed Mainform should be brought to front and made editable,until then all other forms should be on top of Mainform and Mainform should be un-editable
The problem is that you have to specify the MainForm as the parent for (both) form2 and form3. When you use the overload of ShowDialog that has no parameters, WinForms uses the active form as the parent, so form3's parent becomes form2 automatically. You are then trying to close/dispose form2 causing form3 to become orphaned.
There are several options for getting the reference to MainForm, but the simplest is to use:
form2/3.ShowDialog(Application.OpenForms["MainForm"]);
Assuming that you have set the Name property on MainForm to "MainForm".
In your code, this.dispose() is executed only after form3 is closed. What i think you want is to close form2 after form3 was closed, so you can call this.Close() instead of this.Dispose().
this.visible=false;
form3.showdialog();
this.Close();
Or maybe, after form3 is shown up you dont need form2 any more. That meas:
this.visible=false;
//show instead of showdialog so it wont wait until form3 is closed
form3.show();
this.Close();
It looks like you are trying to implement something like a wizard. The best solution would be to launch all the child forms sequentially, in the main form.
If you need to pass data along the sequence, you should pass it from each dialog to the main form, which then passes it to the next dialog.
MainForm:
Form1 f = new Form1();
if (f.ShowDialog(this) == DialogResult.OK) {
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
Form1 (button click which will open the form 2):
button1_click(object sender, EvengArgs e) {
this.DialogResult = DialogResult.OK;
Close();
}
everyone.
How do I call a number of forms one after another (meaning call new form after previous one is closed).
I wrote something like this;
Form1.Show();
Form2.Show();
and it naturally results in opening both forms simultaneously. How to make function to wait for the fist form to be closed?
Open one form, handle the FormClosed event, and open the second form from within that. Note that this is only necessary because your forms are not modal. If you were using ShowDialog() the call would not return until the first form was closed and your code would work as it is currently structured.
If you require non-modal behavior (i.e., your user must be able to interact with the owner form while the owned form is open) then use something like this:
// very simplistic example...
Form1 frm = new Form1();
frm.FormClosed += delegate { new Form2().Show(); }
frm.Show();
I have two forms named frmRegistration & frmMain in my project in c#.
I have set frmRegistration as my start form.
User enters data in the frmRegistration form & presses submit button to get registered. Then, I want to close frmRegistration form & show frmMain form to the user.
I'm trying this by using Dispose() method of the frmRegistration. But, when I use this method, it disposes all my application execution because frmRegistration is the startup form.
I don't want this to happen. Can anyone solve this problem?
thanks.
Use Show() and Hide() methods.
private void btnSubmit_Click(object sender, EventArgs e)
{
...
var frm = new frmMain();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.Show();
this.Hide();
}
UPDATE:
If you don't want to have frmRegistration in memory, start your program in main form and add this in your MainForm's Shown event:
var frm = new frmRegistration();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.FormClosing += delegate { this.Show(); };
frm.Show();
this.Hide();
Now you can just close the registration form and automatically get back to main form.
Try setting frmMain as start up form and hiding it initialy, show frmRegistration, do what you have to do, and Dispose it.
You can also change you Program.cs main class with the Main() function to start frmRegistration and after positive DialogResult or another check it will then start with frmMain - as your main form and message loop.
At least there are two options:
1.Turn your Start up form into a singleton
When you need to hide it, call it's hide method
2.Have new different startup form, call it MainApp form or whatever, have it set to invisible, the you can do what ever you like with the other non-startup forms.
I have a windows form that pops up a dialog box if certian conditions are met when the form loads. The problem is the window does not stay on top and I can still click thing on the parent. However, there is a button on the form that when pressed opens the same window, when I do this it works as expected (like a dialog window).
Is there an issue with showing a dialog when a form is first loading?
Are you calling ShowDialog from the Form class? Because it will only set the parent window if called from another Form. Alternatively you can use the overload that has the IWin32Window parameter to specifically set the owner.
can you explain the issue further as this is my code which do not show the form it self until the dialog has been closed either you set the parent or not
private void Form1_Load(object sender, EventArgs e)
{
//your functionality goes here
AboutBox1 box = new AboutBox1();
box.ShowDialog();
}
}
on the other side you can also check with TopMost property
The ShowDialog method needs to be called from the form that you want to be it's parent/owner in order for it to be modal to that form. Alternatively I believe you can set the owner of a dialog directly but I have never needed to do that.
DaBomb,
To do what you want, you will have to call your modal dialog from the constructor of your main form, NOT from the Form_Load event.
Something like this:
public Form1()
{
InitializeComponent();
this.Show();
Form2 popupForm = new Form2();
popupForm.ShowDialog();
}