动态生成N个控件,如何给这N个控件写click事件啊??我这样写,只能给最后一个动态生的控件加上Click事件:option Expcitisprivate withevents newcommand as commandbuttonfor i=0 to 9    set newcommand=controls.add ("VB.Commandbutton","command"&i)    with form1("command"&i)        .visible=true       .caption="command"&i    end with   private sub newcommand_click()      msgbox newcommand.caption  end subnext 说明:这动态生成的控件数量是不确定的,这个问题该如何解决,谢谢。

解决方案 »

  1.   

    在窗体上先放一个按钮,设置为数组,如果开始不需要,则隐藏该按钮;
    生成时:for i=1 to num'数量
    private sub newcommand_click(index as integer)
    msgbox ""
    end sub
      

  2.   

    我记得是不可以的,动态添加的控件如果要with events,就不可以以数组的样子出现了。
    也就是说,只能一个一个的加,每个名字不一样。这一点很不方便的,我的解决方法是:
    先在设计时,自己加上100个(或更多)控件,全设为visible=false
    然后要添加的时候就把visible=true,然后就和新添控件一样了缺点是
    1。占用资源可能会比动态添加大(只是我感觉的)
    2。添加的控件个数有限制,最多就是你在设计时加的个数。不过你可以多加几个的
      

  3.   

    '先有form1上加一个Command1(0)
    Private Sub Command1_Click(Index As Integer)
          MsgBox Command1(Index).IndexEnd SubPrivate Sub Form_Load()
            For i = 1 To 10
            Load Command1(i)
            Command1(i).Top = 400 * i + 200
            Command1(i).Left = 20
            Command1(i).Visible = True
        Next
    End Sub
      

  4.   

    上面就是添加控件数组private withevents newcommand as commandbutton  不能做数组