How to add a matrix of OvalShapes to the window?

2.3k Views Asked by At

I want to draw 16*64 matrix, each one containing a:

Microsoft.VisualBasic.PowerPacks.OvalShape.

I used this:

List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = 
    new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();

for (int i = 0; i < 16; i++)
{
    for (int j = 0; j < 64; j++)
    {
        OvalShape ovl = new OvalShape();
        ovl.Width = 20;
        ovl.Height = 20;
        ovl.FillStyle = FillStyle.Solid;
        ovl.FillColor = Color.Green;

        ovalShape.Add(ovl);
    }
}

How can I show it in the Window?

3

There are 3 best solutions below

3
On

From MSDN:

An OvalShape control cannot be displayed directly on a form or container control; it must be contained in a ShapeContainer object. After you initialize an OvalShape, you will have to set its Parent property either to an existing ShapeContainer or to a new instance of ShapeContainer.

Try to set Location and add your controls to the Form:

        List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();

        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < 64; j++)
            {
                OvalShape ovl = new OvalShape();
                ovl.Width = 20;
                ovl.Height = 20;
                ovl.FillStyle = FillStyle.Solid;
                ovl.FillColor = Color.Green;

                ovl.Location = new Point(ovl.Width*i, ovl.Height*j);

                ovalShape.Add(ovl);
            }
        }

        foreach(OvalShape os in ovalShape)
        {
              Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
              Control c = new Control();
              shapeContainer.Parent = c;
              os.Parent = shapeContainer;
              myForm.Controls.Add(c);              
        }
0
On

First simplify

int totalCount = 1024; //16*64
const int shapeWidth  =20;
const int shapeHeight = 20;

for (int j = 0; j < totalCount; j++)
{
   OvalShape ovl = new OvalShape();
   ovl.Width = shapeWidth;
   ovl.Height = shapeHeight;
   ovl.FillStyle = FillStyle.Solid;
   ovl.FillColor = Color.Green;

   ovalShape.Add(ovl);
}

After pickup your drawing area and decide how much shapes per row you would like to have. So some hypothetical code could look like this:

int shapesPerRowCount = 5;
int yPos = 0;

for(int i=0;i<ovalShape.Count;i++)
{
  if(i % shapesPerRowCount  == 0) //reach end of the row, so offset Y position by Height
     yPos += shapeHeight; 

  int xPos = i*shapeWidth;
  DrawShapeAtPos(ovalShape[i], xPos, yPos); //some special function that draws the shape
}

A very generic code, but just to provide you an idea.

If it's not what you're searching for, please clarify.

0
On

Creating a separate container for each shape is not required. Also you can skip the additional container list for the shapes. So you could use this very compact code.

        var ovalShapes = new Microsoft.VisualBasic.PowerPacks.ShapeContainer()
        {
            Dock = DockStyle.Fill,
            Margin = new Padding(0),
            Padding = new Padding(0),
        };
        for (int i = 0; i < 16; i++)
            for (int j = 0; j < 64; j++)
                ovalShapes.Shapes.Add(
                    new Microsoft.VisualBasic.PowerPacks.OvalShape()
                    {
                        Width = 20,
                        Height = 20,
                        FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid,
                        FillColor = Color.Green,
                        Location = new Point(20 * i, 20 * j),
                    });
        this.Controls.Add(ovalShapes);