This one has really got me stumped. I have certain forms that are being instantiated. When I instantiate a form i make it a child of the mdi form by
form1.MdiParent = this;
I have set the MDIWindowListITem property of my menustrip to a toolstripmenuitem
However this toolstripmenuitem does not show the mdi child form when it is instantiated
Does any one have any ideas on this?
Any inputs/ leads / hints would be most welcome. I am using .net framework 3.5
Regards
,
You have to write your code so that it is added manually I believe.
See the example here for pointers:
MSDN help on ToolStripPanel
Edit
You're right ignore my previous entry here's the code for a very simple MDI app that appears to do what your after.
It is just two blank forms. Form1 has IsMDIContainer=true. It also has menuStrip1, which contains two items "new" (newToolStripMenuItem) and "windows" (windowsToolStripMenuItem). Clicking new will open a new child window. I have set the MDIWindowListItem of menuStrip1 to windowsMenuStripItem. When a new child window is opened clicking on windowsMenuStripItem produces a drop down that shows all windows open.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int count;
public Form1()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
count++;
//Set a window title text as this is what is shown in the window list.
Form2 newForm = new Form2() { Text = string.Format("Window {0}", count) };
newForm.MdiParent = this;
newForm.Show();//<--- this needed to show window in list.
}
}
}
There is no code in Form2.
The child windows only show below windowMenuStripItem once Form.Show() has been called.
Without this they do not show in the list.
Related
On my windows application written with C#, I have written a form named BaseForm. On this form I have a Maximise button, white the following code:
private void btnMaximise_Click(object sender, EventArgs e)
{
WindowState = WindowState == FormWindowState.Normal ?
FormWindowState.Maximized : FormWindowState.Normal;
}
Now I have several forms derived from BaseForm. When I click the Maximise button on Say Form1, all other derived forms are maximized together, and when I click the button again, they all go back to their normal WindowState, TOGETHER.
I do not understand why this happens. I thought when I say this.WindowState I am pointing to the initiated object, but it is acting like a Static one. When I click the button on Form1 I expect only Form1 to get maximized and not all forms with common inheritance.
In the Designer.cs file, the event handler is linked like below:
this.btnMaximise.Click += new System.EventHandler(this.btnMaximise_Click);
By the way, all these forms are MDI children to an MDI Parent form. Maybe that has something to do with the problem.
New test result:
I tried opening 2 forms with MDI Parent set, and 2 other without it. The result is strange. Those which are independant (without MDI Parent) have no conflict, and those with MDI Parent all behave the same. Even when a form with MDI Parent is maximized and I open an entirely new form (not with CTRL+TAB, with instantiating a new form, setting the MDI Parent, and calling its .Show() method) the new form is opened with Maximized state.
I couldn't reproduce your issue; you'll need to post more code as to how you're creating, subclassing and wiring these events up. Here is a complete example code that DOESN'T demonstrate the issue:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
for (int i = 0; i < 5; i++)
new DerivedForm(this).Show();
}
}
public class DerivedForm : BaseForm {
public DerivedForm(Form parent)
{
this.MdiParent = parent;
}
}
public class BaseForm : Form
{
public BaseForm()
{
Button b = new Button();
b.Text = "Max";
b.Click += B_Click;
this.Controls.Add(b);
}
private void B_Click(object sender, EventArgs e)
{
this.WindowState = this.WindowState == FormWindowState.Normal ? FormWindowState.Maximized : FormWindowState.Normal;
}
}
}
Post something like this that does reproduce your issue and we will tell you why, or examine how this works and make your setup the same
I have a form with the property "MDI container" set to true that opens MDI children when pressing Labels on a MenuStrip, but I have two problems:
The first one is that once I open an MDI Child I can not open another one; I press different labels on the same MenuStrip that I pressed to open the current MDI child and nothing happens.
The second problem is that I can not open an MDI child form from another MDI child form from code.
Following this paragraph, I will show the relevant parts of my code and some things that I have tried (with no solutions)
//Event of the MenuStrip that opens an MDI child (homePage or sellProduct) from the MDI container
HomePage homePage = null;
SellProduct sellProduct = null;
private void HomeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (homePage == null)
{
homePage = new HomePage();
homePage.TopLevel = false;
homePage.MdiParent = this;
}
homePage.Show();
}
private void ToolStripSellPtoduct_click(object sender, EventArgs e)
{
if (sellProduct == null)
{
sellProduct = new SellProduct();
sellProduct.TopLevel = false;
sellProduct.MdiParent = this;
}
sellProduct.Show();
}
I have tried to copy this in a child form but it does not work. something that may be important is that when I load the MDI container I also load the first MDI child:
private void MainPage_Load(object sender, EventArgs e)
{
if (homePage == null)
{
homePage = new HomePage();
homePage.TopLevel = false;
homePage.MdiParent = this;
}
homePage.Show();
}
And that is all the code I consider necesary for the first problem (I can not open a MDI child form from another using my MenuStrip). If you need anything from my code I will provide it.
In the second problem (I can not open an MDI child form from another from code) I am trying to open the MDI child form "HomePage" from the other one "SellProduct" when pressing a button located in the last one:
public partial class SellProduct : Form
{
public SellProduct()
{
InitializeComponent();
}
private void Button_Sale_Click(object sender, EventArgs e)
{
HomePage homePage = new HomePage();
homePage.show();
this.close();
}
}
}
}
The code above closes MDI form SellProduct showing the mdiparent (but it does not execute again the mdi parent, and the MenuStrip still does not work, is weird) and opens a MDI parent (where the MenuStrip actually works). So no, it does not open another mdi child, it just do weird stuffs.
And that is all, thank you for your time, every help is welcome, and hope you have a great day (: .
I have finally solved this, this is my solution:
Problem 1) Once I open an MDI Child I can not open another one; I press different labels on the same MenuStrip that I pressed to open the current MDI child and nothing happens.
Solution: The MDI child forms were not showing because I had to hide the one opened (from the MainPage) before showing another one.
Problem 2) I can not open an MDI child form from another MDI child form from code.
Solution: It is the same problem as the first one If the actual form displayed is not hidden a new one can not be shown, because of that you have to hide the current one, and then open the new one:
//In this case I want to show the HomePage
this.Hide();
HomePage homePage = new HomePage();
homePage.Show();
I'm following this tuto for create a multipage app in Winform. My MdiParent have
MainForm IsMdiParent = true
HomeForm frmHomeForm;
private void HomeIcon_Click(object sender, EventArgs e)
{
if(frmHomeForm == null)
{
frmHomeForm = new HomeForm();
frmHomeForm.MdiParent = this;
frmHomeForm.Show();
}
else
{
frmHomeForm.Activate();
}
}
If someone can help, it's first time I used MDI.
I was trying to show MdiChild on my MdiParent who was covered by another control. I remove the control on my MdiParent and my MdiChild now appear correctly in empty area.
I have a button on Form1 that starts disabled by default. I have a ConfigureForm, where I have a menu strip, with an option to enable the button in Form1.
So my code is:
private void Portal2HammerButtonEnable_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Portal2HammerButton.Enabled = true;
}
But when I close ConfigureForm and look at the button, it's still disabled.
That's because you create a new Form1 and enable on that form the button. Instead, you have to pass the instance of the form you actually have open.
For design purposes you may want to use a controller class between these two forms. This will help you to simplify the complexity of passing data or actions between the two forms and will give you the power to escalate better the app..
When you open the ConfigureForm you have to do the following (in the simplest form however not recommended.)
...
{
ConfigureForm frmConfigure = new ConfigureForm(this);
}
Then inside the ConfigureForm:
public partial class ConfigureForm : Form
{
private From1 mainForm = null;
public ConfigureForm()
{
InitializeComponent();
}
public ConfigureForm(Form callingForm):this()
{
mainForm = callingForm as Form1;
}
private void Portal2HammerButtonEnable_Click(object sender, EventArgs e)
{
mainForm.Portal2HammerButton.Enabled = true;
}
}
You're creating a new form on the click of this button. What you want instead is a valid reference to the actual instance of Form1.
You have some options available:
If one of these forms is the "main" form of the application then you can ensure that it is created first and thus creates the other "sub" forms. you can override the constructors of any sub forms to include a reference to your "main" form.
You can keep references to all your important forms in a public static class such that all your forms can get to those references
You add your own public method to assign the "parent form" as a member or property of the child forms.
You can use reflection to find the instance of the "main" or "parent" form during creation or display of any child forms. If you do this, only do it once rather than upon every request. Try to cache that information.
You can read through the System.Windows.Forms namespace to find out if there's already a collection of objects through which you could iterate to find your main form.
I'd recommend option 2 or 5.
I would like to load multiple forms within a form using user controls and I've tried the following code but nothing seems to happen after clicking on button1. Anyone knows what's wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 control = new UserControl1();
control.Dock = DockStyle.Fill;
this.Controls.Add(control);
}
}
however the contents of UserControl1 seems to be overlapping and I can still see the content of Form1
The Z-order of the controls on the form matters. With Controls.Add(), the control ends up on the bottom of the order, existing controls overlap it. You fix it like this:
this.Controls.Add(control);
control.BringToFront();
Or use Controls.SetChildIndex() to insert it between controls.
Probably you need to change the value of Dock property. When it is DockStyle.Fill -- it will just take the whole area. Try to change it to other value, depending what layout you need.