If statements that look at more then one variable

duelchamp1

Member
Joined
Aug 9, 2006
Messages
13
Programming Experience
Beginner
Hey

Basicaly I am trying to use if statments to make a button that will look at a set of variables and then based on those variables make a certain label visible e.g.

if radIntro1.checked = true + if radBody3.checked = true +
if radconc1.checked = true Then

lblMark3.visible = True

So far I have made this sort of work by using 27 seperate if statments like the one above. one for evary possible combination of
1 2 3
1 2 3
1 2 3

The problem is that when I click the button sometimes it will bring make a label visible that is not the one I had specified.

Can any one tell me why it does this or if there is a better way of getting the button to do what I want.

Thanks

Duelchamp1:)
 
from what ive observed in your code...
you can use Logical Operators to make the checking a single if statement...:confused:
 
First declare a global variable : private chkStatus as integer
In the changed event of each of the radio buttons insert the following:-

VB.NET:
if radiobutton1.checked= true then
chkStatus=chkStatus or 1
else
chkStatus=chkStatus and 6
end if
 
if radiobutton2.checked= true then
chkStatus=chkStatus or 2
else
chkStatus=chkStatus and 5
end if
 
if radiobutton3.checked= true then
chkStatus=chkStatus or 4
else
chkStatus=chkStatus and 3
end if
then use a select case block to determin the result
VB.NET:
private sub chkStatAction
select case chkStatus
case 1
rb1 only actions
case 2
rb2 only actions
case 3
rb1 and rb2 actions
case 4
rb3 only actions
case etc....
end select
end sub
I hope you can follow all this. Essentialy your are translating the values of each of the radio buttons to a binary flag held as decimal integer using bitwise logical operators and then determining which action to take based on which bits of chkStatus are set.
 
Last edited by a moderator:
instead of:
VB.NET:
if radIntro1.checked = true + if radBody3.checked = true +
if radconc1.checked = true Then
 
lblMark3.visible = True

try:

VB.NET:
if radIntro1.checked AND radBody3.checked and radconc1.checked Then
   lblMark3.visible = true
end if
 
Arkette is on track, but should have used an enumeration, this also clarifies the combinations - there are only 8 possible combination of three checkboxes (not 27, because the order doesn't matter, 'ab' = 'ba'). The enumeration should be set up as bitflags including 'none' option and combined flags. You combine two flags with Or operator (can also use + operator in enum definition), toggle with Xor. Code example using three checkboxes and a button:
VB.NET:
<Flags()> Enum checkstates
  none = 0
  a = 1
  b = 2
  c = 4
  ab = a Or b
  ac = a Or c
  bc = b Or c
  abc = a Or b Or c
End Enum
 
Dim checkstate As checkstates = checkstates.none 'all checkboxes unchecked initally
 
 
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CheckBox1.CheckedChanged
  checkstate = checkstate Xor checkstates.a
End Sub
 
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CheckBox2.CheckedChanged
  checkstate = checkstate Xor checkstates.b
End Sub
 
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CheckBox3.CheckedChanged
  checkstate = checkstate Xor checkstates.c
End Sub
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
  MsgBox(checkstate.ToString)
  'if you need specific action do each case
  Select Case checkstate
  Case checkstates.none
    'do..
  Case checkstates.a
    'do..
  Case checkstates.b
  Case checkstates.c
  Case checkstates.ab
  Case checkstates.ac
  Case checkstates.bc
  Case checkstates.abc
  End Select
End Sub
If you want to review the full enumerations members and values then here is some code:
VB.NET:
Dim names() As String = [Enum].GetNames(GetType(checkstates))
Dim values() As Integer = [Enum].GetValues(GetType(checkstates))
Dim sb As New System.Text.StringBuilder
For i As Integer = 0 To names.Length - 1
  sb.AppendLine(String.Format("{1} {0}", names(i), values(i)))
Next
MsgBox(sb.ToString)
The output is:
0 none
1 a
2 b
3 ab
4 c
5 ac
6 bc
7 abc
 
but should have used an enumeration,
Your absolutely right here , I just didn't want to muddy the waters too much. I would take issue a little over the use of toggling the values though. I feel there must always be a chance that values could become out of sync if for example they are manipulated in some other area of code further down the line.
 
I would say there is no chance that variable (checkstate) is manipulated elsewhere in application :) why would I do that? I already made the design decision of that variable should be reflecting the state of those three checkboxes. If I change that variable elsewhere then I'm breaking the design, suddenly the variable supposingly reflects the state of the three checkboxes plus something else? There is no reason to change the value of that variable elsewhere, even if it is possible for you as a programmer programming the application to do whatever you wish. The value can be read out and checked for any flag combination anywhere, but it is only the CheckBoxes CheckedChanged event handlers that manipulates the value. So it is not possible for checkstate variable to get out of sync unless you deliberately make an programming error.
 
Hey guys thanks for all your help on this just to clarify though. There are 27 different posibilities because the radio buttons are split into three groups of three and the order in which you select radio buttons from each group directly affects what label I want to show. :)
 
The priciple set out above can be applied to any number of radiobuttons or groups of radio buttons you just need calculate using the appropriate boolean algebra for the required number of bits.
 
duelchamp1,

Ooh, it's radioboxes, and it's 9 of them... that wasn't clear to me. Then flags enum is perhaps not the best idea after all. The request is still not clear, if there are 3 radios in a group there is only one out of three possible values in that group, you can only get one value from this group. For the three groups total that gives 27 combination possible. See table, abc is the three groups:
abc abc abc
111 211 311
112 212 312
113 213 313
121 221 321
122 222 322
123 223 323
131 231 331
132 232 332
133 233 333 =27 combinations
If you say that the order of which a group is selected matters (ie group order abc acb bac bca cab cba) then there are 6*27=162 possible results of those three values. Even if the order doesn't matter and there is 27 possible combinations, why would you calculate this into one single result? I doubt the user is able to comprehend the difference of a compound value that consist of 27 elements. Further about your clarity in request, maybe you should start to tell exactly what you are trying to do, instead of asking how to calculate some obscure result nobody knows why you want to calculate?
 
Hey

Ok what I am trying to do is build a learning resource program for English students. The basic Idea is that the user must assemble an essay buy selecting 3 preprogrammed paragraph such as a introduction a body and a conclusion that they want to use in their essay they do this by selecting the radio buttons that correlate with Paragraphs they want. There are three groups of radio buttons one group for each section of an essay and their are three different paragraphs (or three radio buttons) in each group to choose from. Once the user has compiled their essay they press the mark button and this is where the problem is I want the mark button to look at which radio button has been selected from each group and then I want it to give a mark of either Achieved, Merit, or Excellence by making a label visible (the label has the mark in it) For example


1,1,1 Achived
1,3,1 Merit
2,1,2 Excellance

I hope this makes things a little clearer

Thanks

Nigel:)
 
That is pretty easy both to check each group and give marks to different combinations, see example:
VB.NET:
'group A
Dim result As String = ""
If radA1.checked Then
   result = "1"
ElseIf radA2.checked Then
   result = "2"
Else
   result = "3"
End If
 
'group B
If radB1.checked Then
   result &= "1"
ElseIf radB2.checked Then
   result &= "2"
Else
   result &= "3"
End If
 
'group C
If radC1.checked Then
   result &= "1"
ElseIf radC2.checked Then
   result &= "2"
Else
   result &= "3"
End If
 
'check result
Select Case result
Case "111", "222", "333", "123", "321"
   MsgBox("excellent")
Case "131"
   MsgBox("good enough")
Case Else
   MsgBox("not that good")
End Select
 
Back
Top