关键字:WithEvents但是不能用于数组,你的程序要麻烦一些。FORM1.FRM:Option ExplicitDim WithEvents cmd As CommandButtonPrivate Sub cmd_Click()
MsgBox "OK"
End SubPrivate Sub Command1_Click()
Set cmd = Me.Controls.Add("VB.CommandButton", "cmdTest")
cmd.Visible = True
cmd.Left = 0
cmd.Top = 0
cmd.Caption = "&Test"
End Sub
MsgBox "OK"
End SubPrivate Sub Command1_Click()
Set cmd = Me.Controls.Add("VB.CommandButton", "cmdTest")
cmd.Visible = True
cmd.Left = 0
cmd.Top = 0
cmd.Caption = "&Test"
End Sub
BUTTON,我想生成多个BUTTON,单击不同的BUTTON,有不同的事件。
有意思,关注,我正在想办法。to edyang:
你的方法好像有错,第二次点击Command1后,出现错误‘727’。
button控件,把它的index属性设为0(即定义成控件数组),然后在调用的时候用
load command1(index) 'index为你数组的下标
command1(index).visible=true
'你可以先把command1(0).visible设为false
在command1_click(index as integer)时间里根据你不同的index值写事件就可以了。 我的方法可能你也想过,可能你一定要用2维数组,不过我觉得1维已经可以足够了。
呵呵,只是个例子,当然不能点第二次了,加个 If cmd Is Nothing Then ... End 就可以啦!看我的全新方案:EventSink.cls:Option ExplicitDim WithEvents m_cmd As CommandButtonPublic Property Set Button(cmd As CommandButton)
Set m_cmd = cmd
End PropertyPublic Property Get Button() As CommandButton
Set Button = m_cmd
End PropertyPrivate Sub m_cmd_Click()
MsgBox m_cmd.Caption
End Sub====================================================================frmMain.frm:Option ExplicitDim cmdgroup(10, 10) As New EventSinkPrivate Sub Form_Load()
Dim i As Long
Dim j As Long
Dim cmdThisOne As CommandButton
For i = 0 To 10
For j = 0 To 10
Set cmdgroup(i, j).Button = Me.Controls.Add("VB.CommandButton", "cmdButton" & i & j)
Set cmdThisOne = cmdgroup(i, j).Button
With cmdThisOne
.Caption = "Test Button R" & j & "C" & i
.Left = i * 1000
.Top = j * 600
.Width = 900
.Height = 500
.Visible = True
End With
Next
Next
End Sub
先声明一个标准模快
Option Explicit
Public lpPrevFunction As Long
Public gHW As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = -4
Public Const WM_COMMAND = &H111
Public size As Integer '大小
Public cmdObject(10, 10) As CommandButton '声明一个动态的BUTTON数组Function WndProc(ByVal hwnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim i, j As Long
Select Case message
Case WM_COMMAND
For i = 0 To size
For j = 0 To size
If (cmdObject(i, j).hwnd = lParam) Then
MsgBox cmdObject(i, j).Caption
End If
Next
Next
End Select
'Debug.Print hwnd, message, wParam, lParam
WndProc = CallWindowProc(lpPrevFunction, hwnd, message, wParam, lParam)
End FunctionPublic Sub Hook()
lpPrevFunction = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WndProc)
End Sub
再把你的窗体的代码改成
Option ExplicitPrivate Sub Form_Load()
size = Val(InputBox("please input the size you want")) '输入大小1-9
gHW = Me.hwnd
Call Hook
Dim i, j As Integer
For i = 0 To size
For j = 0 To size
Set cmdObject(i, j) = Form1.Controls.Add("VB.CommandButton", _
"cmdOne" & i & j) '动态添加BUTTON
cmdObject(i, j).Visible = True
cmdObject(i, j).Caption = i & " " & j
cmdObject(i, j).Left = j * 400
cmdObject(i, j).Width = 400
cmdObject(i, j).Height = 400
cmdObject(i, j).Top = 400 * i '布置大小,位置,
Next
Next
End Sub
就可以了
在窗口里放一个button, 把index设成0
在程序里要添加button, 直接load button(i)就行了, 要多少有多少!
但是记得load 完之后的东西visible都是false, 要设一下的: button(i).visible=true!!!
不错,和我想的一样。