.net - How to reference a dynamically created OvalShape with a string variable in vb.net -
i have created 40 or ovalshapes ms power packs , when user clicks them send id separate function supposed change color of clicked oval. unfortunately controls
method seems not work.
controls.item(dummy).fillcolor = color.red
gives me error saying "fillcolor not member of 'system.windows.forms.control'" dummy string containing name of control.
i'm new vb.net i'm not sure if there's way reference things on form via string besides using controls
. google hasn't helped on matter much, found way search through shapes using ctype
on controls match oval type isn't helpful when want change 1 control...
edit: looking able following:
for = 1 40 ovalname = "oval" & if ovali = next
i not sure how adding ovalshapes or type of container using. in order add them windows form control need use shapecontainer mentioned slaks. in example creating shapecontainer , adding form, using shapecontainers.shapes.add method add oval shapecollection class. attaching eventhandler click event of ovals can access calling shape change fill color through sender object of eventhandler. see if work you.
imports microsoft.visualbasic.powerpacks public class form1 dim offset integer = 0 dim ovalcontainer new shapecontainer public sub new() ' call required designer. initializecomponent() ovalcontainer.size = new size(me.width, 50) me.controls.add(ovalcontainer) ovalcontainer.location = new point(0, 0) end sub private sub button1_click(sender object, e eventargs) handles button1.click dim oval new ovalshape() oval.size = new size(30, 40) oval.location = new point(offset, 0) oval.fillstyle = fillstyle.solid oval.fillcolor = color.transparent oval.bordercolor = color.black oval.borderwidth = 2 addhandler oval.click, addressof shapeclick ovalcontainer.shapes.add(oval) offset += 40 end sub private sub shapeclick(sender object, e eventargs) dim oval ovalshape = directcast(sender, ovalshape) if oval.fillcolor.equals(color.red) oval.fillcolor = color.blue else oval.fillcolor = color.red end if end sub end class
edit per op's clarification
when create ovals add oval.name = "oval" & index
add name property enable following code work.
you can iterate through shapes collection this(this based off of above example):
for each o ovalshape in ovalcontainer.shapes if o.name = "oval1" o.fillcolor = color.azure next
or can search exact oval looking using shapecontainer.shapes.indexofkey
method
private sub button2_click(sender object, e eventargs) handles button2.click dim index integer = ovalcontainer.shapes.indexofkey("oval1") if index >= 0 directcast(ovalcontainer.shapes(index), ovalshape).fillcolor = color.purple end if end sub
Comments
Post a Comment