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. Graphs disappear from panel

Graphs disappear from panel

Scheduled Pinned Locked Moved C#
helptutorialcsharpdelphidata-structures
9 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.
  • S Offline
    S Offline
    StuffyEst
    wrote on last edited by
    #1

    Hello there, First of all I know this is a question asked many times etc. But still I'm quite stuck since I haven't found an answer that would work for my case. So, my project is made in Borland (C# Builder) and consists of following: - I have a panel where graphs are generated (note: 2+ graphs) - I have sliders to change the values inside graph function (to make them longer, move them up or down, etc.) The problem I'm having is that, when I move the window out of bounds, or place another window over my graphs, they just disappear. I've read about the OnPaint, but I'm not sure how I'm suppose to add this to my code, since my graphs are updated every time trackBarNR_Scroll event occurs. Oh and finally, of course it's suppose to be a Windows Application :laugh: Hopefully someone could guide me, how to make those graphs not disappear :)

    C L S 3 Replies Last reply
    0
    • S StuffyEst

      Hello there, First of all I know this is a question asked many times etc. But still I'm quite stuck since I haven't found an answer that would work for my case. So, my project is made in Borland (C# Builder) and consists of following: - I have a panel where graphs are generated (note: 2+ graphs) - I have sliders to change the values inside graph function (to make them longer, move them up or down, etc.) The problem I'm having is that, when I move the window out of bounds, or place another window over my graphs, they just disappear. I've read about the OnPaint, but I'm not sure how I'm suppose to add this to my code, since my graphs are updated every time trackBarNR_Scroll event occurs. Oh and finally, of course it's suppose to be a Windows Application :laugh: Hopefully someone could guide me, how to make those graphs not disappear :)

      C Offline
      C Offline
      Calin Tatar
      wrote on last edited by
      #2

      Where/how are you creating the graphs? Are you not drawing using the Paint event of the Panel? Calin

      S 1 Reply Last reply
      0
      • C Calin Tatar

        Where/how are you creating the graphs? Are you not drawing using the Paint event of the Panel? Calin

        S Offline
        S Offline
        StuffyEst
        wrote on last edited by
        #3

        As a matter of fact, I'm not (and besides I'm not that familiar with C# yet :D) One of the Graphics function is here (and that is called every time I have trackBarNR_Scroll): private ArrayList GraphPointsList2 = new ArrayList(); private void DrawGraphic2(int k3, int k4) { int i=1; int j=1; int x=0; int y=0; double value=0; double sin=0; Point GraphPoint2; System.Drawing.Graphics graph2; graph2 = GraphPanel.CreateGraphics(); graph2.SmoothingMode = SmoothingMode.AntiAlias; Pen penCurrent2 = new Pen(System.Drawing.Color.Red, 2); for(x = 0; x<GraphPanel.Width; x++) { GraphPoint2 = new Point(); GraphPoint2.X = x; sin = Math.Sin((0.01*k4)*x); value = 50*-sin+k3; y = GraphPanel.Height - (int)value; GraphPoint2.Y = y; GraphPointsList2.Add(GraphPoint2); i++; j++; } Point[] pointArray2 = ( Point[] ) GraphPointsList2.ToArray( GraphPointsList[ 0 ].GetType() ); graph2.DrawCurve(penCurrent2, pointArray2); }

        C 1 Reply Last reply
        0
        • S StuffyEst

          Hello there, First of all I know this is a question asked many times etc. But still I'm quite stuck since I haven't found an answer that would work for my case. So, my project is made in Borland (C# Builder) and consists of following: - I have a panel where graphs are generated (note: 2+ graphs) - I have sliders to change the values inside graph function (to make them longer, move them up or down, etc.) The problem I'm having is that, when I move the window out of bounds, or place another window over my graphs, they just disappear. I've read about the OnPaint, but I'm not sure how I'm suppose to add this to my code, since my graphs are updated every time trackBarNR_Scroll event occurs. Oh and finally, of course it's suppose to be a Windows Application :laugh: Hopefully someone could guide me, how to make those graphs not disappear :)

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

          Hi, here are a few simple rules that should guide you: there are several steps to 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]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          S 1 Reply Last reply
          0
          • S StuffyEst

            As a matter of fact, I'm not (and besides I'm not that familiar with C# yet :D) One of the Graphics function is here (and that is called every time I have trackBarNR_Scroll): private ArrayList GraphPointsList2 = new ArrayList(); private void DrawGraphic2(int k3, int k4) { int i=1; int j=1; int x=0; int y=0; double value=0; double sin=0; Point GraphPoint2; System.Drawing.Graphics graph2; graph2 = GraphPanel.CreateGraphics(); graph2.SmoothingMode = SmoothingMode.AntiAlias; Pen penCurrent2 = new Pen(System.Drawing.Color.Red, 2); for(x = 0; x<GraphPanel.Width; x++) { GraphPoint2 = new Point(); GraphPoint2.X = x; sin = Math.Sin((0.01*k4)*x); value = 50*-sin+k3; y = GraphPanel.Height - (int)value; GraphPoint2.Y = y; GraphPointsList2.Add(GraphPoint2); i++; j++; } Point[] pointArray2 = ( Point[] ) GraphPointsList2.ToArray( GraphPointsList[ 0 ].GetType() ); graph2.DrawCurve(penCurrent2, pointArray2); }

            C Offline
            C Offline
            Calin Tatar
            wrote on last edited by
            #5

            Well, you may use this function in Paint event. Add an handler for Paint event of GraphPanel and use e.Graphics (instead of GraphPanel.CreateGraphics()) In this way, your graphs will be drawn every time when the Form is repainted. Now, call GraphPanel.Refresh() in trackBarNR_Scroll() in order to refresh the graph when scrolling. Calin

            S 1 Reply Last reply
            0
            • C Calin Tatar

              Well, you may use this function in Paint event. Add an handler for Paint event of GraphPanel and use e.Graphics (instead of GraphPanel.CreateGraphics()) In this way, your graphs will be drawn every time when the Form is repainted. Now, call GraphPanel.Refresh() in trackBarNR_Scroll() in order to refresh the graph when scrolling. Calin

              S Offline
              S Offline
              StuffyEst
              wrote on last edited by
              #6

              Thanks, I'll try that out

              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, here are a few simple rules that should guide you: there are several steps to 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]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                S Offline
                S Offline
                StuffyEst
                wrote on last edited by
                #7

                Those are some really useful steps, I'll try to keep them in mind :D Thanks a bunch.

                1 Reply Last reply
                0
                • S StuffyEst

                  Hello there, First of all I know this is a question asked many times etc. But still I'm quite stuck since I haven't found an answer that would work for my case. So, my project is made in Borland (C# Builder) and consists of following: - I have a panel where graphs are generated (note: 2+ graphs) - I have sliders to change the values inside graph function (to make them longer, move them up or down, etc.) The problem I'm having is that, when I move the window out of bounds, or place another window over my graphs, they just disappear. I've read about the OnPaint, but I'm not sure how I'm suppose to add this to my code, since my graphs are updated every time trackBarNR_Scroll event occurs. Oh and finally, of course it's suppose to be a Windows Application :laugh: Hopefully someone could guide me, how to make those graphs not disappear :)

                  S Offline
                  S Offline
                  S K Y
                  wrote on last edited by
                  #8

                  Did u solve your problem???

                  A S E L A

                  S 1 Reply Last reply
                  0
                  • S S K Y

                    Did u solve your problem???

                    A S E L A

                    S Offline
                    S Offline
                    StuffyEst
                    wrote on last edited by
                    #9

                    God took ages to find my post .. Anyway I've made some progress and still stuck on other part. I have 3 different functions, one for graphic, second for graphic and third for axis. I solved the paint problem with Axis, since its static and never changes, so it was quite easy to convert to paint event. On the other hand, graphs have both 2 parameters that they receive and the graph changes after that according to the values given. Example : public void JoonistaGraafik(int k1, int k2) now when I want to make it to paint event "public void JoonistaGraafik(object sender,PaintEventArgs e, int k1, int k2)" I have problem calling out in InitializeComponents(); this.GraphPanel.Paint += new System.Windows.Forms.PaintEventHandler(JoonistaGraafik(slider_a, slider_b)); the thing is that, my two values "slider_a" and "slider_b" are generated in the trackbar_scroll event and then JoonistaGraafik(slider_a, slider_b); was called out. I'm pretty bad in explaining, but I hope that made a bit sence. In conclusion, the question is, how can I call out "this.GraphPanel.Paint += new System.Windows.Forms.PaintEventHandler(JoonistaGraafik(slider_a, slider_b));" if the slider_a and slider_b values have to change when I move the trackbar. Thanks, and sorry for not replying so fast.

                    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