CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 1999
    Location
    Baytown, TX, United States
    Posts
    23

    Can somebody answer this?

    I don't understand why all but one of the incorrects come out correct! Here is my code.

    [vbcode]
    Private Sub cmdCheck_Click()
    If optCal.Value = optSac.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optCol.Value = optDen.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optIll.Value = optSpr.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optOre.Value = optSal.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optWis.Value = optMad.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optSac.Value = optCal.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optDen.Value = optCol.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optIll.Value = optSpr.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optOre.Value = optSal.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    If optWis.Value = optMad.Value Then
    lblMsg.Caption = "Correct"
    Else
    lblMsg.Caption = "Incorrect"
    End If
    End Sub
    Private Sub optCal_Click()
    'assign correct capital to strCapital variable
    strCapital = optSac
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optCol_Click()
    'assign correct capital to strCapital variable
    strCapital = optDen
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optDen_Click()
    'assign capital choice to strChoice variable
    strChoice = optCol.Value
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optIll_Click()
    'assign correct capital to strCapital variable
    strCapital = optSpr
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optMad_Click()
    'assign capital choice to strChoice variable
    strChoice = optWis.Value
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optOre_Click()
    'assign correct capital to strCapital variable
    strCapital = optSal
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optSac_Click()
    'assign capital choice to strChoice variable
    strChoice = optCal
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optSal_Click()
    'assign capital choice to strChoice variable
    strChoice = optOre
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optSpr_Click()
    'assign capital choice to strChoice variable
    strChoice = optIll
    'clear label control
    lblMsg.Caption = " "
    End Sub

    Private Sub optWis_Click()
    'assign correct capital to strCapital variable
    strCapital = optMad
    'clear label control
    lblMsg.Caption = " "
    End Sub


    kazooie21

  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Can somebody answer this?

    I'm not sure what you're trying to achieve with this, but there are 2 instances when the lblMsg Label will display "Incorrect".

    When either optWis or optMad are selected.

    This is because of the way you have structured your IF.. Then Statements, the last IF Statement compares the values of optWis and optMad, as these are Option, (Radio), buttons only 1 will ever be selected, therefore optWis.Value = optMad.Value will always be False when either optMad or optWis is selected.

    Maybe you need to use Checkboxes?

    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  3. #3
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Can somebody answer this?

    This kind of IF..Then loops can be really confusing!!
    Check this:- First part of Your code:

    If optCal.Value = optSac.Value then
    lblMsg.Caption = "Correct"
    else
    lblMsg.Caption = "Incorrect"
    End If
    If optCol.Value = optDen.Value then
    lblMsg.Caption = "Correct"
    else
    lblMsg.Caption = "Incorrect"
    End If



    Take the case of optCal.Value = optSac.Value AND optCol.Value <> optDen.Value.. what will happen the first if will set the value of lblMsg to "Correct", which is immediately overwritten by "Incorrect", because of second IF.
    I dont know if any of these option buttons are enclosed in a frame, which will automatically force only one of them ON at a time.

    First clear up your "IF" loops . If there is some inherent priority in checking for IFs, then i will put a Exit sub after every "if", when ever a success occurs
    If there is no need for that, then i will check with combined IF statements.
    Still simpler, start off with a default (negation say) lblMsg = "Incorrect" and only on positive IF, i just set it to "Correct" like:

    lblMsg.Caption = "Incorrect"
    If optCal.Value = optSac.Value then
    lblMsg.Caption = "Correct"
    End If
    If optCol.Value = optDen.Value then
    lblMsg.Caption = "Correct"
    End If
    If optIll.Value = optSpr.Value then
    lblMsg.Caption = "Correct"
    End If
    If optOre.Value = optSal.Value then
    lblMsg.Caption = "Correct"
    End If



    Also, i notice that
    "If optCal.Value = optSac.Value Then"
    and some where down the line:
    "If optSac.Value = optCal.Value Then"
    Aren't they both same!! Order doesn't matter.

    RK

    RK

  4. #4
    Join Date
    Sep 1999
    Location
    Baytown, TX, United States
    Posts
    23

    Re: Can somebody answer this?

    I did it this way.

    [vbcode]
    lblMsg.Caption = "Correct"
    If optCal.Value <> optSac.Value then
    lblMsg.Caption = "Incorrect"
    etc...



    kazooie21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured