HOWTO: Creating a Toolbar Containing a ComboBoxLast reviewed: February 10, 1997Article ID: Q162249 |
The information in this article applies to:
SUMMARYA 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 INFORMATIONHere are two different examples. The first defines the objects visually, and the second defines them within a program.
Example 1This 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:
Example 2This 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 = "\<Close" PROCEDURE Click ThisFormSet.Release CLEAR EVENTS ENDPROC ENDDEFINE DEFINE CLASS tbrTest AS TOOLBAR ADD OBJECT Combo1 AS COMBOBOX ADD OBJECT Combo2 AS COMBOBOX ADD OBJECT Combo3 AS COMBOBOX ADD OBJECT Combo4 AS COMBOBOX ADD OBJECT Combo5 AS COMBOBOX ADD OBJECT Combo6 AS COMBOBOX * Start of the class defInition: one ToolBar with 6 ComboBoxes. Left = 1 Top = 1 Width = 575 Caption = "Test ToolBar #2" Combo1.Height = 27 Combo1.Width = 82 Combo2.Height = 27 Combo2.Width = 82 Combo3.Height = 27 Combo3.Width = 82 Combo4.Height = 27 Combo4.Width = 82 Combo5.Height = 27 Combo5.Width = 82 Combo6.Height = 27 Combo6.Width = 82 PROCEDURE Load * ComboBox2 does not exist at Load time. Dimension gaArray1(10,1) gaArray1(1) = "Arkansas" gaArray1(2) = "Colorado" gaArray1(3) = "Delaware" gaArray1(4) = "Georgia" THIS.Combo2.AddItem(This.gaArray1(1)) THIS.Combo2.AddItem(This.gaArray1(2)) THIS.Combo2.AddItem(This.gaArray1(3)) THIS.Combo2.AddItem(This.gaArray1(4)) ENDPROC * Sets properties of the controls. * Notice that there are no Top or Left property settings for * controls on a ToolBar. Controls on a ToolBar are automatically * positioned in the order they are added. PROCEDURE Activate * ComboBox1 is populated within the activate procedure of the ToolBar. THIS.Combo1.AddItem("This is item 1") THIS.Combo1.AddItem("This is item 2") THIS.Combo1.AddItem("This is item 3") THIS.Combo1.AddItem("There are no more") * ComboBox2 is populated from array elements declared * and populated in the Load event of the ToolBar, but this code is * run before the ToolBar is loaded, so it causes the error "Property * 'GARALPHARRAY' is not found." *THIS.Combo2.AddItem(This.gaArray1(1)) *THIS.Combo2.AddItem(This.gaArray1(2)) *THIS.Combo2.AddItem(This.gaArray1(3)) *THIS.Combo2.AddItem(This.gaArray1(4)) * The following code depends upon the formset having a * gaSomeArray() defined and populated, but it suffers from the same * problem as the code immediately above, and causes the error * "Property 'GASOMEARRAY' is not found." *THIS.Combo3.AddItem(THISFORMSET.gaSomeArray(1)) *THIS.Combo3.AddItem(THISFORMSET.gaSomeArray(2)) *THIS.Combo3.AddItem(THISFORMSET.gaSomeArray(3)) *THIS.Combo3.AddItem(THISFORMSET.gaSomeArray(4)) DIMENSION gaArray4(10,1) gaArray4(1) = "Seattle" gaArray4(2) = "Portland" gaArray4(3) = "San Francisco" gaArray4(4) = "Los Angeles" gaArray4(5) = "San Diego" THIS.Combo4.AddItem(gaArray4(1)) THIS.Combo4.AddItem(gaArray4(2)) THIS.Combo4.AddItem(gaArray4(3)) THIS.Combo4.AddItem(gaArray4(4)) THIS.Combo4.AddItem(gaArray4(5)) * ComboBox5 is populated from program code run after the * formset is loaded, Initialized, and populated, from an * array declared and Initialized before the formset is * created. ENDDEFINE && End of ToolBar class defInition(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Chris Jensen, Microsoft Corporation
|
KBCategory: kbusage kbhowto kbcode
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |