Reputation: 15
I want to draw a 2 dimensional boolean array on a winform project. I want to put a filled rectangle where f[i,j]==1 and leave blank where f[i,j]==0.
My first question is, i'm using winform and i don't have much idea about which component can i do that on.I'm currently trying to do it on a panel but so far, i am not very successfull.
And i would also appreciate, if someone could give me a not complex sample of drawing a 2d array on a winform component, so i can understand it better, thanks
Upvotes: 1
Views: 3983
Reputation: 12993
Try this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Stackoverflow
{
public partial class Print2DArrayForm : Form
{
public Print2DArrayForm()
{
InitializeComponent();
//initial the matrix here
matrix[0, 1] = true;
matrix[1, 2] = true;
matrix[1, 1] = true;
matrix[1, 3] = true;
matrix[2, 2] = true;
this.Paint += Draw2DArray;
}
bool[,] matrix = new bool[3, 4];
private void Draw2DArray(object sender, PaintEventArgs e)
{
int step = 50; //distance between the rows and columns
int width = 40; //the width of the rectangle
int height = 40; //the height of the rectangle
using (Graphics g = this.CreateGraphics())
{
g.Clear(SystemColors.Control); //Clear the draw area
using (Pen pen = new Pen(Color.Red, 2))
{
int rows = matrix.GetUpperBound(0) + 1 - matrix.GetLowerBound(0); // = 3, this value is not used
int columns = matrix.GetUpperBound(1) + 1 - matrix.GetLowerBound(1); // = 4
for (int index = 0; index < matrix.Length; index++)
{
int i = index / columns;
int j = index % columns;
if (matrix[i, j])
{
Rectangle rect = new Rectangle(new Point(5 + step * j, 5 + step * i), new Size(width, height));
g.DrawRectangle(pen, rect);
g.FillRectangle(System.Drawing.Brushes.Red, rect);
}
}
}
}
}
}
}
Upvotes: 1