Which solution of these two is best

T

Tony Johansson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top