Graphs disappear from panel
-
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 :)
-
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 :)
Where/how are you creating the graphs? Are you not drawing using the
Paint
event of the Panel? Calin -
Where/how are you creating the graphs? Are you not drawing using the
Paint
event of the Panel? CalinAs 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); }
-
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 :)
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
-
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); }
Well, you may use this function in
Paint
event. Add an handler forPaint
event of GraphPanel and usee.Graphics
(instead ofGraphPanel.CreateGraphics()
) In this way, your graphs will be drawn every time when theForm
is repainted. Now, callGraphPanel.Refresh()
intrackBarNR_Scroll()
in order to refresh the graph when scrolling. Calin -
Well, you may use this function in
Paint
event. Add an handler forPaint
event of GraphPanel and usee.Graphics
(instead ofGraphPanel.CreateGraphics()
) In this way, your graphs will be drawn every time when theForm
is repainted. Now, callGraphPanel.Refresh()
intrackBarNR_Scroll()
in order to refresh the graph when scrolling. Calin -
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
-
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 :)
-
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.