Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
how to create 200 lines with random width?
I mean something like below picture:
http://www.4shared.com/photo/wkRfD4B-/Form.html
Posted
Updated 25-May-11 18:17pm
v3
Comments
Mathew Crothers 26-May-11 0:18am    
In what GDI+, directx, opengl, a DataGridView?
Member 7904482 26-May-11 0:21am    
I mean something like below picture:
http://www.4shared.com/photo/wkRfD4B-/Form.html
Sergey Alexandrovich Kryukov 26-May-11 1:26am    
WPF, Forms, or what? Tag it!
--SA
Sergey Alexandrovich Kryukov 26-May-11 1:32am    
Please don't post you code which is not the solution as solution. Next time, please use "Improve question", "Add comment", reply to comments.
--SA

use the Paint event like this
private void form_Paint(object sender, PaintEventArgs 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 = e.Graphics;
        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);                
    }
}
 
Share this answer
 
Comments
Member 7904482 26-May-11 0:31am    
this code doesn't do anything!
what I have to do?
Mathew Crothers 26-May-11 0:40am    
You have actually put this code into the paint event of the form?

You can do this in the designer or in your constructor like this

this.Paint += new System.Windows.Forms.PaintEventHandler(this.form_Paint);
Member 7904482 26-May-11 0:46am    
could you write a sample for me because its not work?
thanks
Sergey Alexandrovich Kryukov 26-May-11 1:31am    
Better, but it would be good to explain how to setup the event. Also, OnPaint is better is this is a form . It makes me to vote 4.
--SA
1) You create a small windows form application
2) You create a simple random number generator method
3) You loop 200 times
a) Retrieve a value from the random number generator
b) Draw a line using the value from the random number generator as the length.
4) You're done.

Try some code. If you have issues at that point ask for something more specific related to the code. I actually wanted to see how quickly I could slap a form together to match that in the image link you provided and it took under 1/2 hour so there isn't much code required.

Cheers.
 
Share this answer
 
Comments
Member 7904482 26-May-11 0:28am    
but I need create 200 lines when I run C# program not when I click a button
Sergey Alexandrovich Kryukov 26-May-11 1:27am    
Get my 1 for the question and think a bit on what's "program run".
--SA
fjdiewornncalwe 26-May-11 12:08pm    
Read what I wrote again, I haven't put anything whatsoever in there that involves a button.
Sergey Alexandrovich Kryukov 26-May-11 1:28am    
Marcus, if it's form, you still need to explain how to do OnPaint and instance of Graphics, as a minimum...
--SA
fjdiewornncalwe 26-May-11 12:08pm    
I got the impression that the OP hadn't even tried anything yet, so I wanted the OP to ask regarding that specifically. At this point, they aren't ready for that yet.
Try adding this to your class. As SAKryukov said it would be better to override the OnPaint event.

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(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 = e.Graphics;
        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);
    }
}
 
Share this answer
 
Comments
Member 7904482 26-May-11 2:29am    
I find sample that I need but it needs .net framework 4
if it is possible convert it to older version. the sample is in this link: http://www.codeproject.com/KB/recipes/SortVisualization.aspx
Mathew Crothers 26-May-11 2:40am    
I have just tested the code above and it works fine. Just copy and paste it into your class definition.
Member 7904482 26-May-11 2:44am    
yes it works
thanks alot

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