Click here to Skip to main content
15,900,973 members
Articles / Programming Languages / C#
Tip/Trick

Change Back Color of MDI Parent form on its load.

Rate me:
Please Sign up or sign in to vote.
3.50/5 (5 votes)
18 Feb 2010CPOL1 min read 43.6K   2   4
How to change the background color for an MDI parent form in Visual C#This article demonstrates how to programmatically change the background color for a multiple-document interface (MDI) parent form by using Visual C#.When you use a Windows Form as an MDI parent form, the Application...
How to change the background color for an MDI parent form in Visual C#
This article demonstrates how to programmatically change the background color for a multiple-document interface (MDI) parent form by using Visual C#.
When you use a Windows Form as an MDI parent form, the Application Background color setting in Windows Control Panel, not the form's BackgroundColor property, determines the background color of the form. The following steps demonstrate how to programmatically change the MDI parent form's background color to another color.

Create a Sample Windows Application by Using Visual C#


Create a new Visual C# Windows application. Form1 is created by default. Click the form, and then, on the View menu, select Properties Window to view the properties for the form. Set the BackColor property to the color that you want (such as Blue).
Set the IsMDIContainer property to True. Note that the background color of the form changes to the color that the Application Background color is set to in Control Panel. Set the WindowState property to Maximized. Double-click the form to view its code window. Paste the following code into the form's Load event handler:
C#
MdiClient ctlMDI;
// Loop through all of the form's controls looking
// for the control of type MdiClient.
foreach (Control ctl in this.Controls)
{
   try
   {
      // Attempt to cast the control to type MdiClient.
      ctlMDI = (MdiClient) ctl;
      // Set the BackColor of the MdiClient control.
      ctlMDI.BackColor = this.BackColor;
   }
   catch (InvalidCastException exc)
   {
      // Catch and ignore the error if casting failed.
   }		         
}
		
// Display a child form to show this is still an MDI application.
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();

On the Project menu, click Add Windows Form. Accept the default name Form2.cs, and then click Open.
Press F5 to run the application. Note that the MDI parent form loads and has a blue background.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionA more modern version: Pin
Tony Tullemans12-Mar-24 20:03
Tony Tullemans12-Mar-24 20:03 
foreach (Control ctl in this.Controls)
{
    if (ctl is MdiClient client)
    {
        client.BackColor = this.BackColor;
    }
}

GeneralReason for my vote of 5 helped a lot Pin
nathan7428-Aug-11 20:52
nathan7428-Aug-11 20:52 
GeneralReason for my vote of 1 Can be referred Pin
Santoshshrest7-Apr-11 21:14
Santoshshrest7-Apr-11 21:14 
GeneralReason for my vote of 3 Can be referred... Pin
Santoshshrest7-Apr-11 20:44
Santoshshrest7-Apr-11 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.