Solved

Help with logic

Posted on 2006-11-22
15
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
  • +1
15 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17996160
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
ID: 17996198
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
ID: 17996310
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 67

Author Comment

by:sirbounty
ID: 17996461
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
ID: 17997174
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
ID: 17997293
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
ID: 17997421
Hi Bob - how do you mean?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17997484
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
ID: 17997595
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
ID: 18004251
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
ID: 18084519
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
ID: 18084538
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
ID: 18155234
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
ID: 18155235
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
ID: 18156038
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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