tags:

views:

35

answers:

1

I have a VGroup in my application that contains several custom components of type MyComponent. Now I have a delete method in MyComponent that should once ran delete the element from the VGroup. I know I should use parentDocument.vgroupId.removeElement() but what to pass as a reference?

Note: I want to do the delete within a method from MyComponent

UPDATE: here is my source: In my main application

<s:VGroup id="vgroupId" width="100%" height="100%" />

Now I add my custom component as:

 var cust:FunctionElement = new MyComponent(); // MyComponent extends spark Panel
 vgroupId.addElement(cust);

And from MyComponent I call

parentDocument.vgroupId.removeElement(this) // get this error => TypeError: Error #1034: Type Coercion failed: cannot convert global@5ed30d1 to mx.core.IVisualElement.

If I cast it as this as IVisualElement I get an error that it is equal to null

+1  A: 

This example will show how you can do it..

ExampleApp.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">     
    <fx:Script>
        <![CDATA[
            protected function button1_mouseUpHandler(event:MouseEvent):void
            {
                vgroup.addElement(new MyComponent());
            }
        ]]>
    </fx:Script>
    <s:VGroup id="vgroup" top="30" />
    <s:Button label="Add" mouseUp="button1_mouseUpHandler(event)"/>
</s:Application>

Define MyComponent.mxml like this...

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" width="100" height="35">
    <fx:Script>
        <![CDATA[
            import spark.components.VGroup;
            protected function button1_mouseUpHandler(event:MouseEvent):void
            {
                // A few useful traces to see what's what and where.
                trace(this);
                trace(this.parent);
                trace((this.parent as VGroup).getElementIndex(this));
                // But all we actually need is ...
                var vgroup:VGroup = (this.parent as VGroup);
                vgroup.removeElement(this);
                // (this.parent as VGroup).removeElement(this); // Would also work fine.
            }
        ]]>
    </fx:Script>
    <s:Button mouseUp="button1_mouseUpHandler(event)" label="Kill me!"/>
</s:Group>
slomojo
Already tried it. And get the error "ArgumentError: null is not found in this Group."
Adnan
what does parentDocument.vgroupId.getElementIndex(this as IVisualElement) return? can you post the source?
slomojo
@slomojo just updated the question with the source.
Adnan
Great, can you let me know what - parentDocument.vgroupId.getElementAt(0) - returns? - also set a breakpoint just before the removeElement and see if you can gather any more info in the debugger. Oh, also do a typeof(this) to see what it thinks 'this' is at the calling scope.
slomojo
It return null; "ArgumentError: null is not found in this Group."
Adnan
Was this for ... parentDocument.vgroupId.getElementAt(0) ? ... according to that error, there's actually nothing in your vgroup
slomojo
@Adnan ... look at the example, accessing the VGroup via parent works fine (and is a better way than going up to the parentDocument)
slomojo
@slomojo thanx a lot.
Adnan