# Re: 关于动态产生控制项

Posted by 帅帅阿柳 on May 23, 1999 at 22:47:20:

In Reply to: 关于动态产生控制项 posted by 百吉 on May 22, 1999 at 18:06:47:

Dear 百吉:
关键在于控制项的instance必须以name属性加以识别,也就是说在同个表单内任两个控制项是不可以有相同的识别名称的,除非您使用控制项阵列,因此下一行程式码中:

Set ctlMyControl = Controls.Add("VB.CommandButton", "newCam")

只要改成:

Set ctlMyControl = Controls.Add("VB.CommandButton", "newCam"&i)

这样就会在回圈中产生newCam1及newCam2两个命令按钮了;另外,在回圈中使用test=test+1也会产生问题,因为for test=1 to 2这个指令就会让test在next时自动加1了.

整个程式码Form_Click事件程序的程式码阿柳改写如下:

Private Sub Form_Click()
Dim test As Long
Dim test1 As Long
Static intNumber As Integer

For test = intNumber + 1 To intNumber + 2

Set ctlMyControl = Controls.Add("VB.CommandButton", "newCam" & test)
ctlMyControl.Left = 240 + (ctlMyControl.Width) * test
ctlMyControl.Top = 240
ctlMyControl.Caption = "离开(&X)"
ctlMyControl.Visible = True
intNumber = intNumber + 1
Next

End Sub

也感谢百吉兄购买阿柳的书,自从微软的书大量上市后,阿柳估计除了王国荣先生外,其他都要转行了:)

Follow Ups:

- 抱歉,补充一点 帅帅阿柳 00:03:12 5/24/99 (0)