Вопрос по c#, visual-studio – Изменить фон формы MDI


Как я могу изменить фоновый цвет формы MDI в C #?

Я изменил его, используя свойство background color, но цвет не изменился.

Что я должен сделать, чтобы выполнить эту задачу?

Ваш Ответ

2   ответа

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

Edit - Added comments:

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.

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.

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

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.

Error: User Rate Limit Exceeded sheetal
Error: User Rate Limit Exceeded sheetal
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededforeach (Control control in this.Controls) { if (control is MdiClient) { control.BackColor = Color.Red; break; } }

Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.Purple;

Похожие вопросы