How to change the background color for an MDI parent form in Visual Basic .NET or in Visual Basic 2005

Article translations Article translations
Article ID: 319417 - View products that this article applies to.
This article was previously published under Q319417
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article demonstrates how to programmatically change the background color for a multiple-document interface (MDI) parent form by using Visual Basic .NET or Visual Basic 2005.

When you use a Windows Form as an MDI parent form, the Application Background color setting in Windows Control Panel, not the form's BackgroundColor property, determines the background color of the form. The following steps demonstrate how to programmatically change the MDI parent form's background color to another color.

Create a Sample Windows Application by Using Visual Basic .NET or Visual Basic 2005

  1. Create a new Visual Basic .NET or Visual Basic 2005 Windows application. Form1 is created by default.

    Note The code should be changed in Visual Basic 2005. If you create a new form named Form1 in Visual Basic 2005, you have a Form1.vb file for your code and a Form1.Designer.vb file that contains the section that is automatically generated. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-emitted code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Web site:
    http://msdn.microsoft.com/en-us/library/ms379584(vs.80).aspx
    For more information about partial classes and the Windows Forms Designer, visit the following Microsoft Web site:
    http://msdn.microsoft.com/en-us/library/ms171843.aspx
  2. Click the form, and then, on the View menu, select Properties Window to view the properties for the form.
  3. Set the BackColor property to the color that you want (such as LightBlue).
  4. Set the IsMDIContainer property to True. Note that the background color of the form changes to the color that the Application Background color is set to in Control Panel.
  5. Set the WindowState property to Maximized.
  6. Double-click the form to view its code window.
  7. Paste the following code into the form's Load event handler:
    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
    
    ' Display a child form to show this is still an MDI application.
    Dim frm As New Form2()
    frm.MdiParent = Me
    frm.Show()
    					
  8. On the Project menu, click Add Windows Form.
  9. Accept the default name Form2.vb, and then click Open.
  10. Press F5 to run the application.
Note that the MDI parent form loads and has a light blue background.

REFERENCES

MdiClient Class
Control.BackColor Property

Properties

Article ID: 319417 - Last Review: July 5, 2012 - Revision: 4.0
APPLIES TO
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbvs2005swept kbvs2005applies kbhowtomaster KB319417

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com