求Controls.Add控件详解

ning1958 2011-03-03 12:29:32
在网上找了一下,动态添加控件的方法比较零乱。
添加按钮控件
Controls.Add("Forms.CommandButton.1", "CommandButton" & i + 2, True)
添加textbox控件
Controls.Add(bstrprogid:="Forms.TextBox.1")

请各位大侠帮助的是。
在哪里能找到比较系统的控件表。
checkbox,listbox等。谢谢。

...全文
1784 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
caspernet 2012-03-27
  • 打赏
  • 举报
回复
我感兴趣找了一下,这个叫OLE 编程标识符,(有时也称 ProgID)可创建一个 Automation 对象。

在帮助,搜索OLE,能找到一些。

控件 标识符
CheckBox Forms.CheckBox
ComboBox Forms.ComboBox
CommandButton Forms.CommandButton
Frame Forms.Frame
Image Forms.Image
Label Forms.Label
ListBox Forms.ListBox
MultiPage Forms.MultiPage
OptionButton Forms.OptionButton
ScrollBar Forms.ScrollBar
SpinButton Forms.SpinButton
TabStrip Forms.TabStrip
TextBox Forms.TextBox
切换按钮 Forms.ToggleButton
无·法 2011-03-03
  • 打赏
  • 举报
回复
object.Add (ProgID, name, container)

Add 方法语法有这些部分:

部分 描述
Object 必需的。一个对象表达式,其值是“应用于”列表中的一个对象。
ProgID 必需的。一个标识控件的字符串。大多数控件的 ProgID 都可通过查看对象浏览器来决定。控件的 ProgID 是由控件的库和类组成的。例如,CommandButton 控件的 ProgID 是 VB.CommandButton。在 ProgID 与对象浏览器中所显示的不一样的情况下,Visual Basic 将显示一个包括正确 ProgId 的错误信息。 name 必要的。一个字符串,用来标识集合的成员。
container 可选的。一个对象引用,它指定控件的容器。如果没有指定或为 NULL,缺省值为 Controls 集合所属的容器。通过指定该参数,可以把一个控件放置在任何现存的容器控件(如 Frame 控件)中。用户控件或 ActiveX 文档也可以作为一个容器。
  • 打赏
  • 举报
回复
http://support.microsoft.com/kb/190670
ning1958 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sysdzw 的回复:]
忽然发现你是在vba环境下做的,这个和vb中区别还是不小的,建议以后请先说明环境。下面是vba中msdn查阅到的:

Add 方法的语法包括以下成分:

成分 说明
object 必需。一个有效对象名。
Name 可选。指定被添加的对象的名称。如果没有指定名称,系统将根据使用该窗体的应用程序的规则,产生一默认的名称。
Caption 可选。指定在标签或控件上出现的题注。如果没有……
[/Quote]

谢谢,以后会注意说明excel-vba的工作环境,个人工作习惯是在EXCEL-VBAVB6调试程序,调试差不多,在VB6打包dll。
主要应用是在EXCEL中加载dll文件。
无·法 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 veron_04 的回复:]

看看这本书,里面有例子:
http://download.csdn.net/source/1675743
[/Quote]asdqwhui发表的评论

先下了看看,不清晰我就骂你
时间:2010-07-17 22:55:23 来自:113.96.173.*
贝隆 2011-03-03
  • 打赏
  • 举报
回复
看看这本书,里面有例子:
http://download.csdn.net/source/1675743
无·法 2011-03-03
  • 打赏
  • 举报
回复
忽然发现你是在vba环境下做的,这个和vb中区别还是不小的,建议以后请先说明环境。下面是vba中msdn查阅到的:

Add 方法的语法包括以下成分:

成分 说明
object 必需。一个有效对象名。
Name 可选。指定被添加的对象的名称。如果没有指定名称,系统将根据使用该窗体的应用程序的规则,产生一默认的名称。
Caption 可选。指定在标签或控件上出现的题注。如果没有指定题注,系统将根据使用该窗体的应用程序的规则,产生一默认的题注。
index 可选。表示页或标签在 Pages 或 Tabs 集合中的位置。若没有规定索引,系统会将页或标签添加到 Pages 或 Tabs 集合的末尾,并赋于它相应的索引值。
ProgID 必需。程序设计标识符。是用于标识对象类的、没有空格的文本串。ProgID 的标准语法是 <Vendor>.<Component>.<Version>。ProgID 会被映射为类标识符 (CLSID)。
Visible 可选。若对象为可见的(这是默认方式)则为 True;若对象为隐藏的则为 False。


设置

单个控件的 ProgID 值:

复选框 Forms.CheckBox.1
组合框 Forms.ComboBox.1
命令按钮 Forms.CommandButton.1
框架 Forms.Frame.1
图像 Forms.Image.1
标签 Forms.Label.1
列表框 Forms.ListBox.1
多页 Forms.MultiPage.1
选项按钮 Forms.OptionButton.1
滚动条 Forms.ScrollBar.1
数值调节钮 Forms.SpinButton.1
TabStrip Forms.TabStrip.1
文本框 Forms.TextBox.1
切换按钮 Forms.ToggleButton.1
无·法 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ning1958 的回复:]
引用 1 楼 patrickkong 的回复:
http://support.microsoft.com/kb/190670

谢谢回复
此示例有commandbutton,textbox和treeview控件。不是很全。


VB code
' If you are adding an ActiveX control at run-time that is
' not refe……
[/Quote]要完全的是不可能有的,因为所有第三方控件基本都可以通过这个方法添加进来,而谁又能把所有第三方控件统计出来?
标准控件的话在对象浏览器中都可以看到。按F2就行了。第一个列表框选择VB就会显示vb的标准控件(并不都是)。
如果是第三方控件的话先通过部件添加进来,然后在对象浏览器里也可以看到。比如Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", "myctl", Form1),在添加部件ms windows common controls 6.0后就可以在对象浏览器中看到,MSComctlLib是库名,TreeCtrl是控件类名。
dbcontrols 2011-03-03
  • 打赏
  • 举报
回复
我这里有个全的
ning1958 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 patrickkong 的回复:]
http://support.microsoft.com/kb/190670
[/Quote]
谢谢回复
此示例有commandbutton,textbox和treeview控件。不是很全。

' If you are adding an ActiveX control at run-time that is
' not referenced in your project, you need to declare it
' as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton

Private Sub ctlCommand_Click()
ctlText.Text = "You Clicked the Command button"
End Sub

Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
' test for the click event of the TreeView
If Info.Name = "Click" Then
ctlText.Text = "You clicked " & ctlDynamic.object.selecteditem.Text
End If
End Sub

Private Sub Form_Load()
Dim i As Integer
' Add the license for the treeview to the license collection.
' If the license is already in the collection you will get
' the run-time error number 732.
Licenses.Add "MSComctlLib.TreeCtrl"

' Dynamically add a TreeView control to the form.
' If you want the control to be added to a different
' container such as a Frame or PictureBox, you use the third
' parameter of the Controls.Add to specify the container.
Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
"myctl", Form1)
' set the location and size of the control.
ctlDynamic.Move 1, 1, 2500, 3500

' Add some nodes to the control.
For i = 1 To 10
ctlDynamic.object.nodes.Add Key:="Test" & Str(i), Text:="Test" _
& Str(i)
ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), _
Relationship:=4, Text:="TestChild" & Str(i)
Next i

' Make the control visible.
ctlDynamic.Visible = True

' add a textbox
Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
' Set the location and size of the textbox
ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
1, 2500, 100

' Change the backcolor.
ctlText.BackColor = vbYellow

' Make it visible
ctlText.Visible = True

' Add a CommandButton.
Set ctlCommand = Controls.Add("VB.CommandButton", _
"ctlCommand1", Form1)

' Set the location and size of the CommandButton.
ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
ctlText.Height + 50, 1500, 500

' Set the caption
ctlCommand.Caption = "Click Me"

' Make it visible
ctlCommand.Visible = True
End Sub


7,787

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧