设定了几个checkbox的控件在界面上,想要通过勾选计算出z的值,但计算结果明显不对,是否checkbox不能使用if语句?应该怎么改啊?我vb刚起步,请指教。
Dim z As Single
z = 0
If Check1.Value = 1 Then
z = z + 0.3
Else
If Check2.Value = 1 Then
z = z + 0.3
Else
If Check3.Value = 1 Then
z = z + 0.4
Else 
……
……
……

解决方案 »

  1.   

    enf if?应该是用了的,他说结果不对,没end if应该会报错
      

  2.   


    Private Sub Command1_Click()
    Dim z As Single
    z = 0
    If Check1.Value Then
    z = z + 0.3
    ElseIf Check2.Value Then
    z = z + 0.3
    ElseIf Check3.Value Then
    z = z + 0.4
    End If
    Debug.Print z   
    End Sub
      

  3.   

    If Check1.Value Then和If Check1.Value = 1 Then等价的啊
      

  4.   

    我把代码放这里,大家帮忙看下哪里不对好吗Private Sub Command1_Click()
    Dim z As Single
    z = 0If Check1.Value = 1 Then
    z = z + 0.3
    If Check2.Value = 1 Then
    z = z + 0.3If Check3.Value = 1 Then
    z = z + 0.4If Check4.Value = 1 Then
    z = z + 0.2
    If Check5.Value = 1 Then
    z = z + 0.2
    If Check6.Value = 1 Then
    z = z + 0.2End If
    End If
    End If
    End If
    End If
    End IfIf Option1.Value = True Then
    z = z + 0.1
    Else
    If Option2.Value = True Then
    z = z + 0.15
    Else
    If Option3.Value = True Then
    z = z + 0.2
    Else
    If Option4.Value = True Then
    z = z + 0.25
    End If
    End If
    End If
    End IfIf Option5.Value = True Then
    z = z + 0.25
    Else
    If Option6.Value = True Then
    z = z + 0.2
    If Option7.Value = True Then
    z = z + 0.15
    Else
    If Option8.Value = True Then
    z = z + 0
    End If
    End If
    End If
    End IfIf Option9.Value = True Then
    z = z + 0.25
    Else
    If Option10.Value = True Then
    z = z + 0
    End If
    End IfIf Option11.Value = True Then
    z = z + 0.1
    Else
    If Option12.Value = True Then
    z = z + 0.15
    Else
    If Option13.Value = True Then
    z = z + 0.2
    Else
    If Option14.Value = True Then
    z = z + 0.25
    End If
    End If
    End If
    End IfIf Option15.Value = True Then
    z = z + 0
    Else
    If Option16.Value = True Then
    z = z + 0.1
    Else
    If Option17.Value = True Then
    z = z + 0.2
    End If
    End If
    End If
    If Option18.Value = True Then
    z = z + 0
    Else
    If Option19.Value = True Then
    z = z + 0.1
    Else
    If Option20.Value = True Then
    z = z + 0.2
    End If
    End If
    End IfIf Option21.Value = True Then
    z = z + 0.6
    Else
    If Option22.Value = True Then
    z = z + 0.7
    Else
    If Option23.Value = True Then
    z = z + 0.8
    Else
    If Option24.Value = True Then
    z = z + 0.9
    Else
    If Option25.Value = True Then
    z = z + 1
    End If
    End If
    End If
    End If
    End If
    Text1.Text = z
    End Sub
      

  5.   

    看你的代码是不是自动计分的?你的OptionButton分组了没有?否则所有的OptionButton只能有一个被选中
      

  6.   

    OptionButton分组  是什么意思啊
      

  7.   

    我把optionbutton 放在不同的frame框里的,算不算分组啊
      

  8.   

    算分组的;
    不知道你的程序逻辑是想做什么,如果猜得没错的话,你是用checkbox表示多选,optionbutton表示单选,来算总分。如果是这样的话,那么这段代码就会有问题:If Check1.Value = 1 Then
    z = z + 0.3
    If Check2.Value = 1 Then
    z = z + 0.3If Check3.Value = 1 Then
    z = z + 0.4If Check4.Value = 1 Then
    z = z + 0.2
    If Check5.Value = 1 Then
    z = z + 0.2
    If Check6.Value = 1 Then
    z = z + 0.2End If
    End If
    End If
    End If
    End If
    End If
      

  9.   

    你的check1如果没有选中,将导致不会检查后面的check,也就不会加上相应的分值了
      

  10.   

    改成这样应是对了,你看看
    If Check2.Value = 1 Then
    z = z + 0.3
    End If
    If Check3.Value = 1 Then
    z = z + 0.4
    End If
    If Check4.Value = 1 Then
    z = z + 0.2
    End IfIf Check5.Value = 1 Then
    z = z + 0.2
    End IfIf Check6.Value = 1 Then
    z = z + 0.2
      

  11.   

    不错,如果你能保证你的check总是可用的,还不如把代码改为:
    z=z+check1*0.3+check2*0.3+check3*0.4+check4*0.2+check5*0.0.2+check6*0.2
      

  12.   

    如果你能保证你的check总是可用的???这句话是什么意思?
    难道是设定check值是false?按照你的该法那整个代码就是只有一句了:z=z+check1*0.3+check2*0.3+check3*0.4+check4*0.2+check5*0.0.2+check6*0.2+option1*0.2+
    option2*0.2……………………?
    是这样吗 
     
      

  13.   

    还是要看你的程序的逻辑的,
    保证check可用是因为checkbox不可用时value值为2;
    z=z+check1*0.3+check2*0.3+check3*0.4+check4*0.2+check5*0.0.2。
    这种写法只是取了个巧,因为1*0.3=0.3,0*0.3=0,所以可以保证你选中即加分,后面的option就不行了,因为option的value取值是true(-1,在VB中)和false(0),所以你得写成"-option1*0.2...",不过有些系统中true不一定是等于-1,所以后面这种写法得取决于实际的情况
      

  14.   

    最后那里是 
    Debug.Print z
    End Sub
     而且你 elseif连写
    还要end if
      

  15.   

    改成这样试试:Private Sub Command1_Click()
    Dim z As Single
    z = 0If Check1.Value = 1 Then
    z = z + 0.3
    End IfIf Check2.Value = 1 Then
    z = z + 0.3
    End IfIf Check3.Value = 1 Then
    z = z + 0.4
    End IfIf Check4.Value = 1 Then
    z = z + 0.2
    End IfIf Check5.Value = 1 Then
    z = z + 0.2
    End IfIf Check6.Value = 1 Then
    z = z + 0.2
    End IfIf Option1.Value = True Then
    z = z + 0.1
    End IfIf Option2.Value = True Then
    z = z + 0.15
    End IfIf Option3.Value = True Then
    z = z + 0.2
    End IfIf Option4.Value = True Then
    z = z + 0.25
    End IfIf Option5.Value = True Then
    z = z + 0.25
    End IfIf Option6.Value = True Then
    z = z + 0.2
    End IfIf Option7.Value = True Then
    z = z + 0.15
    End IfIf Option8.Value = True Then
    z = z + 0
    End IfIf Option9.Value = True Then
    z = z + 0.25
    End IfIf Option10.Value = True Then
    z = z + 0
    End IfIf Option11.Value = True Then
    z = z + 0.1
    End IfIf Option12.Value = True Then
    z = z + 0.15
    End IfIf Option13.Value = True Then
    z = z + 0.2
    End IfIf Option14.Value = True Then
    z = z + 0.25
    End IfIf Option15.Value = True Then
    z = z + 0
    End IfIf Option16.Value = True Then
    z = z + 0.1
    End IfIf Option17.Value = True Then
    z = z + 0.2
    End IfIf Option18.Value = True Then
    z = z + 0
    End IfIf Option19.Value = True Then
    z = z + 0.1
    End IfIf Option20.Value = True Then
    z = z + 0.2
    End IfIf Option21.Value = True Then
    z = z + 0.6
    End IfIf Option22.Value = True Then
    z = z + 0.7
    End IfIf Option23.Value = True Then
    z = z + 0.8
    End IfIf Option24.Value = True Then
    z = z + 0.9
    End IfIf Option25.Value = True Then
    z = z + 1
    End IfText1.Text = z
    End Sub