展开全部
思路
两种方式:
1、通过动态按钮方式创建,
2:通过控件数组方式创建。
个人倾向于通过控件数组的方式。一般应用都是有规律的创建的,所以用控件数组合适些。
举例
' 先在窗体上添加一个标准按钮
' 在属性窗口中,把它的 Index属性设置成 0
' 名称改为 cmdDynCmd (其它的名称也行,注意后面的说明)
' Visible 可以设置成 False
Option Explicit
Private lCmdIndex As Long
Private Sub LoadCmdButton()
'这儿就是加载动态按钮的
Dim i&
For i = 0 To NNN '这儿NNN等于你需要的按钮数减1,可以用变量的
'你应该能通过你的“数据库中"a"表中"名称"字段”项目数 来确定值为多少
' *** 注意这儿的 cmdDynCmd 就是设计时确定的按键名称 ***
If (lCmdIndex > 0) Then Load cmdDynCmd(i)
cmdDynCmd(i).Visible = True
'这儿的 Caption 属性,你自己用代码跟你的 数据库中"a"表中"名称"字段内容关联
cmdDynCmd(i).Caption = "动态按钮" & i + 1
'这儿的 x 、y 你可以根据 i 的值来进行计算
cmdDynCmd(i).Top = X
cmdDynCmd(i).Left = Y
'直接 cmdDynCmd(i).move x, y也行
next
End Sub
' 这儿就是动态按钮的事件代码
' 根据 Index 判断是哪个按钮触发了事件
' 跟你事先设计好的控件数组完全是相同的用法
' ***** 注意!!!注意这儿的 cmdDynCmd 就是设计时确定的按键名称
Private Sub cmdDynCmd_Click(Index As Integer)
MsgBox "你点击的是第 " & Index + 1 & " 个动态按钮。", 64
'具体事件代码只能你自己写
End Sub
展开全部
Option Explicit
Dim WithEvents cmdSayHello As CommandButton
Dim WithEvents cmdClose As CommandButton
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdSayHello_Click()
MsgBox "Hello world!", vbInformation, Me.Caption
End Sub
Private Sub Form_Load()
Set cmdSayHello = Me.Controls.Add("VB.CommandButton", "cmdSayHello")
With cmdSayHello
.Left = 4000
.Top = 250
.Width = 1200
.Height = 350
.Caption = "&Say Hello"
.Visible = True
End With
Set cmdClose = Me.Controls.Add("VB.CommandButton", "cmdClose")
With cmdClose
.Left = 4000
.Top = 700
.Width = 1200
.Height = 350
.Caption = "&Close"
.Visible = True
End With
End Sub
Dim WithEvents cmdSayHello As CommandButton
Dim WithEvents cmdClose As CommandButton
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdSayHello_Click()
MsgBox "Hello world!", vbInformation, Me.Caption
End Sub
Private Sub Form_Load()
Set cmdSayHello = Me.Controls.Add("VB.CommandButton", "cmdSayHello")
With cmdSayHello
.Left = 4000
.Top = 250
.Width = 1200
.Height = 350
.Caption = "&Say Hello"
.Visible = True
End With
Set cmdClose = Me.Controls.Add("VB.CommandButton", "cmdClose")
With cmdClose
.Left = 4000
.Top = 700
.Width = 1200
.Height = 350
.Caption = "&Close"
.Visible = True
End With
End Sub
参考资料: http://topic.csdn.net/t/20051222/08/4473985.html
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim WithEvents Command1 As CommandButton
Private Sub Form_Load()
Set Command1 = Controls.Add("vb.commandbutton", "command1", Form1)
With Command1
.Width = 1800
.Height = 450
.Left = (Me.ScaleWidth - .Width) / 2
.Top = (Form1.ScaleHeight - .Height) / 2
.Caption = "我是打哪来的^o^"
.Visible = True
End With
End Sub
Private Sub Command1_Click()
MsgBox "乱按什么啊^&^!!!!" & Chr(10) & Chr(10) & "不理你了!!!", 48
Me.Controls.Remove Command1
End Sub
Private Sub Form_Load()
Set Command1 = Controls.Add("vb.commandbutton", "command1", Form1)
With Command1
.Width = 1800
.Height = 450
.Left = (Me.ScaleWidth - .Width) / 2
.Top = (Form1.ScaleHeight - .Height) / 2
.Caption = "我是打哪来的^o^"
.Visible = True
End With
End Sub
Private Sub Command1_Click()
MsgBox "乱按什么啊^&^!!!!" & Chr(10) & Chr(10) & "不理你了!!!", 48
Me.Controls.Remove Command1
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询