Hey guys,

I have drawing program assignment and I'm almost done, but the two things I want to do are not mentioned in my text book and I can't seem to find a solution anywhere. What this Windows Form Program is suppose to do is be a form which has 3 check boxes one for lines one for ovals and one for Rectangles and when it the particular box is checked and you press the draw button it is suppose to draw random size, shape and color of those check boxes. It works, but it goes really fast and I want to slow down the speed, and I can't find a way to stop it, I want to have a stop button to do it, but I can't seem to find a method or what exactly I need to do. below is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Lab6
{
    public partial class Form1 : Form
    {
        private Random rnd;
        public Form1()
        {
            InitializeComponent();
            rnd = new Random((int)DateTime.Now.Ticks); // seeded with ticks
        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
           
            //check which among the draw options are selected & for any combination of options randomly call the appropriate draw functions.            
            if (cbLines.Checked == true && cbOvals.Checked == true && cbRectangles.Checked == true)
            {
                while (cbLines.Checked == true && cbOvals.Checked == true && cbRectangles.Checked == true)
                {
                    switch (rnd.Next(1, 4))
                    {
                        case 1:
                            DrawLine();
                            break;
                        case 2:
                            DrawOval();
                            break;
                        case 3:
                            DrawRectangle();
                            break;
                    }
                }
            }
            else if (cbLines.Checked == true && cbOvals.Checked == true)
            {
                while (cbLines.Checked == true && cbOvals.Checked == true)
                {

                    switch (rnd.Next(1, 3))
                    {
                        case 1:
                            DrawLine();
                            break;
                        case 2:
                            DrawOval();
                            break;
                    }
                }
            }
            else if (cbOvals.Checked == true && cbRectangles.Checked == true)
            {
                while (cbOvals.Checked == true && cbRectangles.Checked == true)
                {
                    switch (rnd.Next(1, 3))
                    {
                        case 1:
                            DrawOval();
                            break;
                        case 2:
                            DrawRectangle();
                            break;
                    }
                }
            }
            else if (cbLines.Checked == true && cbRectangles.Checked == true)
            {
                while (cbLines.Checked == true && cbRectangles.Checked == true)
                {
                    switch (rnd.Next(1, 3))
                    {
                        case 1:
                            DrawLine();
                            break;
                        case 2:
                            DrawRectangle();
                            break;
                    }
                }
            }
            else
            {
                if (cbLines.Checked == true)
                {
                    while (cbLines.Checked == true)
                    {
                        DrawLine();
                    }
                }
                else if (cbOvals.Checked == true)
                {
                    while (cbOvals.Checked == true)
                    {
                        DrawOval();
                    }
                }
                else if (cbRectangles.Checked == true)
                {
                    while (cbRectangles.Checked == true)
                    {
                        DrawRectangle();
                    }
                }
            }


        }

        //Draw a line
        private void DrawLine()
        {
            System.Drawing.Graphics formGraphics = this.CreateGraphics();
            Pen myPen = new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), rnd.Next(1, 5));

            formGraphics.DrawLine(myPen, (int)rnd.Next(0, this.Width), (int)rnd.Next(0, this.Height), (int)rnd.Next(0, this.Width), (int)rnd.Next(0, this.Height));
            myPen.Dispose();
            formGraphics.Dispose();

        }

        //Draw a rectangle
        private void DrawRectangle()
        {
            System.Drawing.Graphics formGraphics = this.CreateGraphics();
            SolidBrush myBrush = new SolidBrush(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)));
            formGraphics.FillRectangle(myBrush, (int)rnd.Next(0, this.Width), (int)rnd.Next(0, this.Height), (int)rnd.Next(0, this.Width), (int)rnd.Next(0, this.Height));
            myBrush.Dispose();
            formGraphics.Dispose();
        }

        //Draw a circle
        private void DrawOval()
        {
            System.Drawing.Graphics formGraphics = this.CreateGraphics();
            SolidBrush myBrush = new SolidBrush(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)));
            formGraphics.FillEllipse(myBrush, (int)rnd.Next(0, this.Width), (int)rnd.Next(0, this.Height), (int)rnd.Next(0, this.Width), (int)rnd.Next(0, this.Height));
            myBrush.Dispose();
            formGraphics.Dispose();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            

        }


    }
}

Render drawing in a thread separately.

private void btnDraw_Click(object sender, EventArgs e)
        {

            //check which among the draw options are selected & for any combination of options randomly call the appropriate draw functions.            
            if (cbLines.Checked == true && cbOvals.Checked == true && cbRectangles.Checked == true)
            {
              System.Threading.Thread t=new System.Threading.Thread(()=>{
                
                while (cbLines.Checked == true && cbOvals.Checked == true && cbRectangles.Checked == true)
                {
                    System.Threading.Thread.Sleep(10);
                    switch (rnd.Next(1, 4))
                    {
                        case 1:
                            DrawLine();
                            break;
                        case 2:
                            DrawOval();
                            break;
                        case 3:
                            DrawRectangle();
                            break;
                    }
                }
              });
              t.Start();
            }
            else if (cbLines.Checked == true && cbOvals.Checked == true)
            {

                System.Threading.Thread t = new System.Threading.Thread(() =>
                {

                    while (cbLines.Checked == true && cbOvals.Checked == true)
                    {
                        System.Threading.Thread.Sleep(10);
                        switch (rnd.Next(1, 3))
                        {
                            case 1:
                                DrawLine();
                                break;
                            case 2:
                                DrawOval();
                                break;
                        }
                    }
                });
                t.Start();
            }
            else if (cbOvals.Checked == true && cbRectangles.Checked == true)
            {
                System.Threading.Thread t = new System.Threading.Thread(() =>
                {

                    while (cbOvals.Checked == true && cbRectangles.Checked == true)
                    {
                        System.Threading.Thread.Sleep(10);
                        switch (rnd.Next(1, 3))
                        {
                            case 1:
                                DrawOval();
                                break;
                            case 2:
                                DrawRectangle();
                                break;
                        }
                    }
                });
                t.Start();
            }
            else if (cbLines.Checked == true && cbRectangles.Checked == true)
            {
                System.Threading.Thread t = new System.Threading.Thread(() =>
                {


                    while (cbLines.Checked == true && cbRectangles.Checked == true)
                    {

                        System.Threading.Thread.Sleep(10); 
                        switch (rnd.Next(1, 3))
                        {
                            case 1:
                                DrawLine();
                                break;
                            case 2:
                                DrawRectangle();
                                break;
                        }
                    }
                });
                t.Start();
            }
            else
            {
                if (cbLines.Checked == true)
                {
                    System.Threading.Thread t = new System.Threading.Thread(() =>
                    {

                        while (cbLines.Checked == true)
                        {
                            System.Threading.Thread.Sleep(10);
                            DrawLine();
                        }
                    });
                    t.Start();
                }
                else if (cbOvals.Checked == true)
                {
                    System.Threading.Thread t = new System.Threading.Thread(() =>
                    {

                        while (cbOvals.Checked == true)
                        {
                            System.Threading.Thread.Sleep(10);
                            DrawOval();
                        }
                    });
                    t.Start();
                }
                else if (cbRectangles.Checked == true)
                {
                    System.Threading.Thread t = new System.Threading.Thread(() =>
                    {

                        while (cbRectangles.Checked == true)
                        {
                            System.Threading.Thread.Sleep(10);
                            DrawRectangle();
                        }
                    });
                    t.Start();
                }
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.