IF/ELSE STATEMENT DOESN'T WORK

Babe Zaharias

Total Post:19

Points:133
Posted by  Babe Zaharias
VB.Net 
 1512  View(s)
Ratings:
Rate this:
Hi Expert, 

I have a ComboBox containing 5 values. An empty line, "Credit", "Debit", "Credit Settlement", and "Debit Settlement". I am trying to perform an error validation on it. If the user types anything in the ComboBox or if the selection of the empty string is made, I want to trigger an error. This is what I have now but it doesn't work. Any ideas?

If cboTypeRes.Text.Trim = "" or cboTypeRes.Text.Trim <> "Debit" Or cboTypeRes.Text.Trim <> "Credit" Or cboTypeRes.Text.Trim = "Debit Settlement" Or 

cboTypeRes.Text.Trim = "Credit Settlement" Then

Thanks in advance for any recommendations or solutions.

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: IF/ELSE Statement doesn't work

    Hi Babe,


    Check if the selected item is contained in the Items collection

    if cboTypesRes.SelectedItem Is Nothing Then
        Console.WriteLine("Error")
    Else
        Dim curText = cboTypesRes.SelectedItem.ToString().Trim()
        if curText.Length = 0 OrElse Not cboTypesRes.Items.Contains(curText) Then
            Console.WriteLine("Error")
        End If
    End If

    A little update:

    The SelectedItem property could be Nothing when the user types the word directly or you user could types the word with a lower case first letter.
    If you find these scenarios acceptable then you could try with this change

    Dim curText = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(cboTypeRes.Text)
    if curText.Length = 0 OrElse Not cbo.Items.Contains(curText) Then
        Console.WriteLine("Error")
    End If

     I hope it helpful for you.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!