vb控件设置属性的问题

来源:百度知道 编辑:UC知道 时间:2024/06/28 04:31:19
比如,窗体上已有名为button1,button2,.......button100,100个按钮,如何用一个循环修改所有的capation 为"XXXXX"

把下面的代码放入你的窗体中,试一下吧!
Dim mCmd As CommandButton
Dim intCmdCaption As Integer
For Each mCmd In Me.Controls
If InStr(mCmd.Caption, "button") > 0 Then
intCmdCaption = Val(Replace(mCmd.Caption, "button", ""))
If intCmdCaption >= 1 Or intCmdCaption <= 100 Then
mCmd.Caption = "XXXXX"
End If
End If
Next

为什么不用组
要用这样的不麻烦吗?

用控件数组解决问题,那样可以事半功倍!

创建一个command1的数组(索引值为0--99)

for i = 1 to 99
command1(i).caption = "XXXXX"
next

Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is CommandButton Then
ctl.Caption = "VB我的最爱"
End If
Next
这样的代码更具通用性