|
-
Mar 25th, 2021, 12:21 PM
#1
Thread Starter
Addicted Member
[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.
-
Mar 25th, 2021, 12:25 PM
#2
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
-
Mar 25th, 2021, 12:28 PM
#3
Thread Starter
Addicted Member
Re: Combobox Multiple Conditions
OMG... you saved me. I thought I was losing my mind. Thank you for that small detail
-
Mar 25th, 2021, 08:13 PM
#4
Re: [RESOLVED] Combobox Multiple Conditions
You should be using OrElse rather than Or and AndAlso rather than And. Short-circuiting should be the default.
-
Mar 26th, 2021, 08:58 AM
#5
Thread Starter
Addicted Member
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 ?
-
Mar 26th, 2021, 09:50 AM
#6
Re: [RESOLVED] Combobox Multiple Conditions
 Originally Posted by mikeg71
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:
Dim str As String = Nothing
If str Is Nothing OrElse str.Length = 0 Then
Console.WriteLine("No text in str")
End If
If str Is Nothing Or str.Length = 0 Then
Console.WriteLine("No text in str")
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
-
Mar 26th, 2021, 12:24 PM
#7
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
-
Mar 26th, 2021, 01:10 PM
#8
Thread Starter
Addicted Member
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
-
Mar 26th, 2021, 08:35 PM
#9
Re: [RESOLVED] Combobox Multiple Conditions
 Originally Posted by dday9
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|