Question Draw 200 lines with random width

msasan1367

New member
Joined
May 26, 2011
Messages
1
Programming Experience
Beginner
how to create 200 lines with random width?
I mean something like below picture:
Form.jpg - 4shared.com - photo sharing - download image

I have this code that just work with a button, but I need create 200 lines when I run C# program:

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(11, 111);
System.Drawing.Graphics my_graph = this.CreateGraphics();
Point my_point_one = new System.Drawing.Point(10, 2*(i+1));
Point my_point_two = new System.Drawing.Point(RandomNumber, 2 * (i + 1));
my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_one, my_point_two);
my_graph.Dispose();
}
}

How do I do it?
 
A Shortcut

Alright, I think the problem is you are trying to draw something on a form which is not drawn yet. So let it finish drawing itself and we can draw something on it.

I used Threading in this case.

C#:
private void Form1_Load(object sender, EventArgs e)
        {
            Thread T1 = new Thread(draw);
            T1.Start();
        }

        private void draw()
        {

            Thread.Sleep(400);
          Random RandomClass = new Random(); 
int height = 1; 

for (int i = 0; i<= 200; i++) 
{ 
height += 2; 
int RandomNumber = RandomClass.Next(11, 111); 
System.Drawing.Graphics my_graph = this.CreateGraphics(); 
Point my_point_one = new System.Drawing.Point(10, 2*(i+1)); 
Point my_point_two = new System.Drawing.Point(RandomNumber, 2 * (i + 1)); 
my_graph.DrawLine(System.Drawing.Pens.Blue, my_point_one, my_point_two); 
my_graph.Dispose(); 
}
 }
 }


What I did was to call the draw method in the load method on a different thread, the Thread T1 will sleeep for 400 milli second so that the main thread can finish drawing the form. and T1 can draw the lines on the form.

Hope this helps
 
Back
Top Bottom