Little Effort, Big Savings Continued from page 57 Setting the Form events Setting the History control events The configuration and global variables are shown in Listing 1. In this application, the _flag variable is used to filter the controls that need to be populated by the tool. Make sure the names of the controls that will be populated by the tool start with the underscore character "_". Any ArcPad form developed using an APL file and a JavaScript file can be easily modified to use this tool in just two steps. The save_history() function is called when the OK button of a form is clicked as shown in Listing 2. As the program loops through the controls page by page, it scans the name of the control. If the name starts with a character defined by the _flag variable, its value is saved in the associated values array (which is indexed by the control's name). Then the values array is pushed into the _history global variable. The program also checks the number of entries in the _history variable and removes older entries, if necessary. As shown in Listing 3, the fill_form() function is fired when the selected item in the history control has changed. The index of the selected item will be passed to this function. Since the most recent entry is listed as the top item in the history control, the order of the items in the _history variable needs to be reversed to match the index from the history control. As shown in Listing 4, the draw_history() function is fired as the form is loading. It populates the history control with previous entries. //save the values of the current entry into the history variable //triggered by the Form onok event function save_history(){ var ctls, i, j, pgs, pg, m; var values = {}; //An associate array to store all the values pgs = ThisEvent.Object.Pages; //Reference to all the pages for (j = 1; j < pgs.Count + 1; j++) { //loop all the pages pg = pgs.Item(j);//current page ctls = pg.Controls;//all the controls on the current page for (i = 1; i < ctls.Count + 1; i++) { //loop all the controls m = ctls.Item(i).name.toLowerCase();//In case the name control is not case sensitive. if (ctls.Item(i).name.substr(0, 1) == _flag) { //filter the controls, if it starts with "_", then remember it switch (ctls.Item(i).Type) { //depending on the control's type, find the value need to be remembered. //the data looks like, values[control name] = "contro value" case "COMBOBOX": values[m] = ctls.Item(i).ListIndex; break; case "CHECKBOX": values[m] = ctls.Item(i).Value; break; case "EDIT": values[m] = ctls.Item(i).Value; break; case "RADIOBUTTON": values[m] = ctls.Item(i).Value break; } } } } //Now all the data are saved into the values variable //add the address to the values, so it can be populated for the history combobox values[_record] = pgs(_hctlsrc.page).Controls(_hctlsrc.ctl).Value; //Before saving values to the global history variable, check the length of the _history variable. //Only keep most recent 5 entries if (_history.length > 4) { _history.shift(); …….//get rid of the oldest one } _history.push(values); ….//add the current one on the top } Listing 2: Global variables ArcUser Summer 2009 www.esri.com