Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I write below code:
private void button1_Click(object sender, EventArgs e)
        {
            Random RandomClass = new Random();
            int height = 1;
            
            for (int i = 0; i < 200; i++)
            {
                height += 2;
                int RandomNumber = RandomClass.Next(16, 215);
                System.Drawing.Graphics my_graph = this.CreateGraphics();
                System.Drawing.Graphics my_graph2 = this.CreateGraphics();
                System.Drawing.Graphics my_graph3 = this.CreateGraphics();
                Point my_point_s1 = new System.Drawing.Point(15, height);
                Point my_point_f1 = new System.Drawing.Point(RandomNumber, height);
                arrayToSort[i] = RandomNumber - 15;
                Point my_point_s2 = new System.Drawing.Point(245, height);
                Point my_point_f2 = new System.Drawing.Point(RandomNumber+245, height);
                Point my_point_s3 = new System.Drawing.Point(500, height);
                Point my_point_f3 = new System.Drawing.Point(RandomNumber+500, height);
                my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_s1, my_point_f1);
                my_graph2.DrawLine(System.Drawing.Pens.Purple, my_point_s2, my_point_f2);
                my_graph3.DrawLine(System.Drawing.Pens.Red, my_point_s3, my_point_f3);
                my_graph.Dispose();
            }
        }

now I want to sort it. I mean that redraw all the lines sorted ascending or descending.
Posted

What you do is wrong in principle. You should never create an instance of Graphics. You need only one instance which you will always get in the event arguments of your OnPaint method or your handler of the Paint event.

This is what you should do:
capture the drawing on a panel[^].

—SA
 
Share this answer
 
v2
Comments
fjdiewornncalwe 26-May-11 15:19pm    
Correct. +5. I wonder what the next step in his homework is... :)
Sergey Alexandrovich Kryukov 28-May-11 2:38am    
Thank you, Marcus.
--SA
A couple of points:
1) You don't need to create three graphics contexts, in fact it is a bad idea to do that as Graphics contexts are scarce resources. Create one, and use that three times:
int RandomNumber = RandomClass.Next(16, 215);
System.Drawing.Graphics my_graph = this.CreateGraphics();
Point my_point_s1 = new System.Drawing.Point(15, height);
Point my_point_f1 = new System.Drawing.Point(RandomNumber, height);
arrayToSort[i] = RandomNumber - 15;
Point my_point_s2 = new System.Drawing.Point(245, height);
Point my_point_f2 = new System.Drawing.Point(RandomNumber+245, height);
Point my_point_s3 = new System.Drawing.Point(500, height);
Point my_point_f3 = new System.Drawing.Point(RandomNumber+500, height);
my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_s1, my_point_f1);
my_graph.DrawLine(System.Drawing.Pens.Purple, my_point_s2, my_point_f2);
my_graph.DrawLine(System.Drawing.Pens.Red, my_point_s3, my_point_f3);
my_graph.Dispose();
2) Dealing with the first point solves the second: you are responsible for disposing of all Graphics object you create: If you do not, you will get unpredictable problems later...
3) Don't draw these in a button click event: instead, handle the Paint event and draw them there. Just use the Invalidate method, and it will force a redraw. This also gets rid of both problems from 1 and 2 above, because the paint event hands you the Graphics object as part of the PaintEventArgs parameter, so you don't have to cerate and dispose any of them at all.
If you don't draw these in the Paint event, they will not be redrawn if anything covers the window, or if it is minimised.
 
Share this answer
 
Comments
Member 7904482 26-May-11 4:52am    
but I need a button to redraw lines again
OriginalGriff 26-May-11 4:57am    
So? You have one. Set up the parameters you want to draw, then call the Invalidate method. This forces the Paint event, where you actually draw the lines.
Member 7904482 26-May-11 4:58am    
could you tell me how to do it
OriginalGriff 26-May-11 5:08am    
Which bit(s) are giving you problems? It's your homework, I'm not going to just write down a solution for you! :laugh:
Member 7904482 26-May-11 5:15am    
ok I find it
First add all lines to an array, next sort the array.
You can use my_graph.DrawLines(color, array) to draw all lines in the (sorted) array.
 
Share this answer
 
Comments
Member 7904482 26-May-11 4:42am    
can you tell me how add all lines to an array
Sergey Alexandrovich Kryukov 26-May-11 14:26pm    
Didn't you pay attention the the graphics is all wrong?
Please see my answer.
--SA
 
Share this answer
 
Comments
Member 7904482 26-May-11 4:43am    
I see it, but I dont have visual studio 2010 to open it
ambarishtv 26-May-11 4:59am    
try this tool
http://www.codeproject.com/KB/macros/SolutionConverter.aspx
Member 7904482 26-May-11 5:03am    
thanks
Sergey Alexandrovich Kryukov 26-May-11 14:26pm    
Didn't you pay attention the the graphics is all wrong?
Please see my answer.
--SA

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