DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 319465 - Last Review: December 11, 2006 - Revision: 2.4

This article was previously published under Q319465

On This Page

SUMMARY

This step-by-step 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# .NET

  1. Create a new Visual C# Windows application. Form1 is created by default.
  2. Click the form, and then, on the View menu, select Properties Window to view the properties for the form.
  3. Set the BackColor property to the color that you want (such as LightBlue).
  4. 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.
  5. Set the WindowState property to Maximized.
  6. Double-click the form to view its code window.
  7. Paste the following code into the form's Load event handler:
    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();
    					
  8. On the Project menu, click Add Windows Form.
  9. Accept the default name Form2.cs, and then click Open.
  10. Press F5 to run the application.
Note that the MDI parent form loads and has a light blue background.

REFERENCES

MdiClient Class (http://msdn.microsoft.com/en-us/library/system.windows.forms.mdiclient(VS.71).aspx)
Control.BackColor Property (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.backcolor(VS.71).aspx)


APPLIES TO
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
Keywords: 
kbforms kbhowtomaster kbmdi KB319465
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support