i need to change the backcolor or background image of a mdi parent in my application. i tried changing the backcolor or specifying a background image, it won't work. i also tried looping the controls in the form to get the mdiclient and change its backcolor, also zero same result.
Asked
Active
Viewed 8,634 times
5 Answers
1
Maybe this will help? http://support.microsoft.com/kb/319465

Art W
- 2,040
- 1
- 20
- 23
-
Are you setting up your MDI parent and children before or after the form's Load event occurs. When you debug, can you verify that a control of type MdiClient exists at all. More details about the behavior would be helpful. – jpierson Apr 27 '10 at 11:45
1
If you are doing a simple colour then try the below code, if you are trying to set an image then you can use BackgroundImage with BackgroundImageLayout
MdiClient ctlMDI;
foreach (Control ctl in this.Controls)
{
try
{
ctlMDI = (MdiClient)ctl;
// Set the BackColor of the MdiClient control.
ctlMDI.BackColor = Color.DarkRed;
}
catch (InvalidCastException exc)
{
// Catch and ignore the error if casting failed.
}
}

Neo
- 2,305
- 4
- 36
- 70
1
Private ClientControl As MdiClient
Public Sub New()
InitializeComponent()
ClientControl = Nothing
For Each Ctl As Control In Me.Controls
ClientControl = TryCast(Ctl, MdiClient)
If ClientControl IsNot Nothing Then Exit For
Next
End Sub
'iN FORM LOAD
ClientControl.BackColor = Color.Cyan

Yugal Jindle
- 44,057
- 43
- 129
- 197

Nikhil
- 11
- 1
0
Try this, it works.
foreach (Control control in this.Controls)
{
// #2
MdiClient client = control as MdiClient;
if (!(client == null))
{
// #3
client.BackColor = GetYourColour();
// 4#
break;
}
}

Bo Persson
- 90,663
- 31
- 146
- 203

Vaibhav
- 15
- 6
0
Try This
Public Sub MDIBGColor()
Dim ctl As Control
Dim ctlMDI As MdiClient
' Loop through all of the form's controls looking
' for the control of type MdiClient.
For Each ctl In Me.Controls
Try
' Attempt to cast the control to type MdiClient.
ctlMDI = CType(ctl, MdiClient)
' Set the BackColor of the MdiClient control.
ctlMDI.BackColor = Me.BackColor
Catch exc As InvalidCastException
' Catch and ignore the error if casting failed.
End Try
Next
End Sub
and call the sub on the form load event

Adam
- 147
- 1
- 13