Hands On
pTheTextBox.Text="" ‘Enable the save button pWriteButton.Enabled = True ‘Do not reread INI file Application.UserProperties _ ("strCboUpdate1") = "False" End If ‘Clear variables Set pTheComboBox = Nothing Set pTheTextBox = Nothing Set pWriteButton = Nothing Set strAddValue = Nothing Listing 6: OnClick event for the Add button That is all that needs to be done to the preference dialog. If you have multiple combo boxes on a form, add more pages to the preference form. With very little change to the code above (e.g., renaming buttons and INI files), the code can be used for many different combo boxes. The final thing to do is add some code to the script file of the applet. Combos Subroutine By putting the code in subroutines, this code can be accessed and (mostly) reused by multiple forms. First, add the Update global variable in the script file to tell the ComboBox to load the first time it gets the focus. Second, add the variable INIPath. (Refer to Listing 7.) The INIPath variable saves the location where the INI files are stored on the computer. It is a good idea to keep all INI files for a particular application together. This allows the use of only one variable, and it makes it easier to locate them outside of ArcPad. Application.UserProperties("strCboUpdate1") _ = "True" Dim INIPath ‘Path for the INI file INIPath = "C:\temp\Water\INI\" Listing 7: Update the global variable. Next, add the subroutine LoadCombos. (Refer to Listing 8.) This subroutine is the major component of this project. It is called by all combo boxes to fill in the list for each combo box from INI files. This subroutine is passed four values: the form name that is calling it, the page the ComboBox is on, the name of the combo box, and the name of the INI file to read. After the values are passed to the subroutine, the subroutine reads each line in the INI file and adds it to the list in the combo box. When the end of the file is reached, the subroutine closes the file and erases the variables. ‘Generic sub to fill ComboBoxes from INI files ‘arguments(Form name, Page name, Control name, ‘INI file name) Sub LoadCombos(strForm, strPage, strControl, _ strINI) Dim AplFile, INIvalue
www.esri.com
‘Open INI file Set AplFile = Application. _ CreateAppObject("file") AplFile.Open INIPath & "\" & strINI _ &".ini", 1 Dim pTheComboBox Set pTheComboBox = Application.Applets _ ("Demo.apa").Forms(strForm) _ .Pages(strPage) _ .Controls(strControl) ‘Clear contents of the combo box pTheComboBox .Clear INIvalue = AplFile.ReadLine ‘Add values from INI file to ComboBox Do While Not AplFile.EOF INIvalue = AplFile.ReadLine pTheComboBox .AddItem INIvalue, INIvalue Loop ‘Close INI file AplFile.Close ‘Clear variables Set AplFile = Nothing Set pTheComboBox = Nothing Set INIvalue = Nothing
End Sub Listing 8: Add the subroutine LoadCombos. The last thing to be added to this script is the WritePrefs subroutine. (Refer to Listing 9.) This subroutine is passed five values—the name of the calling form, the page the ComboBox is on, the name of the combo box, the name of the INI file to write to, and the header for the INI file. This subroutine begins by opening the INI file to write in all the values from the ComboBox. Then it writes the first line of the INI file (which is the header). Next, it goes through each item in the ComboBox and writes the value to the INI file. When it gets to the end of the list, the INI file is closed and the variables are erased. This is all that needs to be added to the script file. ‘Generic sub to write INI file from ComboBox ‘arguments(Form name, Page name, Control name, ‘INI file name, Header title) Sub WritePrefs(strForm, strPage, strControl, _ strINI, strHeader) Dim AplFile, pTheComboBox , a ‘Open INI file for output Set AplFile = Application. _ CreateAppObject("file") Continued on page 54
ArcUser Fall 2009 53