views:

354

answers:

4

I'm not sure if this is possible, is there a way to get the <mx:Form> to set its own height based on the position of an element within it?

For example something like this:

<mx:Form height="{submitBtn.y + submitBtn.height}">

   <mx:FormItem>... </mx:FormItem>

   <mx:FormItem>... </mx:FormItem>

   <mx:FormItem>... </mx:FormItem>

   <mx:FormItem>
       <s:Button id="submitBtn" label="Submit" /> 
   </mx:FormItem>

</mx:Form>

where the form height is dynamically set based on submitBtn's y position and its height.

I tried using Alert.show to show these values and turned out submitBtn.y = 0 and submitBtn.height = 21. height sounds reasonable, but I know the submitBtn.y can't be 0 since it's below several other

Is it possible to set the form height dynamically?

+2  A: 

Yes. Do it in the measure method:

private var n:Number = 0;
override protected function measure() : void {
super.measure();
    if (submitBtn) {
        var fi:FormItem = FormItem(submitBtn.parent);
        n = fi.y + fi.height;
    }
}

Then just set the height to n.

Edit: I changed this to specify the containing FormItem, not the button, which is what I tested in my own sample code first. You can either get the button's parent or give that FormItem an ID. But this way does work.

Robusto
Hmmm, I tried it and the total turned out to still be 21, meaning that it's counting submitBtn.y to be 0. Not sure what's wrong. This form is inside its own component, do you think that makes any difference? But I'm adding the measure code into the component itself of course.
Daryl
Yeah, actually you should test the FormItem, which is a child of the Form. Sorry, that is what I wrote and tested first, and then "adapted" to your code.
Robusto
tried the parent now, and does give me the right numbers now, thank you.
Daryl
A: 

your submitBtn.y will always return 0, since it's the y position inside the mx:FormItem element (like the relative y position)

So as I guess you want to set the y position of a form based of the y position of a button inside the form. why do you want that ?

Adrian Pirvulescu
No, I want to set the `height` of the form not its y position to the y position of an element. Basically, I'm making the form as tall as it needs to fit its last element. If I can get the y position of that last element (the submitBtn), then add its height + some extra space, the form height would just be as long as needed.
Daryl
ok.. i think i understand now what you want to achive.So if <<no height>> is specified for the form then it will take the needed height to show all content inside it (and this is actually what u are looking for, remove the height for the form). In your example it has the height of the button which is 21 and the Y position of the button which is perfectly set to 0 (since is a relative position to the parent of the button).
Adrian Pirvulescu
A: 

check out the localToGlobal() method available to all children of DisplayObject. It will convert your x,y coordinates to the global x,y. You should be able to use this in your calculations.

invertedSpear
A: 

I hope that thread is not closed cause I just have had the same problem like you and found the correct solution, so I will try to explain.

Whenever my application starts it reads a file and parse it, containing some information that will fit some forms, and I create them dynamically without specificating any height or width.

I add them to a viewStack like this:

<mx:ViewStack id="viewStack" resizeToContent="true">
</mx:ViewStack>

that is inside a component to show Information for some items, depending the class of the item one of the form is shown fitting the necessary information in.

As you can notice, the viewstack has inside a property resizeToContent that when I select an item from a datagrid, the viewstack changes the selectedIndex (indicating another form) and automatically changing its size resizing to the new content.

If for some reason you need the height of the form, you can use, instead of measure(), the measuredHeight property.

I will add some of the code used for you to understand:

onCreationComplete(){
    ...
    for each (var form:Form in DataModel.getForms())
    {
        viewStack.addChild(form);
    }
    ...
}
RamonBoza