Customizable ComboBox (Flash CS3)

21. May 2008 · Thema: Flash & ActionScript 3.0

Here is an example of how to customize the font (size, style, color) for the two components List and TextField inside the standard ComboBox component:

First you have to create a TextFormat object where you can specify font, size and color parameters.

var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = "Comic Sans MS";
myTextFormat.color = 0xFF0000;
myTextFormat.size = 40;

Afterwards you create a new combobox and put it on the stage and assign the previous written TextFormat object to the textfield inside the ComboBox, like this:

var myComboBox:ComboBox = new ComboBox();
myComboBox.addItem( { label:"item1" } );
myComboBox.addItem( { label:"item2" } );
myComboBox.textField.setStyle("textFormat", myTextFormat);
//myComboBox.dropdown.setRendererStyle("textFormat", myTextFormat2);
addChild(myComboBox);

Tips:

  • The instance name of the standard list component within a ComboBox is dropdown.
  • You can create another TextFormat object and assign this one to the ComboBox.dropdown.setRendererStyle() method within the ComboBox to format the font of the dropdown list. Instead of the setStyle() method (which the TextField uses) you have to use the setRendererStyle() method.
  • If you are adding items to a combobox using ActionScript 3.0: First addItems() to the ComboBox and afterwords set the style for the ComboBox.dropdown property. When you first set the style and addItems through actionscript later, somehow the style won’t apply
  • The height and weight of the dropdown list does not auto scale when you set your font size bigger. If you, for example, want to increase the height of rows in the list, use the following code
  • myComboBox.dropdown.rowHeight = 40;
  • Related posts:

    1. Let Flash use WordPress content
      Last week I set up a fresh WordPress system for a client to function as a Content Managament System. This...
    2. Flash CS3 Datagrid with AMFPHP
      Wade Arnold wrote an interesting article about connecting flash to a MySql database with the help of AMFPHP, flash remoting....
    3. Importing Cinema4D model into Papervision3D 2.0
      Preparing a custom made object in Cinema4D for use in Papervision Flash and importing the exported .dae into flash....

    12 Comments
    1. #1 • dhan on 26.07.2008 said:
       

      Even if you create a combobox using only ActionScript, you need to drag&drop an instance of the combobox onto the stage (in your fla file).

      You can delete the combobox after you add it to the stage…. the files needed for the combo box will be in the “Component Assets” folder (in the Library).

    2. #2 • john doe on 24.10.2008 said:
       

      thank you for posting:
      myComboBox.dropdown.setRendererStyle(“textFormat”, myTextFormat2);

      i looked everywhere and couldn’t find a solution that worked and the adobe online help is really not helpful.

      I just wanted to say thanks

    3. #3 • Andrei on 04.03.2009 said:
       

      Thanks a lot for setRendererStyle tip!

      I was wondering is there any chance to set fields’ antiAliasType to “advanced”?

    4. #4 • Tyler on 19.03.2009 said:
       

      Seriously! How did you know about the setRendererStyle() function? It’s not in any of the Adobe docs even though it is a very necessary function! I wonder why they decided to change the function name from the standard used in all other objects only for that one class… yet again, Flash saves the day with its perfectly sensible language decisions and great documentation! NOT!

    5. #5 • Aw on 16.05.2009 said:
       

      Great tip, The CB.textField thing really confused me for days !

      Thanks :)

    6. #6 • Jessica on 06.06.2009 said:
       

      Thanks a lot for the tips, couldn’t find it anywhere on the Adobe documentation !

    7. #7 • Mike on 05.07.2009 said:
       

      Thank you very, very much for the nice and simple explanation!
      Especially on how to modify the text in the dropdown menu! I couldn’t find it anywhere! Great help!

    8. #8 • Dingo on 23.11.2009 said:
       

      Thanks a lot! I also looked in the help. The adobe team should add like common Q/A to each class, for example “How do I change the font size of the dropdown.

    9. #9 • Reinier on 23.12.2009 said:
       

      If you are adding items to a combobox using ActionScript 3.0: First addItems() to the ComboBox and afterwords set the style for the ComboBox.dropdown property. When you first set the style and addItems through actionscript later, somehow the style won’t apply
      Apparently the font style is reset, or the display object for the caption is replaced by a new one, and this happens asynch. If you try to apply the style after 50 millisecs, it works…
      I found similar asynchronicity problems in other components as well.

    10. #10 • AR on 02.04.2010 said:
       

      Thanks a lot for Tips.

    11. #11 • ijcxdagbr on 23.04.2010 said:
       

      XFpK3T bmduoqnmudae, [url=http://nwuhyehcspcz.com/]nwuhyehcspcz[/url], [link=http://bbvdnpfukugm.com/]bbvdnpfukugm[/link], http://vibunhrddrxz.com/

    12. #12 • Marcus on 22.07.2010 said:
       

      Thanks for your comment. Looked for ages through the adobe docs and other web pages and yours is the only one that has worked so far. :)

    Hinterlasse einen Kommentar!

    Kommentar:

    Diskussion zum Beitrag als RSS 2.0 Feed abonnieren.