How to change the Back color of MDI Form – C#


As a C# programmer, you may have used Back Color property for changing the color of the background for control as well as Windows form. You can also found that this not works with MDI Parent form.

MDI is the container for child forms or control, actually, it’s inherited from the panel [leave it no…lol].

Ok, all you want to repaint all the controls in it, even though there are no controls were placed, think about the inherited panel. All we have to do is try to cast every control as MDIClient and loop through it and change the color as follows

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 = Color.Gold;
 }
 catch (InvalidCastException exc)
 {
 // Catch and ignore the error if casting failed.
 }
 }

Place the code on Load Event of MDI Parent Form

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.