Drawing a grid on the form

W

Wade

Hey all,

I am drawing a grid on my windows form -- however, I wonder if the way I'm
doing it isn't the best, as it seems to take awhile to create the grid. Any
thoughts?

private void DrawGrid()
{
DrawGridBoundary();

int Width = FormWidth;
int Height = FormHeight - pnlControls.Height;

int NumberOfCols = Width / GridSize;
int NumberOfRows = Height / GridSize;

for (int i = TopLeft.Y; i < (BottomRight.Y - GridSize); i = i +
GridSize)
{
for (int j = TopLeft.X; j < (BottomRight.X - GridSize); j =
j + GridSize)
{
DrawGridPoint(GridSize + j - 1, GridSize + i);
}
}
}

private void DrawGridBoundary()
{
int Width = FormWidth;

Graphics gfx = this.CreateGraphics();

Pen p = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
Rectangle rect = new Rectangle(TopLeft.X, TopLeft.Y,
BottomRight.X - TopLeft.X, BottomRight.Y - TopLeft.Y);

gfx.DrawRectangle(p, rect);
}

private void DrawGridPoint(int X, int Y)
{
Graphics gfx = this.CreateGraphics();
Rectangle rect = new Rectangle(X, Y, 1, 1);
System.Drawing.Pen p = new
System.Drawing.Pen(System.Drawing.Color.Gray);

gfx.DrawRectangle(p, rect);
}

Thanks for the help!
 
W

Wade

Okay ... now to make this more difficult.

How about within the .NET Compact Framework? :)
 

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