System.Windows.Forms.ControlPaint.DrawGrid equivalent?

  • Thread starter Thread starter Wade
  • Start date Start date
W

Wade

Hi all,

Is there an equivalent to the System.Windows.Forms.ControlPoint.DrawGrid in
the .NET 2.0 Compact Framework? I'd like to find a way to easily draw a
grid on my form.

Thanks,

Wade
 
The ControlPaint class does not exist in the CF. Your best bet is to draw a
sample of the grid onto a Bitmap and then use a TextureBrush to splash this
image over and over again to get the grid look. You can add the following
method to your form class definition and then tweak it as you see fit.

protected override void OnPaintBackground(PaintEventArgs e)
{
using (Bitmap sample = new Bitmap(16, 16))
{
using (Graphics prep = Graphics.FromImage(sample))
{
prep.Clear(this.BackColor);
}
sample.SetPixel(8, 8, Color.Black);
using (TextureBrush brush = new TextureBrush(sample))
{
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
}
}

And, of course, you'll get even better performance if you create the
TextureBrush only when necessary. In other words, you may also want to cache
the TextureBrush so that you don't need to create it everytime the method is
called.
 
I believe that the poster is asking about drawing an actual array of "dots",
similar to the grid that you may see on the form at design-time within the
forms designer. Are you wondering about the DataGrid?
 
Awesome, thanks Tim.

Tim Wilson said:
The ControlPaint class does not exist in the CF. Your best bet is to draw
a
sample of the grid onto a Bitmap and then use a TextureBrush to splash
this
image over and over again to get the grid look. You can add the following
method to your form class definition and then tweak it as you see fit.

protected override void OnPaintBackground(PaintEventArgs e)
{
using (Bitmap sample = new Bitmap(16, 16))
{
using (Graphics prep = Graphics.FromImage(sample))
{
prep.Clear(this.BackColor);
}
sample.SetPixel(8, 8, Color.Black);
using (TextureBrush brush = new TextureBrush(sample))
{
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
}
}

And, of course, you'll get even better performance if you create the
TextureBrush only when necessary. In other words, you may also want to
cache
the TextureBrush so that you don't need to create it everytime the method
is
called.
 
Back
Top