VFP's Form Control toolbar contains commonly used control classes, such as labels, text boxes (textbox), and so on, and most of the time when we add controls to the form, we use the toolbar at design time. When you click a control icon on the toolbar, you can drag and draw the control you want on the form. But sometimes the controls we need may not be determined at design time, but are created dynamically when the application is run, and you need to programmatically add new controls to the form by using the AddObject () function or the Creatobject () function.
In this example, in the Click event code of a command button, a table is dynamically created using the system base class grid, and clicking the New Table button creates a table dynamically and assigns the site information table as the data source. In this example, the data table "Web site Information table" in the "Data 1" database is used, and the situation of the database is already looking at the case study VFP: The sample database is given in the article, the Run-time interface is as follows:
When you click the New Table button, the table is created dynamically and the data for the Site information table is displayed, as shown in the following figure:
This example makes the following steps:
1. Create a new form Form1, save it as "programmatically add a new control to the form. Scx" and set its properties in the following table:
Property name |
Property value |
Caption |
Getting Started web-programmatically adding new controls to a form |
AutoCenter |
. T. |
Width |
375 |
Height |
250 |
2. Add two command buttons Command1 and Command2 to the form, set their caption properties to new table and exit, and adjust their position on the form, as shown below:
3. Add event code:
(1) Init event for form Form1:
Use Web site information table
(2) The Unload event of the form Form1:
Close data
(3) The Click event for the command button Command1:
This.enabled=.f.
thisform.height=250
thisform.addobject (' grid1 ', ' grid ')
thisform.grid1.Setall ("Dynamicbackcolor", "RGB" ( 224,225,255) "," Column ") with
thisform.grid1.
top=50
. left=10.
width=thisform.width-10
. height=thisform.height-70
. recordsource= ' website Information table '
. visible=.t.
Column1.header1.backcolor=rgb (255,255,190)
. Column2.header1.backcolor=rgb (255,255,190)
. Column3.header1.backcolor=rgb (255,255,190)
Endwith
(4) The Click () event of the command button Command2:
Thisform.release
4. Run "Programmatically add a new control to the form. Scx".
This example code is debugged in the win2003+vfp6.0 environment.
See the full set of "rookie also learn VFP" tutorial