[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
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
Re: Combobox Multiple Conditions
OMG... you saved me. I thought I was losing my mind. Thank you for that small detail :)
Re: [RESOLVED] Combobox Multiple Conditions
You should be using OrElse rather than Or and AndAlso rather than And. Short-circuiting should be the default.
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 ?
Re: [RESOLVED] Combobox Multiple Conditions
Quote:
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
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
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
Re: [RESOLVED] Combobox Multiple Conditions
Quote:
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