Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C#
Article

MDI Container background

Rate me:
Please Sign up or sign in to vote.
1.56/5 (6 votes)
2 Sep 2006CPOL2 min read 52.3K   1.2K   13   2
Change mdi form background picture at run time

Introduction<o:p>

 <o:p>

Change mdi form background picture at run time<o:p>

This article demonstrate how to change MDI Container background image at run time <o:p>

 <o:p>

Step 1: Create Windows Application Project<o:p>

 <o:p>

Please do the following to create a Windows Application project:<o:p>

  • Select File menu -> New -> Project. <o:p>
  • Choose C# from Project Types pane. <o:p>
  • In the Templates pane, choose Windows Application for Visual C# projects. <o:p>

Step 2: <o:p>

            Add Controls<o:p>

1.       Menustrip Control<o:p>

2.       OpenFileDialog Control<o:p>

 <o:p>

Change              IsMdiContainer Property = True<o:p>

 <o:p>

Step 3: Coding Sections<o:p>

  1. Open Image file <o:p>

private void openToolStripMenuItem_Click(object sender, EventArgs e)<o:p>

  {<o:p>

    string Backpath = null;<o:p>

  try<o:p>

    {<o:p>

          openFileDialog1.Filter = "Jpg|*.jpg";<o:p>

          openFileDialog1.ShowDialog();<o:p>

          Backpath = openFileDialog1.FileName;<o:p>

          // calling Image background method<o:p>

          Change_BackgroundImage(Backpath);<o:p>

    }<o:p>

  catch (Exception ex)<o:p>

    {<o:p>

           MessageBox.Show(ex.Message);<o:p>

    }<o:p>

  }<o:p>

  1. Change_BackgroundImage(Backpath) Method<o:p>

       private void Change_BackgroundImage(string _path )<o:p>

        {<o:p>

            string imagepath = _path;<o:p>

            System.IO.FileStream fs;<o:p>

            // MDI Form image background layout change here<o:p>

            //(Remember control imagebakground layout take default form background layount )<o:p>

            this.BackgroundImageLayout = ImageLayout.Stretch;<o:p>

            // Checking File exists if yes go ---><o:p>

            if (System.IO.File.Exists(imagepath))<o:p>

            {<o:p>

                // Read Image file<o:p>

                fs = System.IO.File.OpenRead(imagepath);<o:p>

                fs.Position = 0;<o:p>

                // Change MDI From back ground picture<o:p>

                foreach (Control ctl in this.Controls)<o:p>

                {<o:p>

                    if (ctl is MdiClient)<o:p>

                    {<o:p>

                        //ctl.BackColor = Color.AntiqueWhite;<o:p>

                        ctl.BackgroundImage = System.Drawing.Image.FromStream(fs);<o:p>

                        break;<o:p>

                    }<o:p>

                }<o:p>

             }<o:p>

        }<o:p>

Conclusion<o:p>

I am looking forward to receive any comments/suggestion you have for me.<o:p>

Thank you for reading; I sincerely hope this article will help you a bit or two to know reporting services better through my applied approach<o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

License

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralProblem to use this for RightToLeft Layout MDI Form Pin
Farhad Qeshlaqi3-Mar-11 19:28
Farhad Qeshlaqi3-Mar-11 19:28 
GeneralA Guide To Writing Articles For Code Project Pin
KiwiPiet3-Sep-06 15:33
KiwiPiet3-Sep-06 15:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.