Click here to Skip to main content
16,016,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i m trying to give backcolor to a mdi parent form,but i can not see the effect.......plz help
Posted
Comments
Mehdi Gholam 27-Nov-11 8:09am    
What is your code?

An alternative:
C#
private void Form1_Load(object sender, EventArgs e)
{
    // store the result in a class-scoped variable
    // if you need to change the color again, or
    // whatever
    //
    // note that here we assume the Form IsMdiContainer property is 'true;
    // no checking that is true
    //
    this.Controls.OfType<MdiClient>().First().BackColor = Color.Red;
    //
    //
}
 
Share this answer
 
v4
Comments
LanFanNinja 29-Nov-11 11:08am    
+5 I like this the best!
RaisKazi 30-Nov-11 3:14am    
My 5.
Try something like this
C#
foreach (Control c in this.Controls)
{
    if (c is MdiClient)
    {
        c.BackColor = Color.Blue;
        break;
    }
}


EDIT: Added "break;"
 
Share this answer
 
v2
Comments
Himu from Orissa 27-Nov-11 8:54am    
working...... thanx
johannesnestler 27-Nov-11 15:22pm    
break is important!
LanFanNinja 27-Nov-11 16:29pm    
Indeed! :)
RaisKazi 30-Nov-11 3:14am    
Voting 5 to counter that one Down-Vote.
LanFanNinja 30-Nov-11 15:31pm    
Thank you!
This is not clear, or obvious, but...
C#
foreach (Control control in Controls)
    {
    MdiClient client = control as MdiClient;
    if (client != null)
        {
        client.BackColor = Color.Red;
        }
    }
Trust me - try it!
 
Share this answer
 
Comments
Himu from Orissa 27-Nov-11 8:53am    
thanx its working ,but why i need to do so?
LanFanNinja 27-Nov-11 9:07am    
I honestly don't know if I have ever seen a clear explanation as to why this has to be done so I can't really say for sure but I think it is because an actual MdiClient control is added to your form so setting the BackColor of the form has no effect because it is covered up by the control. So we must set the color of the control instead. Also you can "break;" out of the loop once the BackColor has been set and save some processing time.
OriginalGriff 27-Nov-11 9:10am    
Mostly because MS forgot that you might want to do this! :laugh:
When you create a MDI parent form, it gets a MDIClient which is basically the background. Since this is not given a name, you can't access it directly, and since it is not added at the top of the controls list, you have to go looking for it!
LanFanNinja 27-Nov-11 9:14am    
Yes I think MS has alzheimer's! :)
LanFanNinja 27-Nov-11 9:09am    
+5 OG for beating me to it! :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900