How do I hide or close the MDI parent? - c#

In my project I have a MDI parent form, from that form I jump in my main program but when I enter my main program I want to close, hide or minimize the MDI , so it isn't visible for the user. I tried it by myself but it didn't work. I was wondering if it is possible and how it is possible.
private void bot45_Click(object sender, EventArgs e)
{
Form2 fors = new Form2(); //main program
fors.Show();
//bot 45 is the button to enter the main form
// enter main program--->> code to hide close or minimize the MDI form
}

Related

Taskbar icon opens the wrong Active form

Having a strange behaviour with my application whereby when navigation between web browser (or any other application) and clicking back into the application seems to open the wrong Form? So user literally has to make use of Tab window to open the correct Form.
For e.g. Form1 is the main form. User clicks a button which opens Form2. Form1 is hidden behind the scene while Form2 opens. Now if user goes to a different application e.g. browser and clicks back into the application Form1 is displayed and there is no other way of going back to Form2 without the Tab window?
I have used the .ShowDialog() property when opening Form2 which disabled the parent form Form1 but still seems to do it occasionally?!?
I have also set the ShowInTaskBar for Form2 to False so that there is one icon in taskbar for all forms.
Not really sure what can cause this behaviour to happen?
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
I think you need to tell Form2 who its owner form is.
Like this:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
see System.Windows.Forms for more information
When you open form2, you need set mdiParent of form1.
First, set form1 as "isMdiParent" to "true" in properties, and then use the follow code:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}

How do I make a button make a form in another form?

So in my current situation I have my Desktop form set to open things inside that form through the MDI.Container.
I have a form automatically open when the Desktop form is started. After reading that form you would click "next" on the form button and it SHOULD open the new form in the same mdi container, but instead it opens it in a new window in my actual OS. I want it to open in the original MDI container in the Desktop form.
If you can help me out please do!
I've tried the following:
this.IsMdiContainer = true;
Welcome welcome = new Welcome();
welcome.MdiParent = this;
welcome.Show();
Would this help? I'm not sure to get your question
// C#
protected void MDIChildNew_Click(object sender, System.EventArgs e)
{
Form2 newMDIChild = new Form2();
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
the problem is you make the form ismdicontainer = true inside code you should make form ismdicontainer true while designing, not in run time.

Better way to Display forms one on top of another

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();
}

3 forms Show and ShowDialog not working as expected, BUG?

I am using Visual Studio 2010, C# .NET 4.0. I have 3 forms: Form1, Form2, Form3.
In Form1 I have a button to open Form2:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
In Form2 I have a private Form3 variable always pointing to the same Form3:
private Form3 f = new Form3();
And a button to open it as a dialog:
private void button1_Click(object sender, EventArgs e)
{
f.ShowDialog();
}
In Form3 I just have a button to hide the form:
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
}
The problem is that having the situation that Form2 is in front of Form1, and Form3 in front of Form2, when I click the button of Form3 to hide it, it not only hides itself but sends Form1 to the back of all of the other Windows.
This only happens when there is a window of another program (such as Windows Explorer) in the background of Form1. It seems like a bug. What do you think?
Yes, this cannot work properly by design. A dialog disables all of the windows that your program displays. So that it is modal. When you hide the dialog, there are no windows left that can get the focus. Windows is forced to find another window to give the focus to. That will be a window owned by another application. Your own windows will now hide behind it.
There are more side effects, the dialog will also close. Necessary because otherwise the user can never get back to your program anymore since all windows are disabled. This is all unsurprising behavior. Bug would be a strong word, but it would of course work better if it first re-enabled all windows before closing the dialog. But closing the dialog is already undesirable behavior.
Don't call Hide() for a dialog. Just set the DialogResult property to DialogResult.Cancel to achieve the exact same effect, minus the focus problem. You do have to reset it back to None if you want to display the dialog again. That's a real bug.
By the documentation. Form.Close method doesn't dispose forms shown by Form.ShowDialog method. Quote:
The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
So, maybe there are ways to return focus to your application (e.g. via Windows API). But it is much more convenient to call Form.Close manually on dialog windows.

C# Form Problem: new form losing control and randomly hiding

I'm encountering strange behavior with forms on a c# 3.5 app. On a button click, my form1 hides itself, creates a new form2, and shows form2. Form1 also contains the event method triggered when form2 closes. Here's the code inside Form1:
Form2 form2;
void button1_Click(object sender, EventArgs e)
{
this.Hide();
form2 = new form2();
form2.Show();
form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
}
void form2_FormClosed(object sender, FormClosedEventArgs e)
{
form2.Dispose();
form2 = null;
this.Show();
}
Now, my problem is that sometimes when I open form2 (hiding form1), or when I close form2 (showing form1), the new form will come up on the screen for a blink and then hide itself. It's still open and I can click it from the taskbar to show it again, but the window itself is sent behind any other open windows. It looks like it opens up but minimizes instantly.
This behavior occurs randomly. Sometimes forms will open up and hide without a problem, but sometimes they'll lose focus over another window. I've tried using focus(), activate(), and topmost but all have failed to prevent the sudden hiding.
Does anyone know why is this happening and how to fix it?
Thanks.
You hide your form too soon. For a brief moment, your app has no window that can contain the focus. That forces Windows to go hunting for another window to give the focus to, it will pick one from another application. That window will now be the foreground window, your second form will not get the focus and appear lower in the Z-order. The fix is simple:
void button1_Click(object sender, EventArgs e)
{
form2 = new form2();
form2.Show();
form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
this.Hide(); // Moved
}

Categories

Resources