Which solution of these two is best

Discussion in 'ASP .Net' started by Tony Johansson, Dec 7, 2010.

  1. Hello!

    This example solution 1 is from a teacher but as you can see he use drawing
    in the button event handler which is not a recommended way to handle
    drawing. I would instead use solution 2 which is my solution listed below
    solution 1

    So I hope someone can confirm that my solution is better thwen the teachers.
    pic is a pictureBox.

    //Start Solution 1
    public partial class Form1 : Form
    {
    private Single[,] sold = new Single[2, 4];

    public Form1()
    {
    InitializeComponent();
    }

    private void btnDraw_Click(object sender, EventArgs e)
    {
    Graphics gr = pic.CreateGraphics();

    Brush myBrush1 = new SolidBrush(Color.Coral);
    Brush myBrush2 = new SolidBrush(Color.LightGreen);

    //Objects for drawing string
    Brush myBrush3 = new SolidBrush(Color.Black);
    Font myFont = new Font("Times New Roman", 8, FontStyle.Regular);

    //Clear drawing area
    gr.Clear(this.BackColor);

    //Populate the two-dimensional array
    Populate();
    Single max = sold.Cast<Single>().Max();

    //Declar and define the vertical and horizontal length units
    Single scaleV = (pic.Height - 50) / max;

    int unitH = ((int)pic.Width) / 13;
    Pen myPen = new Pen(Color.White);

    ////draw chart
    gr.FillRectangle(myBrush1,unitH, pic.Height - scaleV * sold[0,
    0] - 30, unitH, (scaleV * sold[0, 0]));
    gr.DrawRectangle(myPen, unitH, pic.Height - scaleV * sold[0,
    0] - 30, unitH, (scaleV * sold[0, 0]));
    gr.DrawString(sold[0, 0].ToString(), myFont, myBrush3, unitH,
    pic.Height - scaleV * sold[0, 0] - 42);

    gr.FillRectangle(myBrush2, 2 * unitH, pic.Height - scaleV *
    sold[1, 0] - 30, unitH, (scaleV * sold[1, 0]));
    gr.DrawRectangle(myPen, 2 * unitH, pic.Height - scaleV * sold[1,
    0] - 30, unitH, (scaleV * sold[1, 0]));
    gr.DrawString(sold[1, 0].ToString(), myFont, myBrush3, 2 *
    unitH, pic.Height - scaleV * sold[1, 0] - 42);

    gr.FillRectangle(myBrush1, 4 * unitH, pic.Height - scaleV *
    sold[0, 1] - 30, unitH, (scaleV * sold[0, 1]));
    gr.DrawRectangle(myPen, 4 * unitH, pic.Height - scaleV * sold[0,
    1] - 30, unitH, (scaleV * sold[0, 1]));
    gr.DrawString(sold[0, 1].ToString(), myFont, myBrush3, 4 *
    unitH, pic.Height - scaleV * sold[0, 1] - 42);

    gr.FillRectangle(myBrush2, 5 * unitH, pic.Height - scaleV *
    sold[1, 1] - 30, unitH, (scaleV * sold[1, 1]));
    gr.DrawRectangle(myPen, 5 * unitH, pic.Height - scaleV * sold[1,
    1] - 30, unitH, (scaleV * sold[1, 1]));
    gr.DrawString(sold[1, 1].ToString(), myFont, myBrush3, 5 *
    unitH, pic.Height - scaleV * sold[1, 1] - 42);

    gr.FillRectangle(myBrush1, 7 * unitH, pic.Height - scaleV *
    sold[0, 2] - 30, unitH, (scaleV * sold[0, 2]));
    gr.DrawRectangle(myPen, 7 * unitH, pic.Height - scaleV * sold[0,
    2] - 30, unitH, (scaleV * sold[0, 2]));
    gr.DrawString(sold[0, 2].ToString(), myFont, myBrush3, 7 *
    unitH, pic.Height - scaleV * sold[0, 2] - 42);

    gr.FillRectangle(myBrush2, 8 * unitH, pic.Height - scaleV *
    sold[1, 2] - 30, unitH, (scaleV * sold[1, 2]));
    gr.DrawRectangle(myPen, 8 * unitH, pic.Height - scaleV * sold[1,
    2] - 30, unitH, (scaleV * sold[1, 2]));
    gr.DrawString(sold[1, 2].ToString(), myFont, myBrush3, 8 *
    unitH, pic.Height - scaleV * sold[1, 2] - 42);

    gr.FillRectangle(myBrush1, 10 * unitH, pic.Height - scaleV *
    sold[0, 3] - 30, unitH, (scaleV * sold[0, 3]));
    gr.DrawRectangle(myPen, 10 * unitH, pic.Height - scaleV *
    sold[0, 3] - 30, unitH, (scaleV * sold[0, 3]));
    gr.DrawString(sold[0, 3].ToString(), myFont, myBrush3, 10 *
    unitH, pic.Height - scaleV * sold[0, 3] - 42);

    gr.FillRectangle(myBrush2, 11 * unitH, pic.Height - scaleV *
    sold[1, 3] - 30, unitH, (scaleV * sold[1, 3]));
    gr.DrawRectangle(myPen, 11 * unitH, pic.Height - scaleV *
    sold[1, 3] - 30, unitH, (scaleV * sold[1, 3]));
    gr.DrawString(sold[1, 3].ToString(), myFont, myBrush3, 11 *
    unitH, pic.Height - scaleV * sold[1, 3] - 42);

    ////Drae axes
    gr.DrawLine(new Pen(Color.Black,2), 5, 157, 270, 157);
    gr.DrawLine(new Pen(Color.Black,2), 5, 155, 5, 5);
    }
    private void Populate()
    {
    sold[0, 0] = Single.Parse(txtA1.Text);
    sold[0, 1] = Single.Parse(txtA2.Text);
    sold[0, 2] = Single.Parse(txtA3.Text);
    sold[0, 3] = Single.Parse(txtA4.Text);
    sold[1, 0] = Single.Parse(txtB1.Text);
    sold[1, 1] = Single.Parse(txtB2.Text);
    sold[1, 2] = Single.Parse(txtB3.Text);
    sold[1, 3] = Single.Parse(txtB4.Text);
    }
    }
    //End solution 1

    //Start solution 2
    public partial class Form1 : Form
    {
    private Single[,] sold = new Single[2, 4];
    private bool show = false;

    public Form1()
    {
    InitializeComponent();
    }

    private void btnDraw_Click(object sender, EventArgs e)
    {
    show = true;
    pic.Invalidate();
    }

    private void Populate()
    {
    sold[0, 0] = Single.Parse(txtA1.Text);
    sold[0, 1] = Single.Parse(txtA2.Text);
    sold[0, 2] = Single.Parse(txtA3.Text);
    sold[0, 3] = Single.Parse(txtA4.Text);
    sold[1, 0] = Single.Parse(txtB1.Text);
    sold[1, 1] = Single.Parse(txtB2.Text);
    sold[1, 2] = Single.Parse(txtB3.Text);
    sold[1, 3] = Single.Parse(txtB4.Text);
    }

    private void pic_Paint(object sender, PaintEventArgs e)
    {
    if (show)
    {
    Graphics gr = e.Graphics;

    Brush myBrush1 = new SolidBrush(Color.Coral);
    Brush myBrush2 = new SolidBrush(Color.LightGreen);

    //Objects for drawing string
    Brush myBrush3 = new SolidBrush(Color.Black);
    Font myFont = new Font("Times New Roman", 8,
    FontStyle.Regular);

    //Clear drawing area
    gr.Clear(this.BackColor);

    //Populate the two-dimensional array
    Populate();
    Single max = sold.Cast<Single>().Max();

    //Declar and define the vertical and horizontal length units
    Single scaleV = (pic.Height - 50) / max;

    int unitH = ((int)pic.Width) / 13;
    Pen myPen = new Pen(Color.White);

    ////draw chart
    // Att 30 dras bort beror på pic.height - y position för x
    axeln som är 157
    gr.FillRectangle(myBrush1, unitH, pic.Height - scaleV *
    sold[0, 0] - 30, unitH, (scaleV * sold[0, 0]));
    gr.DrawRectangle(myPen, unitH, pic.Height - scaleV * sold[0,
    0] - 30, unitH, (scaleV * sold[0, 0]));
    gr.DrawString(sold[0, 0].ToString(), myFont, myBrush3, unitH,
    pic.Height - scaleV * sold[0, 0] - 42);

    gr.FillRectangle(myBrush2, 2 * unitH, pic.Height - scaleV *
    sold[1, 0] - 30, unitH, (scaleV * sold[1, 0]));
    gr.DrawRectangle(myPen, 2 * unitH, pic.Height - scaleV *
    sold[1, 0] - 30, unitH, (scaleV * sold[1, 0]));
    gr.DrawString(sold[1, 0].ToString(), myFont, myBrush3, 2 *
    unitH, pic.Height - scaleV * sold[1, 0] - 42);

    gr.FillRectangle(myBrush1, 4 * unitH, pic.Height - scaleV *
    sold[0, 1] - 30, unitH, (scaleV * sold[0, 1]));
    gr.DrawRectangle(myPen, 4 * unitH, pic.Height - scaleV *
    sold[0, 1] - 30, unitH, (scaleV * sold[0, 1]));
    gr.DrawString(sold[0, 1].ToString(), myFont, myBrush3, 4 *
    unitH, pic.Height - scaleV * sold[0, 1] - 42);

    gr.FillRectangle(myBrush2, 5 * unitH, pic.Height - scaleV *
    sold[1, 1] - 30, unitH, (scaleV * sold[1, 1]));
    gr.DrawRectangle(myPen, 5 * unitH, pic.Height - scaleV *
    sold[1, 1] - 30, unitH, (scaleV * sold[1, 1]));
    gr.DrawString(sold[1, 1].ToString(), myFont, myBrush3, 5 *
    unitH, pic.Height - scaleV * sold[1, 1] - 42);

    gr.FillRectangle(myBrush1, 7 * unitH, pic.Height - scaleV *
    sold[0, 2] - 30, unitH, (scaleV * sold[0, 2]));
    gr.DrawRectangle(myPen, 7 * unitH, pic.Height - scaleV *
    sold[0, 2] - 30, unitH, (scaleV * sold[0, 2]));
    gr.DrawString(sold[0, 2].ToString(), myFont, myBrush3, 7 *
    unitH, pic.Height - scaleV * sold[0, 2] - 42);

    gr.FillRectangle(myBrush2, 8 * unitH, pic.Height - scaleV *
    sold[1, 2] - 30, unitH, (scaleV * sold[1, 2]));
    gr.DrawRectangle(myPen, 8 * unitH, pic.Height - scaleV *
    sold[1, 2] - 30, unitH, (scaleV * sold[1, 2]));
    gr.DrawString(sold[1, 2].ToString(), myFont, myBrush3, 8 *
    unitH, pic.Height - scaleV * sold[1, 2] - 42);

    gr.FillRectangle(myBrush1, 10 * unitH, pic.Height - scaleV *
    sold[0, 3] - 30, unitH, (scaleV * sold[0, 3]));
    gr.DrawRectangle(myPen, 10 * unitH, pic.Height - scaleV *
    sold[0, 3] - 30, unitH, (scaleV * sold[0, 3]));
    gr.DrawString(sold[0, 3].ToString(), myFont, myBrush3, 10 *
    unitH, pic.Height - scaleV * sold[0, 3] - 42);

    gr.FillRectangle(myBrush2, 11 * unitH, pic.Height - scaleV *
    sold[1, 3] - 30, unitH, (scaleV * sold[1, 3]));
    gr.DrawRectangle(myPen, 11 * unitH, pic.Height - scaleV *
    sold[1, 3] - 30, unitH, (scaleV * sold[1, 3]));
    gr.DrawString(sold[1, 3].ToString(), myFont, myBrush3, 11 *
    unitH, pic.Height - scaleV * sold[1, 3] - 42);

    ////Draw axes
    gr.DrawLine(new Pen(Color.Black, 2), 5, 157, 270, 157);
    gr.DrawLine(new Pen(Color.Black, 2), 5, 155, 5, 5);
    }
    }
    }

    //End solution 2

    //Tony
     
    Tony Johansson, Dec 7, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keshawra
    Replies:
    7
    Views:
    611
    James McGill
    Feb 24, 2006
  2. Familyman
    Replies:
    3
    Views:
    528
    Alan J. Flavell
    Feb 9, 2006
  3. Ovidiu.Silaghi
    Replies:
    2
    Views:
    8,947
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 25, 2006
  4. GenxLogic
    Replies:
    3
    Views:
    1,291
    andrewmcdonagh
    Dec 6, 2006
  5. decker
    Replies:
    9
    Views:
    336
    Barry Schwarz
    Nov 10, 2007
Loading...

Share This Page