读书人

比较名称找出没有名称的Index,该怎么解

发布时间: 2012-05-27 05:42:30 作者: rapoo

比较名称找出没有名称的Index

有一个按钮数组Command1,5个按钮,其Index为不连续的

Command1(1).Caption="qq"
Command1(2).Caption ="aa"
Command1(4).Caption ="zz"
Command1(5).Caption ="ww"
Command1(7).Caption ="ss"

有数量不固定的Label控件


比如:
Label1.Caption="qq"
Label2.Caption="aa"
Label4.Caption="ss"

怎么根据Label的Caption比较找出按钮数组Command1中Caption与Label的Caption不相同的Index呢
For Each pp In Label
pp.Caption Then
Next pp





[解决办法]
新建一工程,添加控件数组command1(0-5),caption为0-5;添加控件label1到label6,caption为0到5.
本示例能debug.print出caption一样,但是控件数组command1的下标跟label控件名中的数值不符合的index

VB code
Private Sub Form_Click()    Dim ControlS As Control, i As Byte    For Each ControlS In Form1        If LCase(TypeName(ControlS)) = "label" Then            For i = 0 To 5                If Form1.Command1(i).Caption = ControlS.Caption Then                    If Replace(ControlS.Name, "Label", "") <> i Then Debug.Print i                End If            Next        End If    NextEnd Sub
[解决办法]
Dim strSample As String
Dim x As Control

strSample = Label1 & "|" & Label2 & "|" & Label4

For Each x In Me.Controls
If x.Name = "Command1" And InStr(strSample, x.Caption) < 1 Then
Debug.Print x.Index, x.Caption
End If
Next x

读书人网 >VB

热点推荐