动态增加控件

普通用户,可忽略本节内容。

Form提供了四个方法,用于动态增加控件。

CreateControl

用于创建控件,并返回所创建的控件。

语法为:

CreateControl(Name,ControlType)

Name:       控件名称
ControlType:控件类型,ControTypeEnum型枚举,包括以下可选值:

Button
Chart
CheckBox
CheckedComboBox
CheckedListBox
ColorLabel
ComboBox
DateTimePicker
GroupBox
Label
Line
ListBox
NavBar
NumericComboBox
Panel
PictureBox
ProgressBar
RadioButton
SplitContainer
TabControl
TextBox
WebBrowser
Painter
BarCode
FileManager
PictureViewer
RecordGrid
ListView
FilterTree
LoadTree
InlineTree
LayersTree

AddControl

用于将控件加入到窗口中,语法为

AddControl(Control)

Control:要加入到窗口的控件。

RemoveControl

用于从窗口删除指定名称的控件,语法为:

RemoveControl(Name)

Name:字符型,要删除的控件的名称。

ExistControl

返回一个逻辑值,用于判断窗口是否存在指定名称的控件,语法为:

ExistControl(Name)

Name:字符型,要判断的控件名称

示例一

例如希望单击某个按钮,能够向窗口中加入一个标签,可以将按钮的Click代码设为:

Dim lbl As WinForm.Label
lbl
= e.Form.CreateControl("lable1", ControlTypeEnum.Label)
lbl
.Text = "Foxtable"
lbl
.Left = 100
lbl
.Top = 100
e
.Form.AddControl(lbl)

示例二

可以利用BindingField给动态增加的控件设置绑定字段,例如:

Dim txt As WinForm.TextBox
txt = e.Form.CreateControl(
"TextBox1", ControlTypeEnum.TextBox)
txt.Left =
100
txt.Top =
100
e.Form.AddControl(txt)
txt.BindingField =
"订单.数量" '必须先将控件加入到窗口或容器中,才能设置绑定字段
 

上面的代码将创建一个文本框,并将其绑定到订单表的数量列。

提示:动态生成控件的时候,必须先将控件加入到窗口或容器中,才能设置其绑定字段,切记切记。


本页地址:http://www.foxtable.com/webhelp/topics/1409.htm