-
Nov 28th, 2016, 04:05 PM
#1
Thread Starter
Lively Member
Or conditions
I have 2 radio buttons and 4 checkboxes. I am trying to create a check to see if any of the 6 are checked, if not, then display a message box. Below is my code. I have tried it with OrElse and Or. What should I use? Thank you ahead of time.
Code:
If rdoSeenApproved.Checked = False OrElse
rdoNotSeenApproved.Checked = False OrElse
chkSampleBlock.Checked = False OrElse
chkDoorDrawerSample.Checked = False OrElse
chkDoorOnlySample.Checked = False OrElse
chkInsetSample.Checked = False Then
MessageBox.Show(
"Please select an option for Sample",
"Sample Policy",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1)
End If
Thanks
Jeff
-
Nov 28th, 2016, 04:15 PM
#2
Re: Or conditions
If all of the Checkbox and RadioButton controls are located on the same parent then you can use LINQ to return the Count.
Assuming that they're located on the Form it would look something like this:
Code:
Dim radioCount As Integer = Me.Controls.OfType(Of RadioButton).Count(Function(rb) rb.Checked)
Dim checkboxCount As Integer = Me.Controls.OfType(of Checkbox).Count(Function(cb) cb.Checked)
If radioCount + checkboxCount = 0 Then
MessageBox.Show("Please select an option for Sample.", "Sample Policy", MessageBoxButtons.OK, MessageBoxIcon.Exlcamation)
End If
-
Nov 28th, 2016, 04:30 PM
#3
Hyperactive Member
Re: Or conditions
This works, but there are more elegant solutions of course.
Code:
dim SomethingIsChecked as boolean
If optionX.checked = true then SomethingIsChecked = true
If optionY.checked = true then SomethingIsChecked = true
If optionZ.checked = true then SomethingIsChecked = true
'bla bla ^
if SomethingIsChecked = true then MsgBox("Something is checked")
-
Nov 28th, 2016, 04:33 PM
#4
Re: Or conditions
Two RadioButton in the same container cannot be Unchecked at the same time (except if you did it at Form Shown event)
Code:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
rdoSeenApproved.Checked = False
rdoNotSeenApproved.Checked = False
End Sub
-
Nov 28th, 2016, 04:51 PM
#5
Re: Or conditions
Actually, the rule with RadioButtons is that not more than one can be checked, but it's fairly common to start with all unchecked. Once you check one, then one and only one will be checked.
By the way, the problem with the original question is that the logic is wrong. The If statement shown will tell you if any are NOT checked, but the question was whether any ARE checked. That would be written like this:
Code:
If rdoSeenApproved.Checked OrElse
rdoNotSeenApproved.Checked OrElse
chkSampleBlock.Checked OrElse
chkDoorDrawerSample.Checked OrElse
chkDoorOnlySample.Checked OrElse
chkInsetSample.Checked Then
Alternatively, it could be written like this:
Code:
If rdoSeenApproved.Checked = False AndAlso
rdoNotSeenApproved.Checked = False AndAlso
chkSampleBlock.Checked = False AndAlso
chkDoorDrawerSample.Checked = False AndAlso
chkDoorOnlySample.Checked = False AndAlso
chkInsetSample.Checked = False Then
You either have to check whether any are checked, or whether all are not checked. What you ended up with was a mashup of the two where you were checking whether any were not checked...which MUST be true if you have more than one radio button.
Also, you are right to use OrElse rather than Or, and AndAlso rather than And. You use Or and And mostly for bit manipulation and some rare specialized circumstances.
My usual boring signature: Nothing
-
Nov 28th, 2016, 04:54 PM
#6
Re: Or conditions
Originally Posted by jmyersnc
I have tried it with OrElse and Or. What should I use?
AndAlso (or And).
Currently you are testing if Any of them (OrElse) are Not-Checked, and you either want to test if All of them (AndAlso) are Not-Checked, or alternatively test if 'Any are Checked' is false.
It's a common mental block that we all have from time to time, and we just need to remember to check if we really mean And or Or.
edit: I was slow!
-
Nov 28th, 2016, 07:15 PM
#7
New Member
Re: Or conditions
Thank you! got it to work finally
-
Nov 29th, 2016, 09:08 AM
#8
Re: Or conditions
Perhaps you should ask a mod if they can combine your accounts.
-
Nov 29th, 2016, 11:34 AM
#9
Re: Or conditions
We can't. Administrators might be able to, but they might not, too.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|