在VB中,在窗体加载时动态加入了30个按钮控件,他们的click事件的代码相同,
我想知道一个函数能不能实现这些按钮所要完成的功能, 象VC++中的消息源ID和消息ID一样在VB中定义一个函数:
   Function Commmand_Click(Button_name as string,Event_ID as long)
通过判断Button_name 和Event_ID来执行相应的代码.

解决方案 »

  1.   

    Private Sub Command1_Click(Index As Integer)
       if Command1.caption="" then
       '''
      endif
    End Sub
      

  2.   

    cmdDyna CommandButton
      Height  = 255
      Width   = 735
      Index   = 0
      Visible = False'========================================Option ExplicitPrivate Sub cmdDyna_Click(Index As Integer)
        MsgBox "You clicked " & cmdDyna(Index).Caption
    End SubPrivate Sub Form_Load()    Dim cmdNew    As CommandButton
        Dim intUBound As Integer
        Dim intRow    As Integer
        Dim intCol    As Integer
        Dim intX      As Integer
        Dim intY      As Integer
        Dim intW      As Integer
        Dim intH      As Integer
        
        intCol = 1
        intRow = 1
        
        intUBound = cmdDyna.UBound
        Set cmdNew = cmdDyna(intUBound)
        
        intX = 0
        intY = 0
        intW = cmdNew.Width
        intH = cmdNew.Height
        
        Do While True
        
            If intY + intH > Me.ScaleHeight Then
                intX = intX + intW
                intY = 0
                intCol = intCol + 1
                intRow = 1
            End If
            
            If intX + intW > Me.ScaleWidth Then
                Exit Do
            End If
            
            intUBound = intUBound + 1
            Load cmdDyna(intUBound)
            
            Set cmdNew = cmdDyna(intUBound)
            
            cmdNew.Caption = "(" & intCol & "," & intRow & ")"
            cmdNew.Left = intX
            cmdNew.Top = intY
            cmdNew.Width = intW
            cmdNew.Height = intH
            cmdNew.Visible = True
            
            intY = intY + intH
            intRow = intRow + 1
            
        Loop
        
    End Sub根据Index的值来判断是哪个控件触发了该事件
      

  3.   

    VC可以轻易为一个控件添加事件,而在vb中则是很难的,但vb中有控件数组的概念,
    你最可以将相同的控件做成数组,那么它们共享一个事件.
    如果有其他的控件也想集成一个事件.vb的子类化技术可能能帮你.
    例子:vb光盘中的systray工程.