vb 我有几个命令按钮,点击其中的任意两个,如果他们的名称一样,则他们两个都消失,怎么写代码呢? 谢谢

我有几个命令按钮,点击其中的任意两个,如果他们的名称一样,则他们两个都消失,怎么写代码呢?谢谢... 我有几个命令按钮,点击其中的任意两个,如果他们的名称一样,则他们两个都消失,怎么写代码呢? 谢谢 展开
 我来答
数学与计算机编程
2016-07-13 · TA获得超过2159个赞
知道大有可为答主
回答量:2312
采纳率:80%
帮助的人:536万
展开全部

以上是窗体设计界面。

以下是程序运行界面:









程序代码如下:

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设置为一样的,然后点击两个相同的命令按钮以后,他们就会消失
红山人2008
2016-07-13 · TA获得超过4679个赞
知道大有可为答主
回答量:5699
采纳率:0%
帮助的人:3869万
展开全部
给个简单的供你参考。朋友。只要在窗体上添加一个command1并把它的index属性设置为0即可。
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
追问
谢谢!很好地帮到了我,真的非常感谢。不过能不能打乱顺序呢?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
sbj518
2016-07-12 · TA获得超过4.4万个赞
知道大有可为答主
回答量:1.1万
采纳率:81%
帮助的人:5129万
展开全部
你是想做配对游戏的吧?
这个要用控件数组做
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
syx54
2016-07-12 · TA获得超过7376个赞
知道大有可为答主
回答量:6567
采纳率:83%
帮助的人:2641万
展开全部
名称怎么可能一样呢?
追问
caption
caption
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式