Solved

Help with logic

Posted on 2006-11-22
15
151 Views
Last Modified: 2010-04-23
I've got a control loop that has two parts to it (kind of a 'testing' app)
The first one, I've got licked, as it's radiobuttons that I'm looping through - need to see which 'one' is checked.
But, the second instance, where I have checkboxes, I need logic to make sure that 'all' the right ones are checked.

So far, I have:

for each ctl in controls
  if ctl is checkbox
    for each ans in answers
      if ctl.name=ans then
        'accumulate 'right' answers?

I'm kind of stuck - and I may be headed in the wrong direction.
Basically, I've got an array that contains 2 or more elements relating to the correct answer (that's why this is so easy with the radiobtn).

Any advice?
0
Comment
Question by:sirbounty
  • 8
  • 3
  • 2
  • +1
15 Comments
 
LVL 13

Expert Comment

by:newyuppie
Comment Utility
this line
if ctl is checkbox
i think you should change it to
if TypeOf ctl is checkbox
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
well, that was pretty much pseudo...the code is correct up to this point - I just need the logic for checking multiple values...
I was thinking some sort of boolean to indicate that all answers had been checked and only set it to true if the loop was complete.
0
 
LVL 13

Assisted Solution

by:newyuppie
newyuppie earned 50 total points
Comment Utility
is it possible in your thought logic to set the Tags of the controls at design time? for example, you know ahead of time which checkboxes contain the correct answer, so you put a Tag to them like "1" on correct ones, and "999" on wrong ones.
then loop through all the checkboxes, keeping a running count of the Convert.ToInt32(ctl.Tag.ToString()). if it is, say, exactly "2", then you have all the right ones selected. if it is more than 999, you have obviously selected a wrong one.
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
Might not be a bad idea-let me check on this...
The controls are created dynamically for each question, but I do set some properties on them...
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
This is what I'm trying...

       For Each ctl As Control In Controls
            If TypeOf ctl Is CheckBox Then
                If ctl.Tag = 1 Then
                    If CType(ctl, CheckBox).Checked = True Then intA += 1

Then outside my control loop, I test my array's length against intA
If they're equal, it's right, otherwise, it's not.

I'm going to do some more testing, but I think this will work...
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
If you are checking specific check boxes, why would it be important to loop through the controls, looking for the controls that are easy to reference without a loop?

Bob
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
Hi Bob - how do you mean?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I mean something like this:

   If Me.checkAnswer.Checked

versus, trying to find the CheckBox through a loop.

This question might be because I don't completely understand what you are looking for.

Bob
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
You're welcome to have a look, if you'd like...https://filedb.experts-exchange.com/incoming/ee-stuff/1482-quizzer.zip

I loop through records in a dataset.
For each one, I return the question, answers, and the correct answer(s).
The correct answer may be single (radiobutton) or multiple (checkboxes).
So, I loop through that array, if it has more than one element, and set the checkboxes tag to 1.
I think this would work, but if your suggestion offers better code, I'm eager to learn from you yet again! :^)
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 450 total points
Comment Utility
This problem sounds familiar... :)

Assuming that a "right" answer is all the correct check boxes selected and no wrong answers selected, something like this should work:

  Private Function CheckAnswers() As Boolean
    Dim isRight As Boolean = True

    For Each ctl As Control In Controls
      If TypeOf ctl Is CheckBox Then
        Dim chk As CheckBox = CType(ctl, CheckBox)
        isRight = isRight And ((chk.Tag = 1 AndAlso chk.Checked) OrElse (chk.Tag = 999 AndAlso Not chk.Checked))
        If Not isRight Then Exit For
      End If
    Next

    Return isRight
  End Function
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
Back from vacation - sorry for the delay.

Chaosian - I'm not really sure I follow what you're doing there...would you mind explaining it?

Does this
isRight = isRight And ((chk.Tag = 1 AndAlso chk.Checked) OrElse (chk.Tag = 999 AndAlso Not chk.Checked))
basically only continue checking if the current one found is true?
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
It's a boolean operation.

Essentially, it assumes the student is right to begin with (Dim isRight As Boolean = True).

Then we AND the current status with the expected result of the control.
If the tag =1, the control should be checked (chk.Tag = 1 AndAlso chk.Checked)
Otherwise, the control should not be checked (chk.Tag = 999 AndAlso Not chk.Checked)

Since any wrong answer means the student is wrong, we escape the routine if the answer is wrong after this check.
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
I think my concern is blending this with the radio button...I believe I'd need to default to a 'false' value for IsRight

Here's the function as I have it now...

    Private Function CheckAnswers() As Boolean
        Dim isRight As Boolean = True

        For Each ctl As Control In Controls
            If TypeOf ctl Is CheckBox Then
                Dim chk As CheckBox = CType(ctl, CheckBox)
                isRight = isRight And ((chk.Tag = 1 AndAlso chk.Checked) OrElse (chk.Tag = 999 AndAlso Not chk.Checked))
                If Not isRight Then Exit For
            ElseIf TypeOf ctl Is RadioButton Then
                If ctl.Tag = 1 Then
                    If CType(ctl, RadioButton).Checked = True Then
                        isRight = True
                        Exit For
                    End If
                End If
            End If
        Next
       
        Return isRight
    End Function
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
I'm going to go ahead and bump the points from 300 to 500.
I appreciate your help. :^)
0
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 450 total points
Comment Utility
Well, one of the problems with defaulting to false is that a Boolean AND is always false if any term in it is false. Boolean OR doesn't really fit.

I'd probably refine your radio button logic a little bit:

If ctl.Tag = 1 Then
  Return (CType(ctl, RadioButton).Checked = True)
End If

This will return false if the radio button with Tag = 1 is not checked, and true if it is checked. Should be right. (Yours was right too, but this forces the return of false if the radio button is not selected -- and since it's a radio button, that's the correct logic)

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now