I'm working on a legacy WinForms MDI application and have some trouble making the child forms behave as I want.
My objective is to have the child form always maximized (docked).
The problem is, that even if I set MaximizeBox to false the maximize/resize button appears in the MDIs toolstrip and let the user resize (undock) the child form.
The only way to avoid this is to set ControlBox to false but then the close button disappears to (thats not what I want).
I've already tried to use a fixed FormBorderStyle and to maximize the child form when the resize event is fired but none of my approaches worked.
Is there any super secret property I have missed or is it just impossible?
Best Regards & Thanks in advance
Update
I wrote a sleazy method (thanks to #rfresia) for handling my child form, it may help others who run into the same issue:
//All child forms derive from ChildForm
//Parent MDI Form implementation
//...
private void ShowForm(ChildForm form)
{
//Check if an instance of the form already exists
if (Forms.Any(x => x.GetType() == form.GetType()))
{
var f = Forms.First(x => x.GetType() == form.GetType());
f.Focus();
f.WindowState = FormWindowState.Maximized;
}
else
{
//Set the necessary properties (any other properties are set to default values)
form.MdiParent = this;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.WindowState = FormWindowState.Maximized;
Forms.Add(form);
form.Forms = Forms;
form.Show();
form.Focus();
//Lets make it nasty (some forms aren't rendered properly otherwise)
form.WindowState = FormWindowState.Normal;
form.WindowState = FormWindowState.Maximized;
}
}
//...
//ChildForm implementation
//...
public List<Form> Forms { get; set; }
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
Forms.RemoveAll(x => x.GetType() == GetType());
}
protected override void OnResize(EventArgs e)
{
WindowState = FormWindowState.Maximized;
}
The problem wasn't easy to solve, but I accidentally found the answer and it's quite simple; set the windowstate of the child form to Normal by default. Then make sure that you reset the windowstate of the child window AFTER you call the Show() method.
Example:
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "Window " + childFormNumber++;
childForm.Show();
childForm.WindowState = FormWindowState.Maximized;
}
You can override the OnResize of each child form you want to make sure does not minimize. Or create a BaseForm and inherit all children forms from it.
protected override void OnResize(EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
Also, you can use X,y coordinates, but OnResize should be enough. Put this in the child form constructor:
this.WindowState = FormWindowState.Maximized;
Point NewLoc = Screen.FromControl(this).WorkingArea.Location;
//Modifiy the location so any toolbars & taskbar can be easily accessed.
NewLoc.X += 1;
NewLoc.Y += 1;
this.Location = NewLoc;
Size NewSize = Screen.FromControl(this).WorkingArea.Size;
//Modifiy the size so any toolbars & taskbar can be easily accessed.
NewSize.Height -= 1;
NewSize.Width -= 1;
this.Size = NewSize;
this.MinimumSize = this.Size;
this.MaximumSize = this.MinimumSize;
I got the code for the X,Y from here:
http://bytes.com/topic/c-sharp/answers/278649-how-do-i-prevent-form-resizing
form1 obj = new form1 ();
obj.MdiParent = MDIGracular.ActiveForm;
obj.StartPosition = FormStartPosition.CenterParent;
obj.WindowState = FormWindowState.Minimized;
obj.Dock = DockStyle.Fill;
obj.Show();
obj.WindowState = FormWindowState.Maximized;
This is how I overcame the same promblem, cannot remember where I found the code.
private const int WM_SYSCOMMAND = 0x112;
private const int SC_MINIMIZE = 0xF020;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_CLOSE = 0xF060;
private const int SC_RESTORE = 0xF120;
protected override void WndProc(ref Message msg)
{
if ((msg.Msg == WM_SYSCOMMAND) &&
(((int)msg.WParam == SC_MINIMIZE) || ((int)msg.WParam == SC_MAXIMIZE) ||
((int)msg.WParam == SC_CLOSE)) || ((int)msg.WParam == SC_RESTORE))
{
//do nothing
} // end if
else
{
base.WndProc(ref msg);
} // end else
}
In my app I found if I put just these two lines in the form loaded event it worked. Thanks sarvjeet for the basic idea. +1 for you
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;
Related
I am trying to create an interface that has MDI child forms that open in full screen mode within the parent and the controlBox, minimizeBox, and maximizeBox all show up in full screen even though I have them all set to false. Anyone know how to fix this?
Here is the code I am using to switch between screens (children forms) they should open in full screen mode because the default I set them with, but I need to get rid of the minimize, maximize, and control buttons.
public partial class Parent : Form
{
Form activeForm = new Form2();
public Parent()
{
InitializeComponent();
}
private void Parent_Load(object sender, EventArgs e)
{
activeForm.MdiParent = this;
activeForm.Show();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if(toolStripTextBox1.Text == "1")
{
activeForm.Close();
activeForm = new Form2();
activeForm.MdiParent = this;
activeForm.Show();
}
if (toolStripTextBox1.Text == "2")
{
activeForm.Close();
activeForm = new Form3();
activeForm.MdiParent = this;
activeForm.Show();
}
}
}
set the controlbox to false, for any form which you prefer not to have minimizeBox, MaximizeBox and Close.
activeForm.ControlBox = false;
my request is suppose i have two sdi form. one sdi main form has button and when user click on that button then a overlay semi transparent window will appear and cover the main sdi form and as well as another sdi will come on top of the over lay window.
here is my overlay window code
namespace CSRAssistant
{
public partial class MaskedDialog : Form
{
static MaskedDialog mask;
static Form frmContainer;
private Form dialog;
private UserControl ucDialog;
private MaskedDialog(Form parent, Form dialog)
{
this.dialog = dialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private MaskedDialog(Form parent, UserControl ucDialog)
{
this.ucDialog = ucDialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private void AdjustPosition(object sender, EventArgs e)
{
Form parent = sender as Form;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
this.ClientSize = parent.ClientSize;
}
//
public static DialogResult ShowDialog(Form parent, Form dialog)
{
mask = new MaskedDialog(parent, dialog);
dialog.StartPosition = FormStartPosition.CenterParent;
mask.MdiParent = parent.MdiParent;
mask.Show();
DialogResult result = dialog.ShowDialog(mask);
mask.Close();
return result;
}
public static DialogResult ShowDialog(Form parent, UserControl dialog)
{
mask = new MaskedDialog(parent, dialog);
frmContainer = new Form();
frmContainer.ShowInTaskbar = false;
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmContainer.StartPosition = FormStartPosition.CenterParent;
frmContainer.Height = dialog.Height;
frmContainer.Width = dialog.Width;
frmContainer.Controls.Add(dialog);
mask.MdiParent = parent.MdiParent;
mask.Show();
DialogResult result = frmContainer.ShowDialog(mask);
frmContainer.Close();
mask.Close();
return result;
}
public static void CloseDialog()
{
if (frmContainer != null)
{
frmContainer.Close();
}
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MaskedDialog
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "MaskedDialog";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MaskedDialog_FormClosing);
this.Load += new System.EventHandler(this.MaskedDialog_Load);
this.ResumeLayout(false);
}
private void MaskedDialog_Load(object sender, EventArgs e)
{
}
private void MaskedDialog_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}
this way i am calling my overlay when user click on main sdi button.
Form2 f2 = new Form2();
f2.Show();
MaskedDialog.ShowDialog(this, f2);
f2.Dispose();
f2 = null;
when i am running the code and MaskedDialog.ShowDialog(this, f2); is calling then i am getting a error....the error message is Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.
so i am not being able to understand where i am making the mistake. anyone can help me to fix the problem. thanks
You're doing this wrong - call mask.ShowDialog(), not mask.Show() and dialog.ShowDialog(). The error message is pretty clear - you can't ShowDialog a form that's already visible - and that's exactly what you're doing in dialog.ShowDialog(mask);.
Or, in a way that probably more closely shows what you want to do, when calling MaskedDialog.ShowDialog, do this:
Form2 f2 = new Form2();
MaskedDialog.ShowDialog(this, f2);
In other words - don't show the form you're going to ShowDialog. It's going to be made visible by the ShowDialog call.
I'm trying to create a custom form in WPF. I set the windowStyle to be None
I added 3 buttons :
Close
Max/Min
Hide
What I'm trying to do is when the user clicks on the second button is ,
if the form in normal state , maximize the form to be as the screen size, else if
the form is on maximize state , set the form to the initial size..
This is what I tried, but nothing happens when I first click on the second button
private bool maximized = false;
private void button2_Click(object sender, RoutedEventArgs e)
{
if (!maximized)
{
this.MaxHeight = SystemParameters.PrimaryScreenHeight;
this.MaxWidth = SystemParameters.PrimaryScreenWidth;
this.WindowState = System.Windows.WindowState.Maximized;
maximized = true;
}
else
{
this.WindowState = System.Windows.WindowState.Normal;
maximized = false;
}
}
Am I need to add Invalidate or something like that?
Ok, I Solved it..
When I created the xaml file.. I added a rectangle so the form body will be the rectangle..
I needed to change the rectangle width and height instead..
private void button2_Click(object sender, RoutedEventArgs e)
{
if (!maximized)
{
this.FormBody.Width = SystemParameters.WorkArea.Width; //rectangle's width
this.FormBody.Height = SystemParameters.WorkArea.Height;// rectangle's height
this.WindowState = System.Windows.WindowState.Maximized;
maximized = true;
}
else
{
this.WindowState = System.Windows.WindowState.Normal;
maximized = false;
}
}
I also change the SystemParameters.PrimaryScreenWidth to be SystemParameters.WorkArea.Width
and also with the Height so in this way the form wont exceed the taskbar
I want to minimize and maximize manually in C#.net.
I changed form's BorderStyle into none.
So there are no maximize,minimize and close button from bar.
I want to manually create with button like those functions.
I want to do three functions in button click events.
You have to set the forms WindowState property something like this:
In Windows Forms:
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
In WPF:
private void button1_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
Form.WindowState Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate%28v=VS.90%29.aspx
public FormWindowState WindowState { get; set; }
For example -
var form = new Form();
form.WindowState = FormWindowState.Maximized;
form.WindowState = FormWindowState.Minimized;
form.WindowState = FormWindowState.Normal;
However, if you are in the code behind on the main form (or any form) just do this -
WindowState = FormWindowState.Maximized;
If you're using WindowsForms you have to change the WindowState property :)
private void button4_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
}
else
{
this.WindowState = FormWindowState.Normal;
}
Actually Its my first project. While I want to convert my VB.Net2008 to C#2010 I have few clarification pls.
In Form2 Properties I set - IsMDIContainer = True. Then the below code to open my MdiChild and now what is my problem when I click the close button, it's also closing the MDIParent. But I need to close only mdichild... for that I tried as like Vb.Net2008 style by the following codes placed in MDIParent Form2, Its not working. Any right directions ...
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form3 NwMdiChild2 = new Form3;
NwMdiChild2.MdiParent = this;
NwMdiChild2.Dock = System.Windows.Forms.DockStyle.Fill;
NwMdiChild2.Show();
}
private void Form2_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
Form[] MdiChildForms = this.MdiChildren;
int kkk1 = MdiChildForms.Length;
int x = 0;
for (x = 0; x <= MdiChildForms.Length - 1; x += 1)
{
if (MdiChildForms[x].Name == "Form1")
{
kkk1 = kkk1 - 1;
}
MdiChildForms[x].Close();
}
if (kkk1 > 0)
{
// For Not Closing
e.Cancel = true;
}
else
{
// For Closing
e.Cancel = false;
Application.Exit();
}
}
Any Right Directions for Me?
I'm not sure if I understand well what you want to achieve: do you want, when click Parent Form close button, insteed of closing parent form, to close all the child's form? Form2 is your main form (parent MDI container), Form3 is the MDI children, isn't it?
Please try following code and tell if it's what you are asking for:
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form3 NwMdiChild2 = new Form3(); //don't forget ()
NwMdiChild2.MdiParent = this;
NwMdiChild2.Dock = System.Windows.Forms.DockStyle.Fill;
NwMdiChild2.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//if no MDI child - this is going to be skipped and norlmal Form2 close takes place
if (this.MdiChildren.Length > 0) //close childrens only when there are some
{
foreach (Form childForm in this.MdiChildren)
childForm.Close();
e.Cancel = true; //cancel Form2 closing
}
}