How can I set "maxChars" of the TextInput in an editable ComboBox? - actionscript-3

I want to set the maxChars property of the TextInput of an editable ComboBox. I am currently trimming the text to a set number of characters using a change Event:
private function nameOptionSelector_changeHandler(event:ListEvent):void
{
nameOptionSelector.text = nameOptionSelector.text.substr(0, MAX_LENGTH);
}
This feels like overkill. There's got to be a better way to do this....

My alternative is to use the protected textInput directly. This approach allows the "maxChars" property to be set in the GUI builder or code, just as you would for a normal TextField. Note that zero is a valid value for maxChars and indicates unlimited characters. The override of .childrenCreated() is needed to avoid attempting to set maxChars before the TextInput object exists.
package my.controls
{
import mx.controls.ComboBox;
public class EditableComboBox extends ComboBox
{
public function EditableComboBox()
{
super();
}
private var _maxChars:int = 0;
override protected function childrenCreated():void
{
super.childrenCreated();
// Now set the maxChars property on the textInput field.
textInput.maxChars = _maxChars;
}
public function set maxChars(value:int):void
{
_maxChars = value;
if (textInput != null && value >= 0)
textInput.maxChars = value;
}
public function get maxChars():int
{
return textInput.maxChars;
}
}
}

You could extend ComboBox and override the default maxChars value for the internal TextInput. If you need to set it dynamically, you could add a public method to set the property on the extended class.

Using the suggestion of Stiggler, here is the full solution I implemented:
package
{
import mx.controls.ComboBox;
public class ComboBoxWithMaxChars extends ComboBox
{
public function ComboBoxWithMaxChars()
{
super();
}
private var _maxCharsForTextInput:int;
public function set maxCharsForTextInput(value:int):void
{
_maxCharsForTextInput = value;
if (super.textInput != null && _maxCharsForTextInput > 0)
super.textInput.maxChars = _maxCharsForTextInput;
}
public function get maxCharsForTextInput():int
{
return _maxCharsForTextInput;
}
override public function itemToLabel(item:Object):String
{
var label:String = super.itemToLabel(item);
if (_maxCharsForTextInput > 0)
label = label.substr(0, _maxCharsForTextInput);
return label;
}
}
}

Related

Flex - Problems in accessing static variable on another mxml page

First.mxml - Contains a Datefield control as follows:
<mx:DateField id="G2_CRTLoadDate" width="150" selectedDate="{modelProxy.G2_CRTLoadDate}" change="{modelProxy.G2_CRTLoadDate = event.currentTarget.selectedDate;changeManagerStatus()}"/>
I'm assigning this Datefield value to a static variable CERT_LOAD_DATE as follows(First.mxml):
[Bindable]
public static var CERT_LOAD_DATE:String = "";
private function changeManagerStatus():void
{
CERT_LOAD_DATE = G2_CRTLoadDate.selectedDate.toDateString();
}
Second.mxml -Here, I have a Combobox as follows:
<mx:ComboBox id="General_Release_Dates"
selectedItem="{modelProxy.General_Release_Dates}"
valueCommit="{model.General_Release_Dates = event.currentTarget.selectedItem;updateReleaseDate(event)}"
change="{model.General_Release_Dates = event.currentTarget.selectedItem;updateReleaseDate(event)}" close="closeHandler(event);" includeInLayout="true" visible="true">
</mx:ComboBox>
Inside the closeHandler function, I'm trying to access the variable CERT_LOAD_DATE as follows:
private function closeHandler(evt:DropdownEvent):void {
var CurrentDate:Date = new Date();
if(General_Release_Dates.selectedLabel.toString() == "TBD")
{
Alert.show(First.CERT_LOAD_DATE);
}
}
The alert box displays no value (null). Please help.
I can't figure out the relationship between First.mxml and Second.mxml from your question.
However, the following code can't access First.mxml.
Alert.show(First.CERT_LOAD_DATE);
Because the "First" is not the same instance as loaded "First.mxml".
How about to use singleton? It's accessible from anywhere.
1st, add MySingleton.as class like this.
package foo.bar
{
public class MySingleton
{
private var _cert_load_date:String;
public function MySingleton(internally:SingletonInternal)
{
super();
if(internally == null)
{
throw new Error("Please use getInstance() method.");
}
}
public static function getInstance():MySingleton
{
return SingletonInternal.instance;
}
public function set cert_load_date(value:String):void
{
_cert_load_date = value;
}
public function get cert_load_date():String
{
return _cert_load_date;
}
}
}
import foo.bar.MySingleton;
class SingletonInternal{
public static var instance:MySingleton
= new MySingleton(new SingletonInternal());
public function SingletonInternal(){}
}
How to use
Set value at First.mxml.
public var singleton: MySingleton = MySingleton.getInstance();
private function changeManagerStatus():void
{
singleton.cert_load_date = G2_CRTLoadDate.selectedDate.toDateString();
}
Second.mxml
public var singleton: MySingleton = MySingleton.getInstance();
private function closeHandler(evt:DropdownEvent):void {
var CurrentDate:Date = new Date();
if(General_Release_Dates.selectedLabel.toString() == "TBD")
{
Alert.show(singleton.cert_load_date);
}
}
Updated: Aug 27 10:00(JST)
I think there are two way to change First.mxml's element using singleton.
1) Binding the DateField value to singleton variables, and clear the value at Secend.mxml.
2) Assign to singleton variables whole "First", and control from Second.mxml.
I'll write here the 2nd way.
If you use this way, anything is controlable from Second.mxml.
MySingleton.as
private var _first:Object;
public function set first(value:Object):void
{
_first = value;
}
public function get first():Object
{
return _first;
}
First.mxml
singleton.first = this;
Second.mxml
public function something(): void{
First(singleton.first).G2_CRTLoadDate.selectedDate = null;
// The cast is unnecessary. Following code also works.
// singleton.first.G2_CRTLoadDate.selectedDate = null;
}
Also you can execute First.mxml's public function from Second.mxml.
singleton.first.someFunctionDefinedAtFirst();

Calling methods from another class Action Script 3

I have two classes called 'main' and 'TimerCountDown'. I was try to call a single function 'reset' from 'TimerCountDown' in 'main' class.
This is my TimerCountDown class:
public class TimerCountDown extends MovieClip
{
public function TimerCountDown(t:TextField, timeType:String, timeValue:Number, es:String, _documentclass):void
{
this.documentclass = _documentclass;
this.tfTimeDisplay = t;
if (timeType == "seconds")
{
this.timeInSeconds = timeValue;
}
if (timeType == "minutes")
{
this.timeInSeconds = timeValue * 60;
}
this.tfEndDisplayString = es;
this.startTimer();
}
public function reset():void{
clockCounter.reset();
}
}
How can I create a reference in main class use the reset function in functions of main class? I can only do sth like
var myTimerObject:TimerCountDown = new TimerCountDown(timer, "seconds", 40, "0!", this);
but have no idea on calling reset function.
You can call it like this :
myTimerObject.reset();
You may keep a reference of myTimerObject in main class
public class Main {
private var _targetTimerObject:TimerCountDown;
public function set targetTimerObject(value:TimerCountDown):void {
_targetTimerObject = value;
}
public function someFunction():void {
if (_targetTimerObject) {
_targetTimerObject.reset();
}
}
}

AS3 access a text field

I have 3 movieclips and each has a textbox as a child.
I set the active one with
var myroot:MovieClip = this.root as MovieClip;
var activeText:MovieClip;
This works
function keyClicked (e:MouseEvent) {
myroot.firstname_mc.getChildAt(0).text += "hello";
}
This does not
function keyClicked (e:MouseEvent) {
activeText.getChildAt(0).text += "hello";
}
How can I get this to work dynamically?
Your whole problem is that you're trying to do things that you shouldn't do. What you should do instead is write Classes that encapsulate the desired behavior, and let them handle the details. For example:
package view {
public class Label extends MovieClip {
/* This is public so the Flash Player can
populate it, not so you can "talk" to it
from outside. This is a stage instance
*/
public var tf:TextField;
protected var _text:String;
public function get text():String {
return _text;
}
public var set text(value:String):void {
if (value != _text) {
_text = value;
tf.text = _text;
}
}
}
}
Now, in your main Document Class, you type activeText as Label, and you can then set its text like this:
activeText.text += 'hello';
And you can now reuse the new Class you wrote to make all sorts of different-looking Labels, just as long as each contains a TextField called tf.

Class extends MX DataGrid how to implement allowMultipleSelection

I try to implement footer datagrid with allowMultipleSelection properties.
But in my case, it seems not to work.
Can you help me to implement this function.
Thanks
package fr.component.dgFooter
{
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
public class FooterDataGrid extends DataGrid
{
protected var _footerAllowMultipleSelection:Boolean=false;
public function FooterDataGrid()
{
super();
if(_footerAllowMultipleSelection){
this.allowMultipleSelection=true;
}
else{
}
}
public function get footerAllowMultipleSelection():Boolean
{
return _footerAllowMultipleSelection;
}
public function set footerAllowMultipleSelection(value:Boolean):void
{
_footerAllowMultipleSelection = value;
}
protected var footer:DataGridFooter;
protected var footerHeight:int = 22;
override protected function createChildren():void
{
super.createChildren();
if (!footer)
{
footer = new DataGridFooter();
footer.styleName = this;
addChild(footer);
}
}
override protected function adjustListContent(unscaledWidth:Number = -1,
unscaledHeight:Number = -1):void
{
super.adjustListContent(unscaledWidth, unscaledHeight);
listContent.setActualSize(listContent.width, listContent.height - footerHeight);
//this deals w/ having locked columns - it's handled differently in
//the dg and the adg
footer.setActualSize(listContent.width+listContent.x, footerHeight);
footer.move(1, listContent.y + listContent.height + 1);
}
override public function invalidateDisplayList():void
{
super.invalidateDisplayList();
if (footer)
footer.invalidateDisplayList();
}
}
}
Your code only checks _footerAllowMultipleSelection and sets allowMultipleSelection in the constructor, when the value is always false.
Set the allowMultipleSelection property in the setter of footerAllowMultipleSelection. You can also change the initial value of _footerAllowMultipleSelection to true.
But why are you wrapping it in a getter and setter at all? The allowMultipleSelection property is already public, so it can be set without providing another property in your subclass that does the exact same thing.

AS3 1120 Error on instantiating

I am trying to create a basic functionality in AS3.
I have a class like this:
package tax
{
public class Calculator
{
public function Calculator()
{
}
private var grwage:Number;
private var taxes:Number;
private var superannuation:Number;
private var insurance:Number;
private var net:Number;
public function getGrwage():Number {
return grwage;
}
public function setGrwage(grwage:Number):void {
this.grwage = grwage;
}
public function getTaxes():Number {
return grwage;
}
public function setTaxes(taxes:Number):void {
this.taxes = grwage * 0.2;
}
public function getSup():Number {
return superannuation;
}
public function setSup(superannuation:Number):void {
this.superannuation = superannuation * 0.05;
}
public function getIns():Number {
return insurance;
}
public function setIns(insurance:Number):void {
this.insurance = insurance * 0.1;
}
public function getNet():Number {
return net;
}
public function setNet(net:Number):void {
this.net = grwage - taxes - superannuation - insurance;
}
}
}
Just some getters and setters.
And in my mxml file:
import tax.Calculator;
public var calculate:Calculator = new Calculator();
calculate.setGrwage(1000);
trace(calculate.getTaxes());
trace(calculate.getSup());
trace(calculate.getIns());
trace(calculate.getNet());
I get an error 1120 Access to undefinded property when I try to access calculate. Do you have any ideas?
Thanks.
I guess you're mixing up things a bit between getters and setters. If a variable depends on another, you have to write the calculation in its getter. This variable shouldn't exist as a private member and the accesses to it must always be done through the appropriate getter.
for example:
public function getNet():Number {
return getGrwage() - getTaxes() - getSup() - getIns();
}
So here, you only need one setter (setGrwage), you can remove the others ones and put the calculation in the getters.
Note that there is a more appropriate syntax for setters and getters in AS3.
class GetSet
{
private var privateProperty:String;
public function get publicAccess():String
{
return privateProperty;
}
public function set publicAccess(setValue:String):void
{
privateProperty = setValue;
}
}
There are multiple issues with your class. So lets redesign it
The main issue you had was you were not initiating the vars before you accessed them as they had a NULL value the flash player was throwing you that error.
As you can see in the constructor I set a default value of 0 so that they will never be NULL.
package tax {
public class Calculator{
private var _grwage:Number;
private var _taxes:Number;
private var _superannuation:Number;
private var _insurance:Number;
public function Calculator() {
this._grwage = 0;
this._taxes = 0;
this._superannuation = 0;
this._insurance = 0;
}
public function get grwage():Number {
return this._grwage;
}
public function set grwage(val:Number):void {
this._grwage = val;
}
public function get taxes():Number {
return this._taxes;
}
public function set taxes(val:Number):void {
this._taxes = val * 0.2;
}
public function get superannuation():Number {
return this._superannuation;
}
public function set superannuation(val:Number):void {
this._superannuation = val * 0.05;
}
public function get insurance():Number {
return this._insurance;
}
public function set insurance(val:Number):void {
this._insurance = val* 0.1;
}
public function get net():Number {
return this._grwage - this._taxes - this._superannuation - this._insurance;
}
}
}
import tax.Calculator;
public var calculate:Calculator = new Calculator();
//Now due to the fact we are using true setter/getter methods.
calculate.grwage = 1000;
trace( calculate.taxes); // should be 0 as we are defaulting to the initial value
trace( calculate.superannuation );// should be 0 as we are defaulting to the initial value
trace( calculate.insurance );// should be 0 as we are defaulting to the initial value
trace( calculate.net );// should be 1000 as we are defaulting to the initial values of everything but grwage
The answers here are all good, but they all miss the primary point:
You're trying to get your variables before you set them.
import tax.Calculator;
public var calculate:Calculator = new Calculator();
calculate.setGrwage(1000);
trace(calculate.getTaxes());
trace(calculate.getSup());
trace(calculate.getIns());
trace(calculate.getNet());
This will throw an error (due to the way you've set up your class), because you never called setSup(), setIns(), and setNet() before trying to use them.
While your class may not be written in the best way, it is syntactically sound.
Try calling each setter before your getter. I think you'll solve your issue.