Question

How can I change the BACKGROUND color of the MDI FORM in C#?

I changed it using the background color property but the color is not changed.

What should I do to perform this task?

Was it helpful?

Solution

The actual BackGround colour of the MDI control is based on the colour in the Windows current Theme. You have to physically set the MdiClient control's background inside the WinForm.

    // #1
    foreach (Control control in this.Controls)
    {
        // #2
        MdiClient client = control as MdiClient;
        if (!(client == null))
        {
            // #3
            client.BackColor = GetYourColour();
            // 4#
            break;
        }
    }

Edit - Added comments:

  1. We need to loop through the controls in the MdiParent form to find the MdiClient control that gets added when you set the Form to be an MdiParent. Foreach is just a simple iteration of a type through a collection.

  2. We need to find the MdiClient control within the form, so to do this we cast the current control within the loop using the 'as' keyword. Using the 'as' keyword means that if the cast is invalid then the variable being set will be null. Therefore we check to see if 'client' is null. If it is, the current control in the loop is not the MdiClient control. As soon as the variable 'client' is not null, then the control we've got hold of is the MdiClient and we can set its background colour.

  3. Set the backcolour to anything you want. Just replace "GetYourColour()" with whatever colour you want, i.e. Color.White, Color.Blue, Colour.FromArgb(etc)...

  4. As there is only ever 1 MdiClient, there's no point continuing the loop as it's just a waste of processing time. Therefore we call 'break' to exit the loop.

Let me know if you want anything else explaining.

OTHER TIPS

Write this in your load method of your MDI form.

Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.Purple;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top