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
  1. Home
  2. General Programming
  3. C#
  4. How do I pass and reference a Panel?

How do I pass and reference a Panel?

Scheduled Pinned Locked Moved C#
graphicsquestioncsharpdata-structureshelp
7 Posts 4 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.
  • D Offline
    D Offline
    dino2094
    wrote on last edited by
    #1

    I have a form with a panel called test_panel1. I need to display a bitmap on that panel and I have this code that works. private void SetBitmap(Bitmap bmap) { Graphics graph = this.test_panel1.CreateGraphics(); graph.DrawImage(bmap, 0, 0); } I now need to add another panel, lets call it test_panel2 and I need to do the same thing. But I'd like to call SetBitmap and pass it the panel to put the bitmap on Something like: SetBitmap(mybitmap,test_panel2) or SetBitmap(mybitmap,test_panel1) and have the function look like private void SetBitmap(Bitmap bmap,Panel pnl) { Graphics graph = this.pnl.CreateGraphics(); graph.DrawImage(bmap, 0, 0); } But C# doesn't like it and I don't know what the proper way to do this is? Any help is very very welcomed.

    D C 2 Replies Last reply
    0
    • D dino2094

      I have a form with a panel called test_panel1. I need to display a bitmap on that panel and I have this code that works. private void SetBitmap(Bitmap bmap) { Graphics graph = this.test_panel1.CreateGraphics(); graph.DrawImage(bmap, 0, 0); } I now need to add another panel, lets call it test_panel2 and I need to do the same thing. But I'd like to call SetBitmap and pass it the panel to put the bitmap on Something like: SetBitmap(mybitmap,test_panel2) or SetBitmap(mybitmap,test_panel1) and have the function look like private void SetBitmap(Bitmap bmap,Panel pnl) { Graphics graph = this.pnl.CreateGraphics(); graph.DrawImage(bmap, 0, 0); } But C# doesn't like it and I don't know what the proper way to do this is? Any help is very very welcomed.

      D Offline
      D Offline
      Dawid Mazuruk
      wrote on last edited by
      #2

      Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }

      D L C 3 Replies Last reply
      0
      • D Dawid Mazuruk

        Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }

        D Offline
        D Offline
        dino2094
        wrote on last edited by
        #3

        So simple, yet it is so hard! Mucho Gracias.

        C 1 Reply Last reply
        0
        • D Dawid Mazuruk

          Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, if you create an instance of a class that offers a public Dispose() method, you MUST call that method when you're done. This also applies to Control.CreateGraphics() and Graphics.FromImage() and the like !!! :)

          Luc Pattyn [My Articles]

          1 Reply Last reply
          0
          • D Dawid Mazuruk

            Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            This won't work properly. If the form is invalidated, the panel will be erased, unless this is called from a paint event, in which case, it should just use the existing graphics object.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            1 Reply Last reply
            0
            • D dino2094

              So simple, yet it is so hard! Mucho Gracias.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              What you've been told will not work if your form is ever obscured by another form.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              1 Reply Last reply
              0
              • D dino2094

                I have a form with a panel called test_panel1. I need to display a bitmap on that panel and I have this code that works. private void SetBitmap(Bitmap bmap) { Graphics graph = this.test_panel1.CreateGraphics(); graph.DrawImage(bmap, 0, 0); } I now need to add another panel, lets call it test_panel2 and I need to do the same thing. But I'd like to call SetBitmap and pass it the panel to put the bitmap on Something like: SetBitmap(mybitmap,test_panel2) or SetBitmap(mybitmap,test_panel1) and have the function look like private void SetBitmap(Bitmap bmap,Panel pnl) { Graphics graph = this.pnl.CreateGraphics(); graph.DrawImage(bmap, 0, 0); } But C# doesn't like it and I don't know what the proper way to do this is? Any help is very very welcomed.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                You should handle the paint event of the panel, and draw the bitmap onto the panel there. CreateGraphics will not redraw the image if your form is obscured by another form, as no paint event will occur.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                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