回 帖 发 新 帖 刷新版面

主题:新手请教单选命令按钮

程序要求如下:单击"显示"命令按钮时,根据文本框中输入的内容、单选按钮和复选框状态在标签中显示出相应的信息。如:王洪男生爱好体育
我写了两种代码运行都可以:
(1)Private Sub Command1_Click()
If Check1.Value = 1 Then
       b = Check1.Caption
Else
       b = ""
End If

If Check2.Value = 1 Then
       c = Check2.Caption
Else
       c = ""
End If
If Option1.Value = True Then
    a = Option1.Caption
Else
    a = Option2.Caption
End If
Label2.Caption = Text1.Text  + a + b + c
End Sub
(2)
Dim a, b, c As String

Private Sub Check1_Click()
If Check1.Value = 1 Then
       b = Check1.Caption
Else
       b = ""
End If
      
End Sub

Private Sub Check2_Click()
 If Check2.Value = 1 Then
       c = Check2.Caption
    Else
       c = ""
    End If
    
End Sub

Private Sub Command1_Click()
     Label2.Caption = Text1.Text  + a + b + c
End Sub

Private Sub Option1_Click()
   a = Option1.Caption
End Sub

Private Sub Option2_Click()
   a = Option2.Caption
End Sub
我的问题:一开始运行程序时,如果用户不做任何选择就直接单击“显示”命令按钮,如果用第2种方式写的代码则标签上什么都不显示。但用第1种方法写的代码会在标签上显示“女生”,这个“女生”的值是怎么出来的呀?为什么不是“男生”呢?
[em10]

回复列表 (共2个回复)

沙发

看看你第一种方法的代码:

If Option1.Value = True Then
    a = Option1.Caption
Else
    a = Option2.Caption
End If

当你没有进行任何选择时,它执行的是Else后面的代码,也就是a = Option2.Caption
如果你不想出现这种情况,可修改如下:

If Option1.Value = True Then
    a = Option1.Caption
ElseIf Option2.Value = True Then
    a = Option2.Caption
End If

板凳


呵!原来是这样!太感谢你啦~~~~~[em5]

我来回复

您尚未登录,请登录后再回复。点此登录或注册