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;
Related
I have custom MDI Parent form and custom child form and I want to set title on mdi form it's own text + mdi child form's text when mdi child form is maximize. So how it possible?
Normal state of mdi child form it is work correctly.
In this image you can see form state is maximise but text of MDI child not shown with text of MDI Parent form.
When you maximize an MDI child, the text of MDI container will be shown as:
ParentText - [ChildText]
So based on your edit I suppose you have a CustomText property like below:
private string CustomText
{
get { return toolStripLabel1.Text; }
set { toolStripLabel1.Text = value; }
}
Which shows a custom title-bar for form. Then if you have it acts like standard title-bar of form you can handle Activated and SizeChanged event of MDI child forms and set the CustomText using BeginInvoke this way:
private void button1_Click(object sender, EventArgs e)
{
var f = new Form2() { Text = "Form2" };
f.MdiParent = this;
f.Activated += new EventHandler(f_Activated);
f.SizeChanged += new EventHandler(f_SizeChanged);
f.Show();
}
void f_SizeChanged(object sender, EventArgs e)
{
this.BeginInvoke(new Action(() => { CustomText = this.Text; }));
}
void f_Activated(object sender, EventArgs e)
{
this.BeginInvoke(new Action(() => { CustomText = this.Text; }));
}
Note
MdiChildActivate is useful to detect when an MDI child opens or closes.
ActiveMdiChild property shows you the active MDI child.
Try Form's Size_Changed event, (Form1 is MDI parent, TestForm (frm) is MDI child)
TestForm frm;
private void Form1_Load(object sender, EventArgs e)
{
frm = new TestForm();
frm.MdiParent = this;
frm.SizeChanged += Frm_SizeChanged;
frm.Show();
}
private void Frm_SizeChanged(object sender, EventArgs e)
{
if (frm.WindowState == FormWindowState.Maximized) { // checks Form's Window state and return true if it is maximized (mdi child's event btw)
this.Text = this.Text + " " + frm.Text; // do what ever do you want in here
}
}
Hope helps,
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;
I'm trying to create one window and on that window I would have a toolbar with different buttons.
When I go to click on one of the buttons It would display something like information about a person or when I click on another button It would display some other information about employees.
How can I do this. Can I make add pages and then insert that page onto a grid or panel when that button calls for it?
Or Should I just make multiple panels and create them all on one window(but if I do this how would it be easy for me to edit each of those panels when they are stacked on one another all in one window). I hope I'm being clear about this, Idk how else to ask this question. Any help is appreciated.
Also how do I dock something so that it resizes itself when maximize or minimize?
One way is to create another form and open it from a button event:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
If you want everything in one window, you can create a user control and add it to the first window:
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);
}
}
Another option is using child forms:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}
}
Create a panel for each button you have. Then:
panelx.Dock = DockStyle.Fill; //this will fill the window.
And put all you want to show for that button on that panel.
When you want to show, say panel2 instead of panel1:
panel1.Hide();
panel2.Show();
As mentioned earlier, this can be achieved using a tab control.
public ParentForm()
{
TabControl tabcontrol1 = new TabControl();
tabcontrol1.Dock = DockStyle.Top;
TabPage tab1 = new TabPage("Form1 Name");
Form1 frm1 = new Form1();
frm1.TopLevel = false;
frm1.Parent = tab1;
frm1.Visible = true;
tabcontrol1.TabPages.Add(tab1);
Form2 frm2 = new Form2();
TabPage tab2 = new TabPage("Form2 Name");
frm2.TopLevel = false;
frm2.Parent = discotab;
frm2.Visible = true;
tabcontrol1.TabPages.Add(discotab);
}
Using "stacked" custom user controls
public ParentForm()
{
InitializeComponent();
MyUserControl1 control1 = new MyUserControl1();
control1.Dock = DockStyle.Bottom;
control1.BringToFront();
}
private void button1_Click(object sender, EventArgs e)
{
MyUserControl2 control2 = new MyUserControl2();
control2.Dock = DockStyle.Bottom;
control2.BringToFront();
}
I implemented a "paged options" dialog box in Windows Forms a while ago. My blog post about it is here: http://www.differentpla.net/content/2004/10/implementing-a-paged-options-dialog (the images are missing, though).
i have created an application in which a menustrip is present in which 2 buttons are there, one for ADD, and another for UPDATE & both controls are in a single form, means a button of add & update is there in a single form, whenever i press add button in menustrip update button will be disabled, and when i press update on menustrip the add button will disable. how to do this? i m doing this by show method but that form is opening multiple times using show().
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button1.Enabled = true;
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
you simply have to use a single form in this case. try using the singleton approach -
http://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/
try using .ShowDialog() instead .Show() and no other form will be able to be clicked on until that one closes.
To do that you'll need to have an instance of that Form outside of those methods that you dismply show if the Form has already been created, or create and show it if it has not (this is the singleton pattern). Here's an example:
Form1 f2 = null;
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new Form1();
f2.MdiParent = this;
f2.button1.Enabled = true;
}
f2.Show();
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2.MdiParent = this;
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
f2.Show();
}
One question on your disabling of the menu items though, how do you plan on re-enabling them after they have been disabled?
just try to check that form is already opened or not by using its Text Property.. if it is opened just focus on that form other wise show that form as normally
private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "Form1")
{
IsOpen = true;
f.Focus();
break;
}
}
if (IsOpen == false)
{
Form f1 = new Form1();
f1.Show();
}
}
Try This Guys Its Simple
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
}
}