14.4. Preselecting Date Control Values

Problem

You want a Flash form date control to initialize with a particular value selected.

Solution

Assign a Date object to the component instance’s selectedDate property.

Discussion

The selectedDate property of a date field or date chooser component instance is a read/write property. That means that you can use it not only for retrieving a user-selected value, but also to programmatically assign a value. The selectedDate property must be in the format of an ActionScript Date object. Though you can create a Date object in a variety of ways, the simplest here is to use the Date constructor in a new statement, passing the constructor three parameters: the year, month, and date. The following example creates a new Date object that represents December 01, 2010, and assigns it to a variable named dSomeDay:

	var dSomeDay:Date = new Date(2010, 11, 1);

You may notice that the month value for December is 11, and not 12. This is because the month indices for the Date class start with 0 instead of 1. Therefore, January is 0, February is 1, March is 2, and so on.

After you’ve created the date, you can assign it to the date chooser or date field’s selectedDate property. The following example assigns the value from dSomeDay to the selectedDate property of a date field named cdfAppointment:

	cdfAppointment.selectedDate = dSomeDay;

Of course, you could skip the intermediate step of assigning the date to a variable first and simply create the date while assigning it ...

Get Flash 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.