This is a new request from my friend Pierre. He wants to implement a POC to demonstrate how to control UI objects on the stage. That’s a very good sample for your reference if you want to implement some layout application.
If you want to add a resize functionality to the object, you may refer to my previous post Image Manipulation - Scale.
Comparison
Flash implementation: 75 minutes(Implemented First)
Silverlight implementation: 55 minutes
What’s the difference:
- Creating random color
- Limit the range of Text Input
Source codes
Controlling UI Object [Flash 9, AS3] (13.6 KiB, 450 hits)
Controlling UI Object [Silverlight 2, C#] (12 KiB, 850 hits)
Flash
Silverlight
Creating random color
Creating random color is very easy in AS3 if you know the way. Just one line!
// AS3 var color:uint = uint(0xFFFFFF * Math.random());
For C#, here is one of the approaches. Does anyone have a better solution?
// C# Random r = new Random(); byte red = (byte)(255 * r.NextDouble()); byte green = (byte)(255 * r.NextDouble()); byte blue = (byte)(255 * r.NextDouble()); Color color = Color.FromArgb(255, red, green, blue);
Limiting the range of Text Input
Sometime, you may want to limit the range of numeric values entered by the user. Here is my solution.
// AS3
// limit the input to numeric value
_textField.restrict = "01234567890";
_textField.addEventListener(Event.CHANGE, on_change);
private function on_x_change(e : Event):void{
var value:Number = parseInt(_textField.text);
value = Math.max(MIN_VALUE, Math.min(MAX_VALUE, value));
// update the text field value again
_xText.text = value.toString();
}
For Silverlight, it’s more or less the same.
// C#
TextField.TextChanged += new TextChangedEventHandler(TextChanged);
void TextChanged(object sender, TextChangedEventArgs e)
{
try
{
double value = Convert.ToInt16(TextField.Text);
value = Math.Max(MIN_VALUE, Math.Min(MAX_VALUE, value));
TextField.Text = value.ToString();
}
// If user attempt to add non-numeric value to the text box
catch (Exception ex)
{
TextField.Text = "";
}
}
