WindowState of all derived forms are changing together - c#

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

Related

WinForms MDI : How to show MdiChild in Parent TableLayoutPanel's line?

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.

Getting button event back to objectListView form and exiting gracefully

Still learning c#. I am creating an ObjectListView, and this among other things is capturing button clicks.
public partial class MainForm : Form
{
private FormSpecParser parser;
private static ObjectListviewForm form;
public MainForm()
{
InitializeComponent();
createMainForm();
}
private void createMainForm()
{
form = new ObjectListviewForm();
form.Populate(parser.ParsedSpec);
form.Show(this);
}
}
public partial class ObjectListviewForm : Form
{
...
void b_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if (b == null) return;
var button = GetFormSpecButton(b.Name);
MessageBox.Show(button.ClickMessageBoxStub);
}
}
I have 2 problems: 1) how do I get the ObjectListView to tell MainForm that a button was clicked, please deal with it.
2) I want the ObjectListView Form to be the first one the user sees, and when the ObjectListView form closes, the program closes. Right now the ObjectListView Form does appear, but "behind" it is a blank form, and when ObjectListView Form is closed, control seems to pass back to this blank form instead of exiting.
1) You passed MainForm as Owner to Show method - form.Show(this). This means that in ObjectListviewForm you can call method from MainForm like this:
(this.Owner as MainForm).MethodInMainform();
2) In Program.cs file You should have line like this:
Application.Run(new MainForm());
change it to:
Application.Run(new ObjectListviewForm ());

Winforms: bring 2 obscured forms to front

I'm trying to solve this problem for a long time.
I have 2 forms, my objectives are:
When user minimize form2, form1 must minimize too.
When user maximize form2, form1 must maximize too.
When both forms are obscured by another window, and user clicks in the form2 icon in taskbar, form1 must also come to front.
The first 2 things I solved with the a_Resize method. But I can't do the third one. I tried with activate event but when I do that the form2 keeps blocked.
Here is my code:
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Show();
form2.Resize += new EventHandler(a_Resize);
}
void a_Resize(object sender, EventArgs e)
{
if (((Form)sender).WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Minimized;
}
else
{
this.WindowState = FormWindowState.Normal;
}
}
}
If I add a handler to the form2 activate event:
form2.Activated += new EventHandler(form2_Activated);
And call for instance the Focus method (I tried other methods too), the form2 keeps blocked behind form1.
void form2_Activated(object sender, EventArgs e)
{
this.Focus();
}
Someone have any ideas how I can do that?
When you create form2, just pass this as a parameter to Show() to signify that form1 is the owner. With an owner link, the forms will always be raised together (at least in my experience -- I don't have a specification to back me up on this).
public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Show(this); //pass 'this' as argument to Show() to link them
form2.Resize += new EventHandler(a_Resize);
}

C# How do I leave the maximized window as it is?

I am using visual studio 2010 to do my C# GUI.
The current problem that I am facing is that after maximizing a window, it stays there but when I go to other forms, the window will go back to its original size.
How do I leave the maximized window all the way for all the forms, once I click the maximize button?
Heres an example:
User maximizes Form A
Form A maximized
User goes to Form B
Form B goes back to original size instead of a maximized window
What I want is when user maximizes a form, it stays that way till the program is closed or resized.
Assuming you're using WinForms, you can have either implement a shared FormWindowState manager or use a Multiple Document Interface (MDI) container.
Shared FormWindowState
You can register each of your forms with a class responsible for propagating changes in forms' FormWindowState.
public class FormWindowStateManager {
List<Form> _Forms;
...
public void Register(Form form) {
if(!_Forms.Contains(form)) {
_Forms.Add(form);
form.Resize += new EventHandler(Form_Resize);
}
}
public void Unregister(Form form) {
if(_Forms.Contains(form)) {
_Forms.Remove(form);
form.Resize -= new EventHandler(Form_Resize);
}
}
private void Form_Resize(object sender, EventArgs e) {
Form form = sender as Form;
if(form != null) {
if(form.FormWindowState == FormWindowState.Maximized || form.FormWindowState == FormWindowState.Normal) {
PropagateWindowState(form.FormWindowState);
}
}
}
private void PropagateWindowState(FormWindowState state) {
foreach(Form form in _Forms) {
if(form.FormWindowState != state) {
form.FormWindowState = state;
}
}
}
}
MDI Container
MdiParentForm.cs
IsMdiContainer = true;
MdiChildForm.cs
MdiParent = myParentForm; // instance of MdiParentForm
You can iterate through a form's MDI children using the form's MdiChildren property such that when on MDI child window changes its FormWindowState, the MDI parent form can apply the change to each of its children, similar to the shared FormWindowState approach.
These ideas are just off the top of my head but maybe they'll get you in the right direction.

MDIWindowListITem Not Working

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.

Categories