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. Why the line disappear?

Why the line disappear?

Scheduled Pinned Locked Moved C#
graphicshelpquestion
10 Posts 5 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.
  • L Offline
    L Offline
    lune12
    wrote on last edited by
    #1

    Hi, I use the following code for drawing line on a panel, but after few seconds the line disappear. Can someone help with this?

    Graphics formGraphics = panel1.CreateGraphics();
    //p1 and p2 are well defined points.
    formGraphics.DrawLine((Pens.Red, p1, p2);

    Thanks in advance. Lune

    L 0 2 Replies Last reply
    0
    • L lune12

      Hi, I use the following code for drawing line on a panel, but after few seconds the line disappear. Can someone help with this?

      Graphics formGraphics = panel1.CreateGraphics();
      //p1 and p2 are well defined points.
      formGraphics.DrawLine((Pens.Red, p1, p2);

      Thanks in advance. Lune

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

      Hi, that indicates you didn't paint it in the right way. there are several steps to correctly draw something so it becomes visible on the screen: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      H L 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi, that indicates you didn't paint it in the right way. there are several steps to correctly draw something so it becomes visible on the screen: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #3

        And you have the nerve to suggest that I am verbose! :laugh:

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        L 1 Reply Last reply
        0
        • L lune12

          Hi, I use the following code for drawing line on a panel, but after few seconds the line disappear. Can someone help with this?

          Graphics formGraphics = panel1.CreateGraphics();
          //p1 and p2 are well defined points.
          formGraphics.DrawLine((Pens.Red, p1, p2);

          Thanks in advance. Lune

          0 Offline
          0 Offline
          0x3c0
          wrote on last edited by
          #4

          CreateGraphics is temporary. Use e.Graphics and the Form.Paint event instead. You also appear to have a syntax error (two brackets in front of Pens.Red)

          L 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, that indicates you didn't paint it in the right way. there are several steps to correctly draw something so it becomes visible on the screen: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            L Offline
            L Offline
            lune12
            wrote on last edited by
            #5

            Thanks, But do you mean that if I want a persistant line I should keep the point variables all along the program? and also you suggest that the line should be drawn in the OnPaint event but is it the only way? I need to create the line as a response to an other events, so what is the right way? Thanks,

            L 1 Reply Last reply
            0
            • H Henry Minute

              And you have the nerve to suggest that I am verbose! :laugh:

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

              Fortunately I do have a small collection of standard replies available, this is the one I give for all paint problems unless more specific symptoms ask for a more personalized answer. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              1 Reply Last reply
              0
              • L lune12

                Thanks, But do you mean that if I want a persistant line I should keep the point variables all along the program? and also you suggest that the line should be drawn in the OnPaint event but is it the only way? I need to create the line as a response to an other events, so what is the right way? Thanks,

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

                lune12 wrote:

                what is the right way?

                I just indicated the right way. if you want to change the drawing, then change the data and call Control.Invalidate() to request an automatic repaint. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  lune12 wrote:

                  what is the right way?

                  I just indicated the right way. if you want to change the drawing, then change the data and call Control.Invalidate() to request an automatic repaint. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                  L Offline
                  L Offline
                  lune12
                  wrote on last edited by
                  #8

                  Thanks, it's working but isn't it a waist of time to redraw all the line each time that the paint event is invoked?

                  L 1 Reply Last reply
                  0
                  • L lune12

                    Thanks, it's working but isn't it a waist of time to redraw all the line each time that the paint event is invoked?

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

                    you could reduce the amount of work executed by taking into account the clipping data that is present in the PaintEventArgs/Graphics. IMO that is worthwhile only in special cases, like complex paintings that need frequent repaints (e.g. as in games, which probably get programmed quite differently anyhow). :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                    1 Reply Last reply
                    0
                    • 0 0x3c0

                      CreateGraphics is temporary. Use e.Graphics and the Form.Paint event instead. You also appear to have a syntax error (two brackets in front of Pens.Red)

                      L Offline
                      L Offline
                      Letsan
                      wrote on last edited by
                      #10

                      Thanks, very helpfull.

                      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