Which solution is best

T

Tony Johansson

Hello!

The asp.net group doesn't seem to know this so I try here again.

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 as many of you have said. 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 then 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
 
J

Jeff Johnson

The asp.net group doesn't seem to know this so I try here again.

This looks like a WinForms app. Why in the world would you post the question
in an ASP.NET group??
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 as many of you have said. 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 then the
teachers.
pic is a pictureBox.

I didn't dive in too deep, but it looks like your teacher's code will work
until something obscures the picture box (like another window or dragging
this window down so far that the picture box goes off the bottom of the
screen). After that, the picture box will display white or whatever its
background color is.

Your solution, on the other hand, should keep the resulting image in the
picture box forever. And I really mean forever, since I didn't see anywhere
that the show variable ever gets set back to false....

Just direct your teacher to Bob Powell's site and tell him never to use
CreateGraphics() again.
 
T

Tony Johansson

Jeff Johnson said:
This looks like a WinForms app. Why in the world would you post the
question in an ASP.NET group??


I didn't dive in too deep, but it looks like your teacher's code will work
until something obscures the picture box (like another window or dragging
this window down so far that the picture box goes off the bottom of the
screen). After that, the picture box will display white or whatever its
background color is.

Your solution, on the other hand, should keep the resulting image in the
picture box forever. And I really mean forever, since I didn't see
anywhere that the show variable ever gets set back to false....

Just direct your teacher to Bob Powell's site and tell him never to use
CreateGraphics() again.

My solution will redraw when the user click the button and so will the
techers solution..

//Tony
 
J

Jeff Johnson

My solution will redraw when the user click the button and so will the
techers solution..

Yes, but your solution will ALSO redraw ANY TIME the picture box needs to
repaint itself. The teacher's solution will not.
 
T

Tony Johansson

Jeff Johnson said:
Yes, but your solution will ALSO redraw ANY TIME the picture box needs to
repaint itself. The teacher's solution will not.

I mean if someone want to control the drawing by clicking on a button then
there is two main solutions either use
a solution somewhat like the techer have done it or use my solution..

In addition if you also want the drawing to be repainted if you hite the
form and then unhide it
you must use a solution that is somewhat like the one I have done.

//Tony
 
J

Jeff Johnson

I mean if someone want to control the drawing by clicking on a button then
there is two main solutions either use
a solution somewhat like the techer have done it or use my solution..

In addition if you also want the drawing to be repainted if you hite the
form and then unhide it
you must use a solution that is somewhat like the one I have done.

This thread amuses me. You asked whose solution is better. I told you it was
yours because yours is more thorough. Yet you continue to talk about how
your teacher's solution will work. Yes, it will, but it's doing the absolute
minimum and ultimately is an example of bad practice. So I don't understand
why we continue to discuss it!

Which is what, as I said, amuses me....
 
T

Tony Johansson

Jeff Johnson said:
This thread amuses me. You asked whose solution is better. I told you it
was yours because yours is more thorough. Yet you continue to talk about
how your teacher's solution will work. Yes, it will, but it's doing the
absolute minimum and ultimately is an example of bad practice. So I don't
understand why we continue to discuss it!

Which is what, as I said, amuses me....
No I didn't ask whose solution was the best because you had already
explained that I just told you what the alternatives were

//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

Top