vb 我有几个命令按钮,点击其中的任意两个,如果他们的名称一样,则他们两个都消失,怎么写代码呢? 谢谢
以上是窗体设计界面。
以下是程序运行界面:
程序代码如下:
Dim Flag1(7) As Boolean, Flag2(7) As Boolean, Flag As Boolean
Private Sub Command1_Click(Index As Integer)
Flag1(Index) = False
If Flag2(Index) = False Then
Command1(Index).Visible = False
Command2(Index).Visible = False
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Flag2(Index) = False
If Flag1(Index) = False Then
Flag = Not Flag
Command2(Index).Visible = False
Command1(Index).Visible = False
End If
End Sub
Private Sub Command3_Click()
Flag = Not Flag
If Not Flag Then
Command3.Caption = "显示按钮名称"
For i = 0 To 7
Command1(i).Caption = ""
Command2(i).Caption = ""
Next i
Else
Command3.Caption = "隐藏按钮名称"
For i = 0 To 7
Command1(i).Caption = Chr(i + 65)
Command2(i).Caption = Chr(i + 65)
Next i
End If
End Sub
Private Sub Command4_Click()
Command4.Caption = "重新打乱按钮顺序"
Randomize (Timer)
Dim intOrder1(1 To 8) As Integer, intOrder2(1 To 8) As Integer
For i = 1 To 8
10 intOrder1(i) = Int(Rnd(i) * 8 + 1)
For j = 1 To i - 1
If intOrder1(i) = intOrder1(j) Then GoTo 10
Next j
Next i
For i = 1 To 8
20 intOrder2(i) = Int(Rnd(i) * 8 + 1)
For j = 1 To i - 1
If intOrder2(i) = intOrder2(j) Then GoTo 20
Next j
Next i
For i = 1 To 8
Command1(intOrder1(i) - 1).Left = 200 + (Command1(i - 1).Width + 100) * (i - 1)
Command2(intOrder2(i) - 1).Left = 200 + (Command2(i - 1).Width + 100) * (i - 1)
Next i
End Sub
Private Sub Command5_Click()
For i = 0 To 7
Command1(i).Left = 200 + (Command1(i).Width + 100) * i
Command2(i).Left = 200 + (Command2(i).Width + 100) * i
Next i
End Sub
Private Sub Form_Load()
Flag = True
Command3.Caption = "隐藏按钮名称"
Command4.Caption = "打乱按钮顺序"
Command5.Caption = "恢复按钮初始顺序"
Command3.Height = 400
Command4.Height = 400
Command5.Height = 400
Command3.Width = 1800
Command4.Width = 1800
Command5.Width = 1800
Command3.Left = 3000
Command4.Left = Command3.Left + 100 + Command3.Width
Command5.Left = Command3.Left + 100 + Command3.Width + 100 + Command4.Width
Command3.Top = 100 + Command1(0).Height + 100 + Command2(0).Height + 100
Command4.Top = 100 + Command1(0).Height + 100 + Command2(0).Height + 100
Command5.Top = 100 + Command1(0).Height + 100 + Command2(0).Height + 100
For i = 0 To 7
Command1(i).Visible = True
Command2(i).Visible = True
Flag1(i) = True
Flag2(i) = True
Command1(i).Caption = Chr(i + 65)
Command2(i).Caption = Chr(i + 65)
Command1(i).Height = 400
Command2(i).Height = 400
Command1(i).Width = 1000
Command2(i).Width = 1000
Command1(i).Left = 200 + (Command1(i).Width + 100) * i
Command2(i).Left = 200 + (Command2(i).Width + 100) * i
Command1(i).Top = 100
Command2(i).Top = 100 + Command1(i).Height + 100
Next i
Me.Width = 200 + Command1(0).Width * 8 + 100 * 7 + 200 + 100
Me.Height = 100 + Command1(0).Height + 100 + 2 * Command2(0).Height + 100 + Command3.Height + 100
End Sub
您的问题表述得不是很具体,我无端地设计了一些简单的功能,不知是否能对您有所帮助?
不完全是这样,差不多跟连连看一样的,就是我把命令按钮的caption设置为一样的,然后点击两个相同的命令按钮以后,他们就会消失
Private Sub Command1_Click(Index As Integer)
Static n%
Static k%
Static cption$
If n = 0 Then
k = Index
cption = Command1(k).Caption
n = n + 1
ElseIf Command1(Index).Caption = cption Then
Command1(Index).Visible = False
Command1(k).Visible = False
n = 0: k = -1: cption = ""
End If
End Sub
Private Sub Form_Load()
Dim myl%, myt%
Command1(0).Caption = 0
For i = 1 To 5
Load Command1(i)
myl = Command1(0).Width * (i Mod 3) + 50
myt = Command1(0).Height * (i \ 3) + 50
Command1(i).Left = Command1(0).Left + myl
Command1(i).Top = Command1(0).Top + myt
Command1(i).Caption = i Mod 3
Command1(i).Visible = True
Next
End Sub
谢谢!很好地帮到了我,真的非常感谢。不过能不能打乱顺序呢?
这个要用控件数组做
caption
caption