Hey Guys
I am coding a grid in c#. I have posted the code below. I get the grid to print, but I want to allow the user to change the Grid Size by using a checkbox. I have tried writing some code to achieve this but I am not having any success.
Could anyone help me and suggest what I need to add to the code to achieve what I am after. Or if there is a better way of doing it then I would be interested in knowing.
Here is the code that paints the grid.
private int SQsize = 25;
private int gridSize = 250;
/// <summary>
/// Event fired on paint of the grid
/// Draws the grid lines based on the selected sizes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (gridLines == 1)
{
Graphics grid = e.Graphics;
Pen myPen = new Pen(Color.Black, 1); //create a pen object
for (int a = 0; a <= this.gridSize; a += this.SQsize)
{
grid.DrawLine(myPen, 0, a, gridSize, a); //use the DrawLine Horizontal
grid.DrawLine(myPen, a, 0, a, gridSize); //use the DrawLine Vertical
}
myPen.Dispose();
}
Graphics paint = e.Graphics;
SolidBrush myBrush = new SolidBrush(colorDialog1.Color);
if (gridSize == 500 && SQsize == 10)
{
this.maxCol = 50;
this.maxRow = 50;
toolStripStatusLabel1.Text = "Grid Size: Large 50x50";
}
if (gridSize == 500 && SQsize == 25)
{
this.maxCol = 20;
this.maxRow = 20;
toolStripStatusLabel1.Text = "Grid Size: Large 20x20";
}
if (gridSize == 500 && SQsize == 50)
{
this.maxCol = 10;
this.maxRow = 10;
toolStripStatusLabel1.Text = "Grid Size: Large 10x10";
}
if (gridSize == 250 && SQsize == 10)
{
this.maxCol = 25;
this.maxRow = 25;
toolStripStatusLabel1.Text = "Grid Size: Medium 25x25";
}
if (gridSize == 250 && SQsize == 25)
{
this.maxCol = 10;
this.maxRow = 10;
toolStripStatusLabel1.Text = "Grid Size: Medium 10x10";
}
if (gridSize == 250 && SQsize == 50)
{
this.maxCol = 5;
this.maxRow = 5;
toolStripStatusLabel1.Text = "Grid Size: Medium 5x5";
}
if (gridSize == 200 && SQsize == 10)
{
this.maxCol = 20;
this.maxRow = 20;
toolStripStatusLabel1.Text = "Grid Size: Small 20x20";
}
if (gridSize == 200 && SQsize == 25)
{
this.maxCol = 8;
this.maxRow = 8;
toolStripStatusLabel1.Text = "Grid Size: Small 8x8";
}
if (gridSize == 200 && SQsize == 50)
{
this.maxCol = 4;
this.maxRow = 4;
toolStripStatusLabel1.Text = "Grid Size: Small 4x4";
}
int cellRow = 0;
// iterates over each row in teh array...
while (cellRow < maxRow)
{
int cellCol = 0;
/// each column in the row
while (cellCol < maxCol)
{
// if the array has a value of '1' in this position then it needs to be painted (the value is set to '1' when
// the user clicks on it - handled my the MouseDown event below)
if (shape == 1)
{
if (paintSQ[cellCol, cellRow] == 1)
{
paint.FillRectangle(myBrush, (cellCol * this.SQsize) + 1, (cellRow * this.SQsize) + 1, this.SQsize - 1, this.SQsize - 1);
}
}
if (shape == 0)
{
if (paintSQ[cellCol, cellRow] == 1)
{
paint.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
paint.FillEllipse(myBrush, new Rectangle((cellCol * this.SQsize), (cellRow * this.SQsize), this.SQsize, this.SQsize));
}
}
cellCol++;
}
cellRow++;
}
myBrush.Dispose();
}
Here is the code I want to use to change the size of the grid when the check box is selected. It builds and runs, and when i select the checkbox the label displays Grid Size: Large 50x50. But the grid itself doesnt change. I know I need to add some more code to do this, but i have had a play around and can't get it to work. My lack of understanding isn't helping me much.
//I want to use a check box to resize the grid, each checkbo will represent a static grid size
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//here i have set the variables for gridSize and SQsize. This would paint a grid size 50x50
//I need to add more code to call the grid paint function
int gridSize = 500;
int SQsize = 10;
if (gridSize == 500 && SQsize == 10)
{
this.maxCol = 50;
this.maxRow = 50;
toolStripStatusLabel1.Text = "Grid Size: Large 50x50";
}
}
Thanks to anyone who takes the time to read this and give me help with feedback. I really appreciate the help.
regards