none
How to compare combobox item to the first 10 digits value in textbox RRS feed

  • Question

  • Hi All,

    I want to make a program that will compare the selected items of combo box to the first 10 digits value of text box, user can input up to 24 digits serial no. from the text box. and I will set default value to the items of combo box. example see my code below. 
    I don't know what to use if else , or select case, 
    Please help me thank you in advance. 

    Public Class Form1 
        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 
            ' Case 1 
            If cmbModel.SelectedItem.text <> "" Then 
                MsgBox("Choose model") 
            End If 
            ' case 2 
            If cmbModel.SelectedItem.text = "PBGS01 (MR SENSOR)" Then 
                If txtSerialNo.Text = "11150PP20000%" Then 
                    MsgBox("Success") 
     
                Else 
                    MsgBox("Model and Serial no not match !") 
                End If 
            End If 
            ' case 3 
            If cmbModel.SelectedItem.text = "PBGE01 (ECU BOARD)" Then 
                If txtSerialNo.Text = "38700BD20101%" Then 
                    MsgBox("Success") 
     
                Else 
                    MsgBox("You select wrong Model") 
                End If 
            End If 
            ' case 4 
            If cmbModel.SelectedItem.text = "PBGM01 (MCU BOARD)" Then 
                If txtSerialNo.Text = "12110PP20000%" Then 
                    MsgBox("Success") 
     
                Else 
                    MsgBox("You select wrong Model") 
                End If 
            End If 
        End Sub 
    End Class

    thank you in advanced.

    Tuesday, June 26, 2018 3:58 AM

All replies


  • I want to make a program that will compare the selected items of combo box to the first 10 digits value of text box

    You need to use the Substring method.  This will allow you to use the first ten digits only of the text box value in the comparison. You should check that the text box entry is at least ten digits long before attempting to get the first ten digits.  You should only need to do the length test and get the substring once, before you start testing. See:
    https://msdn.microsoft.com/en-us/library/aka44szs%28v=vs.110%29.aspx

    It doesn't make a lot of difference whether you use a Case or multiple IF statements.  What will make a difference is to create a class that consists of the model name and the serial, and create an instance of that class for each model.  If you do that then you could create a collection (such as a List) of these class instances, and iterate over that list to do the checking.  Your code would then look like this (if Models is the List of Model instances):

    For Each M As Model in Models     If cmbModel.SelectedItem.text = M.Name Then          If SerialNumber = M.Serial Then              MsgBox("Success")           Else              MsgBox("Model and Serial no not match !")          End If          Exit For
         End If Next


    Tuesday, June 26, 2018 5:06 AM
  • Hi

    If you want to use select case method, the code is as follows:

    Public Class Form1
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
            If cmbModel.SelectedIndex = -1 Then
                MsgBox("Choose model")
            Else
                Dim com As String = cmbModel.SelectedItem.ToString
                Select Case com
                    Case "PBGS01 (MR SENSOR)"
                        If txtSerialNo.Text = "11150PP20000%" Then
                            MsgBox("Success")
    
                        Else
                            MsgBox("Model and Serial no not match !")
                        End If
                    Case "PBGE01 (ECU BOARD)"
                        If txtSerialNo.Text = "38700BD20101%" Then
                            MsgBox("Success")
    
                        Else
                            MsgBox("Model and Serial no not match !")
                        End If
                    Case "PBGM01 (MCU BOARD)"
                        If txtSerialNo.Text = "12110PP20000%" Then
                            MsgBox("Success")
    
                        Else
                            MsgBox("Model and Serial no not match !")
                        End If
                End Select
            End If
    
    
        End Sub
    End Class

    Best Regards

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 26, 2018 7:23 AM
  • By the way,cmbModel.SelectedItem.text?The correct code should be

    cmbModel.SelectedItem.ToString()

    Best Regards

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 26, 2018 8:44 AM