1

When I add a ComboBox component into a Sprite, the height of the container is larger than it should.

Here's what I mean:

import fl.controls.ComboBox;
//add combo box inside a container sprite
var combo:ComboBox = new ComboBox();
var container:Sprite = new Sprite();
addChild(container);
container.addChild(combo);
//draw the outline of the container sprite
container.graphics.lineStyle(1,0x009900);
container.graphics.drawRect(0,0,container.width,container.height);
//I don't get this:
trace(combo.height);//outputs 22
trace(container.height);//outputs 101

Note: You will need the ComboBox component in your Library. I'm using Flash CS3 for this.

If I invalidate/redraw, like this:

combo.invalidate(InvalidationType.ALL,true);
combo.drawNow();

the height changes from 101 to 104.

Any solutions ?

UPDATE: I've overwritten the configUI method in a ComboBox subclass, but the measurements are correct all the time. Why does the container height change to 100 ?

George Profenza
  • 50,687
  • 19
  • 144
  • 218

6 Answers6

3

This is because of Adobe silly implantation of Flash components, if you look in the 2nd frame of a component inside a Flash IDEA you can see it's temporary avatar which returns the initial size.

enter image description here

to solve this you should iterate over the avatar children and normalize their size:

public static function normalizedComponent(component:Sprite):void {
    for (var i:int = 0; i < component.numChildren; i++) {
        component.getChildAt(i).height = component.height;
        component.getChildAt(i).width = component.width;
    }
}

usage:

var comboBox:ComboBox = new ComboBox();
normalizedComponent(comboBox);
normalizedComponent(comboBox.textField);
Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186
1

"if it is opened, then the height WITH the drop down box"

Hmm, I think when the list is added to the displayList below the button its actually added has a popup. So the height is supposed to remain the button height, since the sprite will never actually contain the drop-down list.

A possible reason the Container height could be wrong before it goes through any invalidation could be due to the children it contains. Maybe the combobox skin (could be a movieClip of 102px height) or a combobox sub-component that always start at 102px height or with weird height (TextField in a Button is known to be sometime wrong).

A simple solution would be to wait until a creationComplete/added event and see what is the final height, then draw the border.

  • I've looked at the component skins: the list component is 100x100. I subclassed ComboBox and in the 'overriden' configUI method, I've set the height to 22(setSize). If I draw the border in an event handler for Event.ADDED triggered by the combobox, the border still looks wrong. I added a timeout though, for 335 miliseconds and that seems to work, but it's a flakey workaround. – George Profenza Sep 30 '10 at 13:21
  • Have a look at validateNow() method http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/ComboBox.html#methodSummary – Guillaume Malartre Sep 30 '10 at 14:17
1

I don't think this a ComboBox exclusive bug.

When I add a component Button to a Sprite container I also get different results when tracing the button and the container dimensions. Actually, I receive the same 100 x 100 results.

I would discard the drop down box possibility, since the Button component doesn't have one.

I think the workaround would be the same for the 2 components objects (ComboBox and Button), but I haven't found the solution just yet. Will update when I do.

UPDATE:

I was just able to get this working using validateNow(), and a few minutes later - I found the following link: http://forums.adobe.com/message/816912?tstart=0

Essentially, the link instructs us to put the validateNow() call inside an EnterFrame event, or inside a SetTimeout with proper timing.

Troy Alford
  • 26,660
  • 10
  • 64
  • 82
  • One thing that worked for me is a dirty little hack: I used a Timer/setTimeout and after about 650 ms I start using the measurement, which are correct. I tried to decrease that number, but it gets unstable...also the amount of code/graphics that need to be initialized in that area also affect the time needed for the redraw. Again, this is a dirty hack. I wish there was a clean solution I could mark/vote/etc. – George Profenza Jan 29 '11 at 20:15
  • You answered while I was editing the post. But, thanks man ! As I told before in the end the validateNow() by itself did the job. ;-) Saturday night coders ? – Luciano Santana Jan 29 '11 at 20:26
0

So - I guess the displayed height of ComboBox is the actual height - id est, if it is opened, then the height WITH the drop down box, if not then WITHOUT. However - the items are STILL there, although with .visible set to false, which still expands the container, even if you can't see that... Therefore - I would say to do:

container.graphics.drawRect(0, 0, combo.width, combo.height);

That is the way to usually do it as well...

Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34
  • Thanks for the input. I'm trying to put together a simple GUI. I want to use sprites to align components inside them(either vertically or horizontally). Say 'container' in my example would contained horizontally aligned components. If I were to add another container bellow it, I would set container2.y = container1.y+container.height,right ? Since container is giving me incorrect measurements, this aligning containers vertically would fail. About the dropdown List, not so sure you're right. If I set the rowCount to 2, that doesn't change anything, and by default(closed) it's not added – George Profenza Sep 06 '10 at 15:36
  • Still - this is a kind of bug... Maybe the .visible=false drop down is by default (before initialization) set to some value, and it is changed to the right size after... Not sure though - bugs, glitches - those annoying things are very hard to "reverse engineer" or even remotely understand if you don't have the source and/or can not debug it properly. – Aurel Bílý Sep 06 '10 at 18:39
  • It looks like a bug. The source is available(e.g. /Applications/Adobe\ Flash\ CS3/Configuration/Component\ Source/ActionScript\ 3.0/User\ Interface). A fix would be amazing, but all I'm expecting really is a workaround. – George Profenza Sep 06 '10 at 18:43
  • and what about **Flash CS5**? – Eugene Sep 06 '10 at 20:21
  • @Eugene Just tried that :( No luck. I've checked the source for the components and it looks like it hasn't changed much since Flash CS3. I'll need to dive deep and see where flash components measure the children sprites/components. – George Profenza Sep 07 '10 at 10:07
0

can you setup manually before this code:

container.width=100;

container.height=100;

container.graphics.drawRect(0,0,container.width,container.height);

Eugene
  • 2,226
  • 1
  • 14
  • 15
  • not really :( If I set the height after the combo was added, the combo gets scaled down. If I set the height, before, while there is nothing added to container, it goes to 0 after I add the combo – George Profenza Sep 07 '10 at 15:07
  • explain please, what is your real goal? – Eugene Sep 07 '10 at 15:22
  • 1
    I want to whip out a simple GUI with some comboboxes, text inputs, etc. I started adding components to the stage, but had problems with that, so I decided I to draw the GUI from code. I'm just nesting Sprites and trying to align them to get an HBox/VBox type of behavior. At the moment am having issues with the measures because of the ComboBox. – George Profenza Sep 13 '10 at 11:28
  • what if I'll advice you are flex with mxml, i think it will more powerful to cover your goals. Could you explain which component/GUI you are trying to build? – Eugene Sep 13 '10 at 17:33
0

Hi i found somewhere a solution for a similar problem with NumericStepper.

and the solution was:

var tInput:TextInput = numericStepper.getChildAt(2) as TextInput;
tInput.textField.height = 22; 

In your case try the following:

var tInput:TextInput = combo.getChildAt(1) as TextInput;
tInput.textField.height = 22; 
George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • 1
    Hi @Sanjay. I'm affraid I don't fully understand the link. So you access the TextInput inside the NumbericStepper component and explicitly set the height to 22 ? I'll give that a go with my issue and see how it goes. – George Profenza Nov 11 '10 at 10:05