Results 1 to 4 of 4

Thread: Changing the Background of an MDI Parent Form

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Changing the Background of an MDI Parent Form

    C# version here.

    I've posted a solution to this question many times already and it's come up again recently, so I figured a little CodeBank submission might be helpful. First, some background information:

    When you set the IsMdiContainer property of a form to True, the IDE adds an MdiClient control to your form. The grey background you see is not the form but the MdiClient control. If you set the BackColor or BackgroundImage properties of the form it will have no effect because you can't see the form's background.

    Now, it is actually this MdiClient control, not the form itself, that hosts the MDI child forms. If you add any controls to the parent form then they will either be behind the MdiClient and, therefore, not visible or else on top of the MdiClient and, therefore, on top of the MDI child forms too.

    The developer is not intended to mess with this MdiClient control so no member variable is provided with which to access it. It's not inaccessible though, so if you want to change the visible background of the parent form you can do this:
    VB.NET Code:
    1. Private Sub Form1_Load(ByVal sender As Object, _
    2.                        ByVal e As EventArgs) Handles MyBase.Load
    3.     For Each ctl As Control In Me.Controls
    4.         If TypeOf ctl Is MdiClient Then
    5.             'Set properties of ctl here, e.g.
    6.             ctl.BackgroundImage = My.Resources.MdiBackgroundImage
    7.             Exit For
    8.         End If
    9.     Next ctl
    10. End Sub
    Last edited by jmcilhinney; Oct 30th, 2008 at 04:57 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Hyperactive Member kuldevbhasin's Avatar
    Join Date
    Mar 2008
    Location
    Mumbai, India
    Posts
    493

    Re: Changing the Background of an MDI Parent Form

    Quote Originally Posted by jmcilhinney
    I've posted a solution to this question many times already and it's come up again recently, so I figured a little CodeBank submission might be helpful. First, some background information:

    When you set the IsMdiContainer property of a form to True, the IDE adds an MdiClient control to your form. The grey background you see is not the form but the MdiClient control. If you set the BackColor or BackgroundImage properties of the form it will have no effect because you can't see the form's background.

    Now, it is actually this MdiClient control, not the form itself, that hosts the MDI child forms. If you add any controls to the parent form then they will either be behind the MdiClient and, therefore, not visible or else on top of the MdiClient and, therefore, on top of the MDI child forms too.

    The developer is not intended to mess with this MdiClient control so no member variable is provided with which to access it. It's not inaccessible though, so if you want to change the visible background of the parent form you can do this:
    VB.NET Code:
    1. Private Sub Form1_Load(ByVal sender As Object, _
    2.                        ByVal e As EventArgs) Handles MyBase.Load
    3.     For Each ctl As Control In Me.Controls
    4.         If TypeOf ctl Is MdiClient Then
    5.             'Set properties of ctl here, e.g.
    6.             ctl.BackgroundImage = My.Resources.MdiBackgroundImage
    7.             Exit For
    8.         End If
    9.     Next ctl
    10. End Sub
    i tried the code and am using vb.net 2003 it gives a blue line on
    Code:
    my
    says :
    Code:
    Name "My" is not declared
    if i use Me instead of My it says "Resources" is not a member of the form
    what can i use instead of it?
    thankx...

  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Changing the Background of an MDI Parent Form

    Quote Originally Posted by kuldevbhasin
    i tried the code and am using vb.net 2003 it gives a blue line on
    Code:
    my
    says :
    Code:
    Name "My" is not declared
    if i use Me instead of My it says "Resources" is not a member of the form
    what can i use instead of it?
    thankx...
    That part of the code is not really relevant to the topic of this thread. That is simply getting an Image object form the application resources. You can get your Image object in whatever way is appropriate. You can get it from a file or, if you have embedded it as a resource, you can get it in the standard way for retrieving resource images in .NET 1.x. That is beyond the scope of this thread so I'm not going to explain it here. I suggest that you search MSDN as there's example code available. If you still aren't sure then post on the VB.NET forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Changing the Background of an MDI Parent Form

    Here's some more succinct code that uses extension methods and, therefore, requires .NET 3.5 or later:
    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As Object, _
    2.                        ByVal e As EventArgs) Handles MyBase.Load
    3.     'Get a reference to the one and only MdiClient in the Controls collection.
    4.     With Me.Controls.OfType(Of MdiClient).Single()
    5.         'Set properties of the MdiClient here, e.g.
    6.         .BackgroundImage = My.Resources.MdiBackgroundImage
    7.     End With
    8. End Sub
    Last edited by jmcilhinney; Mar 15th, 2019 at 08:57 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width