Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How can I center a picture box within an MDI Windows form?????

How can I center a picture box within an MDI Windows form?????

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joey Picerno
    wrote on last edited by
    #1

    I need to center a picture box in the exact middle of my windows MDI Parent form. Can anyone tell me the easiest way or best way to do this through code. Thank you!

    G 1 Reply Last reply
    0
    • J Joey Picerno

      I need to center a picture box in the exact middle of my windows MDI Parent form. Can anyone tell me the easiest way or best way to do this through code. Thank you!

      G Offline
      G Offline
      Gregory Gadow
      wrote on last edited by
      #2

      The client area of an MDI form is an ordinary control, although it is not directly exposed by the form. This is a method I have in Class Toolbox:

      Public Shared Function GetMdiClient(ByRef MdiForm As Form) As MdiClient
          Dim Client As MdiClient = Nothing
      
          For Each C As Control In MdiForm.Controls
              Client = TryCast(C, MdiClient)
              If Client IsNot Nothing Then Exit For
          Next
      
          Return Client
      End Function
      

      With that, you can then use this code:

          Dim MC As MdiClient = Toolbox.GetMdiClient(MyMainForm)
          If MC IsNot Nothing Then
              PictureBox1.Top = (MC.ClientRectangle.Height \\ 2) - (PictureBox1.Height \\ 2)
              PictureBox1.Left = (MC.ClientRectangle.Width \\ 2) - (PictureBox1.Width \\ 2)
          End If
      

      Using this technique, you can do things like change the background color of the MDI client area. MdiClient exposes a BackgroundImage property, which might allow you to dispense with the PictureBox entirely. Edited to fix another bug :sigh:

      modified on Wednesday, July 30, 2008 3:41 PM

      J 2 Replies Last reply
      0
      • G Gregory Gadow

        The client area of an MDI form is an ordinary control, although it is not directly exposed by the form. This is a method I have in Class Toolbox:

        Public Shared Function GetMdiClient(ByRef MdiForm As Form) As MdiClient
            Dim Client As MdiClient = Nothing
        
            For Each C As Control In MdiForm.Controls
                Client = TryCast(C, MdiClient)
                If Client IsNot Nothing Then Exit For
            Next
        
            Return Client
        End Function
        

        With that, you can then use this code:

            Dim MC As MdiClient = Toolbox.GetMdiClient(MyMainForm)
            If MC IsNot Nothing Then
                PictureBox1.Top = (MC.ClientRectangle.Height \\ 2) - (PictureBox1.Height \\ 2)
                PictureBox1.Left = (MC.ClientRectangle.Width \\ 2) - (PictureBox1.Width \\ 2)
            End If
        

        Using this technique, you can do things like change the background color of the MDI client area. MdiClient exposes a BackgroundImage property, which might allow you to dispense with the PictureBox entirely. Edited to fix another bug :sigh:

        modified on Wednesday, July 30, 2008 3:41 PM

        J Offline
        J Offline
        Joey Picerno
        wrote on last edited by
        #3

        I already am using a backgroundimage with the backgroundimage property. I just need another image ( a logo ) to overlay the background image and always be in the center of the MDIForm. I am about to attempt to try your method, I appreciate your response and I will let you know if it works or not. Thanks again!

        1 Reply Last reply
        0
        • G Gregory Gadow

          The client area of an MDI form is an ordinary control, although it is not directly exposed by the form. This is a method I have in Class Toolbox:

          Public Shared Function GetMdiClient(ByRef MdiForm As Form) As MdiClient
              Dim Client As MdiClient = Nothing
          
              For Each C As Control In MdiForm.Controls
                  Client = TryCast(C, MdiClient)
                  If Client IsNot Nothing Then Exit For
              Next
          
              Return Client
          End Function
          

          With that, you can then use this code:

              Dim MC As MdiClient = Toolbox.GetMdiClient(MyMainForm)
              If MC IsNot Nothing Then
                  PictureBox1.Top = (MC.ClientRectangle.Height \\ 2) - (PictureBox1.Height \\ 2)
                  PictureBox1.Left = (MC.ClientRectangle.Width \\ 2) - (PictureBox1.Width \\ 2)
              End If
          

          Using this technique, you can do things like change the background color of the MDI client area. MdiClient exposes a BackgroundImage property, which might allow you to dispense with the PictureBox entirely. Edited to fix another bug :sigh:

          modified on Wednesday, July 30, 2008 3:41 PM

          J Offline
          J Offline
          Joey Picerno
          wrote on last edited by
          #4

          I tried what you wrote and for some reason it isnt liking this line of code :

          Dim MC As MdiClient = clsGetMDI.GetMdiClient()

          I get a squiggly under clsGetMDI.GetMdiClient. I changed the name of the class by the way from toolbox to clsGetMDI. It is saying argument not specifyed for the parameter frmMainMDI in the public function getmdiclient etc. frmMainMDI is the name of my MDI Parent form. I have the exact same code you do except I replaced MDIForm with the name of my actual MDI form and I have a different class name where the function is located, that is it. Other than that I am using the exact same code. Any thoughts????

          G 1 Reply Last reply
          0
          • J Joey Picerno

            I tried what you wrote and for some reason it isnt liking this line of code :

            Dim MC As MdiClient = clsGetMDI.GetMdiClient()

            I get a squiggly under clsGetMDI.GetMdiClient. I changed the name of the class by the way from toolbox to clsGetMDI. It is saying argument not specifyed for the parameter frmMainMDI in the public function getmdiclient etc. frmMainMDI is the name of my MDI Parent form. I have the exact same code you do except I replaced MDIForm with the name of my actual MDI form and I have a different class name where the function is located, that is it. Other than that I am using the exact same code. Any thoughts????

            G Offline
            G Offline
            Gregory Gadow
            wrote on last edited by
            #5

            You need to pass in the the form. Sorry about that.

            J 1 Reply Last reply
            0
            • G Gregory Gadow

              You need to pass in the the form. Sorry about that.

              J Offline
              J Offline
              Joey Picerno
              wrote on last edited by
              #6

              Ok gotcha, it is working fine now. Thank you for your help. You saved me a major headache... Thanks!!!!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups