Results 1 to 9 of 9

Thread: Or conditions

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2015
    Posts
    66

    Question 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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    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")

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Or conditions

    Quote Originally Posted by jmyersnc View Post
    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!

  7. #7
    New Member
    Join Date
    Nov 2016
    Posts
    8

    Re: Or conditions

    Thank you! got it to work finally

  8. #8
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Or conditions

    Perhaps you should ask a mod if they can combine your accounts.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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
  •  



Click Here to Expand Forum to Full Width