DOCUMENT:Q162249 13-AUG-1999 [foxpro] TITLE :HOWTO: Create a Toolbar Containing a ComboBox PRODUCT :Microsoft FoxPro PROD/VER: OPER/SYS: KEYWORDS:kbcode kbOOP kbvfp500 kbvfp600 ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual FoxPro for Windows, versions 5.0, 6.0 ------------------------------------------------------------------------------- SUMMARY ======= A ToolBar is a class that is meant to be used with a variety of different forms. Because of this, a ToolBar's ComboBox, which shows a list of items relevant to the data shown on a particular form, generally will not be populated until the ToolBar that contains it is bound to that form. This article presents approaches to placing a ComboBox in a ToolBar, and several different methods of populating a ComboBox. Two of the ComboBoxes in Example 2 do not work, but they are intentionally included to demonstrate that an object that is instantiated before the object that contains it cannot be defined as having data from its container as its RowSource. A ComboBox in a ToolBar is instantiated before the ToolBar, which is instantiated before the formset (which ultimately contains the ToolBar) is instantiated. The data shown in the ComboBox must be defined from within the methods of the ComboBox, or placed there by events that happen after the ComboBox is instantiated. MORE INFORMATION ================ Here are two different examples. The first defines the objects visually, and the second defines them within a program. Example 1 --------- This example shows you how to create a ToolBar with 3 ComboBoxes through the user interface by using the class designer and the form designer. Two of the ComboBoxes are populated within methods of the containing ToolBar. The third is populated from the Init event of the formset that uses the ToolBar. In Visual FoxPro, create the ToolBar as follows: 1. On the File menu, click New. Select Class, and choose New File. 2. The New Class dialog box appears. Enter the following: Class Name: tbrTest Based On: Toolbar Store In: Test.VCX Click OK. 3. The Class Designer appears on the Visual FoxPro desktop. Be sure the Forms Control ToolBar is visible. If not, select Forms Control Toolbar from the View menu so that particular option has a checkmark at the left. Add a three ComboBox controls to the ToolBar. Leave the default control names so that tbrTest contains controls named Combo1, Combo2, and Combo3. 4. On the Class menu, select New Property. Enter "aArray2(10,1)" (without the quotes) as the Name. Click Add, and then click Close. 5. If the Properties window is not visible, go to the View menu and click Properties. Select tbrTest in the Object list at the top. Double-click "Init Event" in the list of properties. The Edit window for the Init Event of tbrTest appears. Enter the following code: ** ToolBar tbrTest Init event ** aArray2 is dimensioned as a new property of the ** tbrTest ToolBar class. THIS.aArray2(1) = "Newspapers" THIS.aArray2(2) = "Magazines" THIS.aArray2(3) = "Radio" THIS.aArray2(4) = "Television" THIS.aArray2(5) = "Cable" THIS.aArray2(6) = "Direct Mail" THIS.aArray2(7) = "the Internet" THIS.aArray2(8) = "Billboards" THIS.aArray2(9) = "Telemarketing" THIS.aArray2(10) = "Word-of-mouth" ** The next 3 lines populate Combo2 using the AddItem method. i = 0 FOR i = 1 to ALEN(THIS.aArray2) THIS.Combo2.AddItem(This.aArray2(i)) NEXT ** The next 3 lines populate Combo3 using the properties ** of that ComboBox. THIS.Combo3.RowSourcetype = 5 && Type Array THIS.Combo3.RowSource = "THIS.Parent.aArray2" ** The code uses the string at the right in the line above ** in the Requery method of the Combo as the RowSource ** for the Combo. THIS.Combo3.Requery ******** End of tbrTest Init Event ******** 6. On the File menu, click Close and then click Yes to save the file. 7. On the File menu, click New. Click Form and select New File. 8. On the Forms Control ToolBar, click View Classes (usually the #2 control on the Forms Control ToolBar). Click Add, and then select the class library you specified in step 2. 9. An abbreviated controls ToolBar appears. The tbrTest ToolBar appears as three tiny command buttons arrayed in a horizontal row. (If there are several similar controls, the tooltip will identify tbrTest.) Select tbrTest and place it by clicking on the form. Click Yes to create a form set object. 10. In the Properties window, select the Formset1 object in the ComboBox at the top. 11. Select New Property from the Form menu. Enter "aArray1(10,1)" (without the quotes) as the name. Click Add, then click Close. 12. In the Properties window for the Formset, set the Name property to "frsTest" (without the quotes). 13. In the Properties window, double-click the Init Event line. When the edit window for frsTest.Init appears, enter the following code: *** Init event code for formset frsTest *** IF TYPE("THIS.form1") = "O" && Ensures the form is instantiated. THIS.tbrTest.left = THIS.form1.left THIS.tbrTest.top = THIS.form1.top - (THIS.tbrTest.height + 25) ENDIF THIS.aArray1(1) = "This is 1" THIS.aArray1(2) = "This is 2" THIS.aArray1(3) = "This is 3" THIS.aArray1(4) = "This is 4" THIS.aArray1(5) = "This is 5" THIS.tbrTest.Combo1.AddItem(THIS.aArray1(5)) THIS.tbrTest.Combo1.AddItem(THIS.aArray1(4)) THIS.tbrTest.Combo1.AddItem(THIS.aArray1(3)) THIS.tbrTest.Combo1.AddItem(THIS.aArray1(2)) THIS.tbrTest.Combo1.AddItem(THIS.aArray1(1)) *** End if frsTest Init event code *** 14. Press Ctrl+W to save the code in the edit window. On the File menu, click Save As. Specify frsTest as the name of the .scx file. Run the form by clicking the red exclamation point tool on the Visual FoxPro main ToolBar. You should see an empty form, with the tbrTest ToolBar immediately above that form. Each Combo should show the list of information the above code provided. Example 2 --------- This example shows you how to create a ToolBar programmatically, and then use it on a formset that is created programmatically. Enter the following code into a new program named tbrTest: ************************************************* ** tbrTest.prg ** Sample program to populate ComboBoxes in ** a ToolBar. ************************************************* DIMENSION aFifthArray(10,1) aFifthArray(1) = "Boston" aFifthArray(2) = "Providence" aFifthArray(3) = "New Haven" aFifthArray(4) = "Armonk" aFifthArray(5) = "Grand Central" Dimension aSixthArray(10,1) aSixthArray(1) = "James" aSixthArray(2) = "Charles" aSixthArray(3) = "Hudson" aSixthArray(4) = "Rapahoneck" aSixthArray(5) = "Catawba" aSixthArray(6) = "Susquehanna" aSixthArray(7) = "Cooper" aSixthArray(8) = "Pee Dee" aSixthArray(9) = "Neuss" aSixthArray(10) = "Ohio" MyFormset = CREATEOBJECT("FormSet") WITH MyFormset .AddObject("tbrTools1", "tbrTest") .AddObject("Myform", "form1") WITH .myform .AddObject("quitter", "cmdClose") WITH .Quitter .top = 150 .left = 200 .visible = .T. ENDWITH .top = 100 .left = 10 .visible = .T. ENDWITH WITH .tbrTools1 .top = myformset.myform.top - 60 .Left = myformset.myform.left .visible = .T. WITH .Combo5 .AddItem(aFifthArray(1)) .AddItem(aFifthArray(2)) .AddItem(aFifthArray(3)) .AddItem(aFifthArray(4)) .AddItem(aFifthArray(5)) ENDWITH WITH .Combo6 .RowSourcetype = 5 .RowSource = "aSixthArray" .Requery ENDWITH ENDWITH ENDWITH READ EVENTS DEFINE CLASS form1 AS FORM Height = 100 Width = 500 Top = 100 Left = 10 ADD OBJECT cmdQuit as cmdClose ENDDEFINE DEFINE CLASS cmdClose AS CommandButton Height = 29 Width = 50 Caption = "\