2

I am trying to select something which is not a Control - An OvalShape.

This works to find a button

string y = "btn_down_" + x;
Button button = this.Controls.Find(y, true)[0] as Button;

How can I do the same thing to find an OvalShape for example: (this doesn't work obviously)

string y = "ovalShape_" + x;
OvalShape light = this.Controls.Find(y, true)[0] as OvalShape;

enter image description here

_____Solution_____

string z = "ovalShape" + (21-x);
OvalShape light = shapeContainer2.Shapes.OfType<OvalShape>().FirstOrDefault(ov =>    ov.Name == z);

Tom S
  • 174
  • 14
  • It all depends on what your oval shape IS? Could you please include its definition? Also, how have you added `OvalShape` into your form? – gideon Feb 02 '14 at 05:27
  • Microsoft.VisualBasic.PowerPacks.OvalShape yes its added to the form – Tom S Feb 02 '14 at 05:30
  • If its not possible I might have to just use labels and change the background of those instead. However this will involve naming a whole bunch of OvalShape.. nooo – Tom S Feb 02 '14 at 05:31

2 Answers2

1

From the docs I see that a Shape is added first on a ShapeContainer which is just a container control.

The ShapeContainer then has a property called Shapes which is of type ShapeCollection and exposes list methods like Contains.

Assuming you have set it all up like this:

 Microsoft.VisualBasic.PowerPacks.ShapeContainer canvas = 
    new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
 Microsoft.VisualBasic.PowerPacks.OvalShape oval1 = 
    new Microsoft.VisualBasic.PowerPacks.OvalShape();
 // Set the form as the parent of the ShapeContainer.
 canvas.Parent = this;

for(int i =0;i < canvas.Count; i++)
{
  var shape = (Shape)canvas.Item[i];
   //now check if shape is your oval by looking at it's properties.
}

//You could also do this:
// (But this means you have to store a reference to your shape somewhere
// as some sort of global, not very good design.)
OvalShape myShape = ..
int index = canvas.IndexOf(myShape);
canvas.Item[index];//returns your shape.

If you are going with the for loop approach, you could check for the shape's Name property. You could also use the Tag property; Set the tag to say "OvalShape" and then check for this in your loop : if(shape.Tag == "OvalShape") {...

gideon
  • 19,329
  • 11
  • 72
  • 113
  • I dragged the OvalShapes onto the form from the toolbox – Tom S Feb 02 '14 at 05:57
  • @TomS ok. My best guess is that you should have a control called `shapeContainer1`, which is accessible in your form. This variable is what I call `canvas` in my code. – gideon Feb 02 '14 at 06:04
1

Based on some links provided in @gideon's answer, you can try it this way :

Get the default ShapeContainer first, then search through shapes inside the container having type OvalShape and name equals y.

string y = "ovalShape_" + x;
var shapeContainer = this.Controls.OfType<ShapeContainer>().FirstOrDefault();
OvalShape light = shapeContainer.Shapes.OfType<OvalShape>().FirstOrDefault(o => o.Name == y);

This code worked fine for me, but name generated after dragging OvalShape on to Form is something like "ovalShape" + x instead of "ovalShape_" + x here.

har07
  • 88,338
  • 12
  • 84
  • 137