Results 1 to 9 of 9

Thread: [RESOLVED] Combobox Multiple Conditions

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Resolved [RESOLVED] Combobox Multiple Conditions

    Hi All,

    Could someone please tell me where I am going wrong with this? I have tried a few different scenarios of which none are working. The only value when selected that works is the first one "Winding". I need the YESNoCancel message to show up ONLY if none of these 3 selections have been chosen in the combobox. I have tried "If Not....Or If Not...Or If Not" , "If Not...If Not...If Not" , "If Not...OrElse...OrElse"


    Code:
    If Not CmbProcess.SelectedItem = "Winding" Or CmbProcess.SelectedItem = "Gap Seal" Or CmbProcess.SelectedItem = "Bonding" Then
    Dim result As DialogResult = MessageBox.Show("   Did this part FAIL?", "", MessageBoxButtons.YesNoCancel)
                If result = DialogResult.Cancel Then
                    testResults = "Cancelled"
                    Exit Sub
                ElseIf result = DialogResult.No Then
                    testResults = "Pass"
                    rButton = Panel1.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
                    BtnDrop.PerformClick()
                ElseIf result = DialogResult.Yes Then
                    Me.Height = 160
                    Me.Top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - Me.Height * 1.6
                    testResults = "Fail"
                End If
            End If
    Last edited by mikeg71; Mar 25th, 2021 at 12:24 PM.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Combobox Multiple Conditions

    It should work if you enclose the conditions after Not with parenthesis:

    Code:
    If Not (CmbProcess.SelectedItem = "Winding" Or CmbProcess.SelectedItem = "Gap Seal" Or CmbProcess.SelectedItem = "Bonding") Then

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: Combobox Multiple Conditions

    OMG... you saved me. I thought I was losing my mind. Thank you for that small detail

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Combobox Multiple Conditions

    You should be using OrElse rather than Or and AndAlso rather than And. Short-circuiting should be the default.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: [RESOLVED] Combobox Multiple Conditions

    Thanks for the response jmcilhinney, I will look at using it this way. What is the difference with OrElse vs Or ?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Combobox Multiple Conditions

    Quote Originally Posted by mikeg71 View Post
    Thanks for the response jmcilhinney, I will look at using it this way. What is the difference with OrElse vs Or ?
    Try this code:
    vb.net Code:
    1. Dim str As String = Nothing
    2.  
    3. If str Is Nothing OrElse str.Length = 0 Then
    4.     Console.WriteLine("No text in str")
    5. End If
    6.  
    7. If str Is Nothing Or str.Length = 0 Then
    8.     Console.WriteLine("No text in str")
    9. End If
    to see what happens, then read the relevant documentation:

    https://docs.microsoft.com/en-us/dot...relse-operator
    https://docs.microsoft.com/en-us/dot...rs/or-operator
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: [RESOLVED] Combobox Multiple Conditions

    To elaborate on what JMcIlhinney suggested, the Or operator will evaluate the right-hand conditional check regardless of if the left-hand evaluated to True. Contrast this to the OrElse statement where it will not bother to evaluate the right-hand condition if the left-hand condition is True.

    The reason why the second If/Then statement would throw an exception in JMcIlhinney's example is because event though str Is Nothing is True, because it is using the Or operator, it will still evaluate str.Length = 0, but because [str] is nothing you will get a NullReferenceException.

    It is a subtle, but important distinction.

    Also, something that I will usually do when evaluating multiple conditions is to store them in an array and then use IndexOf. I find that it is generally shorter than writing out each comparison. E.g.:
    Code:
    Dim values = { "Winding", "Gap Seal", "Bonding" }
    Dim selectedItemInValues = Array.IndexOf(values, CmbProcess.SelectedItem) > -1
    If (Not selectedItemInValues) Then
        ' ...
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: [RESOLVED] Combobox Multiple Conditions

    These are great learning experiences you guys have provided... I greatly appreciate the time spent on it. I plan to do some good testing with these examples. Thanks again

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Combobox Multiple Conditions

    Quote Originally Posted by dday9 View Post
    Also, something that I will usually do when evaluating multiple conditions is to store them in an array and then use IndexOf. I find that it is generally shorter than writing out each comparison. E.g.:
    Code:
    Dim values = { "Winding", "Gap Seal", "Bonding" }
    Dim selectedItemInValues = Array.IndexOf(values, CmbProcess.SelectedItem) > -1
    If (Not selectedItemInValues) Then
        ' ...
    End If
    These days, it would be more appropriate to use Enumerable.Contains:
    Code:
    Dim values = { "Winding", "Gap Seal", "Bonding" }
    
    If values.Contains(CStr(CmbProcess.SelectedItem)) Then
        ' ...
    End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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