Click here to Skip to main content
15,032,024 members
Please Sign up or sign in to vote.
2.64/5 (4 votes)
See more:
Good evening everyone,
I created a panel where I drew some lines that update via a Timer1 with an interval of 200 ms, and with a Invalidate (). The point is that I wanted to create a trail lines, or to keep the lines that are created, ranging through updating of coordinates X, Y. As the Invalidate () does repaint the panel, just updates the Line with the new value, and delete the others. How to solve this? I've read a lot, but nothing can solve this problem.

private void RefreshTimer_Tick(object sender, EventArgs e)
            RefreshTimer.Interval = 200;

private void Panel1_Paint(object sender, PaintEventArgs e)
                Graphics gi = e.Graphics;
                gi.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                angle = Valor3;//Comes from a microcontroller via serial port
                Pen pen2 = new Pen(Color.Red, 2f);
                int r = Uval;//Comes from a microncontroller via serial port
                int X1 = Panel1.Size.Width/2;
                int Y1 = Panel1.Size.Height/2;
                Point pStart = new Point(X1, Y1);

                int X2 = (int)(X1 + r * Math.Cos((double)angle * Math.PI/180));
                int Y2 = (int)(Y1 + r * Math.Sin((double)angle * Math.PI/180));
                Point pEnd = new Point(X2, Y2);
                gi.DrawLine(pen2, pStart, pEnd);

                gi.DrawEllipse(new Pen(Color.Green, 2f), 0, 0, Panel1.Size.Width,      Panel1.Size.Height);


Thank for all.


Updated 19-Jan-14 15:38pm
BillWoodruff 19-Jan-14 21:01pm
Is it the case that the lines once drawn are fixed, or is the case that you need to clear them from time to time and draw new ones using different co-ordinates; I'm not totally clear reading your post what your intent is.
Member 10369986 19-Jan-14 21:09pm
Greetings BillWoodruff

Thank you for your kind reply. Need to update every +/-200ms with new co-ordinates.and would like to keep the other lines already created, for a few seconds at least, and with this created a Line trail. I hope I am clear now.




BillWoodruff 19-Jan-14 21:11pm
So, "trail" here means a kind of animation effect ... the previous line persist for an interval ?
Member 10369986 19-Jan-14 21:28pm
Exactly. Need to maintain the old Lines.
You need to maintain the lines, so do exactly this. Please see my answer.

Greetings all,

I found the following solution:

Create a point List:

List<point> pointStart = new List<point>();
List<point> pointEnd = new List<point>();

Get a new point and add it to the list:


Then with for cicle:

for (int x = 0; x < pointEnd.Count; x++)
 g.DrawLine(pen2, pointStart[x], pointEnd[x]);

Maybe not the best solution, but it works...

I am working in another solution since this is fast but limited in points stored ...

I wish to acknowledge the assistance provided by Sergey.


First of all, you could not possibly invent something worse than using a timer. How, tell me please, timer events are related to the drawing you make? You drawing actions already generate events, mouse events; and this is all you need. If you change the graphical data programmatically, then again, you already have moment of time where you change it.

The basic approach is this: you need to have the model of your graphics stored in memory. The overridden virtual method System.Windows.Control.OnPaint or the event handler of the event System.Windows.Control.OnPaint should be used to render the model on screen (or, actually, any other device, such as a bitmap or print page instance of System.Drawing.Graphics. Each time WM_PAINT Windows message is generated, your graphics rendering. For animation, you need to force this process or re-rendering as soon as your graphic model changes. This is what Invalidate does.

See also my past answers:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
How to speed up my application?[^].


You cannot possibly reduce flicker with the timer. Instead use double-buffering:[^].

Member 10369986 20-Jan-14 11:06am
Greetings Sergey

Thank you for your help. The Timer idea was to reduce flicker and then stay like that.
Anyway, I read what you have posted, and it would be great if you could post an idea of the code for this, becoming this way clearer, and pedagogical. I have and I got an idea how to do, but is a little tricky to implement.


Sergey Alexandrovich Kryukov 20-Jan-14 11:14am
As I say, this is a bad idea. Please see my update to the answer.

As to the "way clearer, and pedagogical", you might need to wait for my big article, but don't hold your breath: it waits in a line of few more unpublished articles...
Member 10369986 20-Jan-14 12:12pm
Greetings Sergey

regarding your update, I sort out my flicker problem with:

protected override CreateParams CreateParams
CreateParams handleParam = base.CreateParams;
handleParam.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
// handleParam.ExStyle |= 0x04000000L; //
return handleParam;

But my problem is now how to create a trail effect on my Drawline (x1,y1,x2,y2) in a Panel.


Sergey Alexandrovich Kryukov 20-Jan-14 13:09pm
Good; as to the second question, the main part of my answer explains that. Is anything unclear? What part of it?
Member 10369986 20-Jan-14 13:47pm

From a concept to the practical side of the model, goes a certain distance. You explained (and I appreciate), but the problem is that without a code example ("Hello World"), is complicated. I understand that you have no time, but only with what you explained I still cannot implement. However, I’m trying other ways to resolve this issue. I've seen and read many interesting things, tried customizing for my code, but does not work!
See this - -
(I already eliminated the Timer).


Sergey Alexandrovich Kryukov 20-Jan-14 14:00pm
You should not use "other ways". You should not "try" at all, you need to do sure things with full understanding. Nothing can replace some hard learning.
Here is what you can do: read my answers again, this time making records of all APIs and documentation pages referenced, open those pages and learn related documentation until the general understanding is achieved. When you come the some particular concerns, feel free to ask some follow-up questions.
Member 10369986 20-Jan-14 16:02pm
Greetings Sergey

Notwithstanding being very grateful for your teachings, I do not agree that it is only through reading, and reading the APIs I get there. Hard learning is not only read APIs, if so, in the universities they will give you the APIs send you home and then you were an excellent programmer. Application Programming Interface (API) specifies how some software components should interact with each other, but do not invalidates the practical use, the hands-on, example analysis. Learning is an empirical process! In any case I will follow your usefully tips, because I do not want to appear to want things offered, not my nature. I like to understand, and I appreciate your support. Once justified, I'll be posting. Thanks for everything Sergey.


Sergey Alexandrovich Kryukov 20-Jan-14 16:14pm
It looks like you know better how to learn, it is apparent from your results :-) (Who told you that word, "only", anyway?!)
Let me tell you something. All capable engineers I know, all of them, never ask anyone for help like that, they did it all by themselves: read documentation (there were very little back then, no computer forums at all), did research, developed purely experimental or research code, analyzed results. Never asked anyone to write a piece of code for them. Never, even when they were the beginners.
Now we do a lot of exclusion; in this forum, we write short fragments of code based on OP specific situation, digest something which already should be obvious from documentation, and do a lot more.
Maybe we a doing wrong things? Maybe the condition for the survival of the beginners should be way more severe, so only decent one would continue? Maybe then we would have less "programmers" but more work actually done?..
Member 10369986 20-Jan-14 17:39pm
Greetings Sergey,

Let's put a point of order here. In my opinion there is no capable engineer who do not ask, no capable engineer who has never asked. If not asked, then copied from the side partner!

Learning is asking, investigating is asking, and discover is the outcome of all these questions.

I do not know any lecturer who inquiries the "quality" or the “type" of asked questions, as much as the naive questions are, or refute to clarify the doubts!

No matter how philosophical question are, we asked!

If we are in the street and looking for a specific site, we ask - and no one sends you to consult the city map!

If the world today there is huge dynamics knowledge, resulting in steady and everyday improvement, it is because the ease way which we share knowledge! Sharing knowledge is also asking!

"Maybe we a doing wrong things?”

I do not think we are doing anything wrong, we share! Even if I do not start any post, this does not invalidate that we are asking because we read what someone “asked for us”!

As for the "only decent one would continue ", I fear that the "decent" adjective is relative, because I only know decent people or not, and "Engineers” are a career and not people!

But let me share a passage: in college I had a professor - Dr. Valdmir Gorban -, discipline Mathematical analysis, which in the last chapters of B. Demidovitch - Problems and Exercises in Mathematical Analysis, I asked him for what purpose served what we have learned in that chapter. He gave classes terminated, and left angry. The next day, entered the classroom, and explained with clarity of teaching, the practical usefulness of what was taught in the previous class.

As for the engineers you know, they are super-engineers, not like me, a simple human!

Anyway, no hard feelings Sergey, since I am learned a lot from you. I never lose, because I am always learning and asking! Every seconds! Like a Timer!


Sergey Alexandrovich Kryukov 20-Jan-14 18:19pm
Why are you asking for an advice if you know what engineers should do better? Apparently, you are capable enough to solve your problem. :-)
No, I have no hard feeling, too, but I would advise you to pay considerable efforts and then ask more specific questions.
Good luck,
Member 10369986 20-Jan-14 18:54pm
Greetings Sergey,
I just replied to your comments. And yes, I am capable enough to solve my problem, because when I do not know, I do not mind to ask! When I know I don’t mind to help unconditionally. I'm not God, nor put me up there looking down!
Honestly Sergey, I hope you never have to ask anything or read responses of others as well as I do, mere human, even in silence.
Once I have the answer to my problem, I'll post it here because already noticed that is an issue. Taught, I learn much more...
No hard feeling,
Thank you for your help and support. I appreciate.



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900