Results 1 to 6 of 6

Thread: [RESOLVED] How to compare combobox item to the first 10 digits value in textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Resolved [RESOLVED] How to compare combobox item to the first 10 digits value in textbox

    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.

    Code:
    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
    here is my form look likes.
    Name:  comparing cmbbox to textbox.PNG
Views: 1607
Size:  60.1 KB

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to compare combobox item to the first 10 digits value in textbox

    You should start by creating objects that have two properties: one for the value you want to display in the ComboBox and the other for the value you want to compare to the TextBox, e.g.
    vb.net Code:
    1. Public Class Model
    2.     Public Property Name As String
    3.     Public Property Code As String
    4. End Class
    You can then create a list of instances of that type and bind them to your ComboBox, e.g.
    vb.net Code:
    1. Dim models = {New Model With {.Name = "PBGS01 (MR SENSOR)", .Code = "11150PP20000"},
    2.               New Model With {.Name = "PBGE01 (ECU BOARD)", .Code = "38700BD20101"},
    3.               New Model With {.Name = "PBGM01 (MCU BOARD)", .Code = "12110PP20000"}}
    4.  
    5. With cmbModel
    6.     .DisplayMember = "Name"
    7.     .ValueMember = "Code"
    8.     .DataSource = models
    9. End With
    You can then compare the selection to the TextBox like this:
    vb.net Code:
    1. Dim modelCode = CStr(cmbModel.SelectedValue)
    2.  
    3. If txtSerialNo.Text.StartsWith(modelCode) Then

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How to compare combobox item to the first 10 digits value in textbox

    hi Sir jmcilhinney ,

    I got it ! thanks a lot ! ,
    how can i mark your answer as my answer ?
    or i will just make my thread question as RESOLVED?
    I tried to rate your post or add reputation to you but it says "You must spread some Reputation around before giving it to jmcilhinney again."

    thanks a lot again !

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

    Re: How to compare combobox item to the first 10 digits value in textbox

    Quote Originally Posted by BONITO View Post
    how can i mark your answer as my answer ?
    or i will just make my thread question as RESOLVED?
    That's all you need to do.
    Quote Originally Posted by BONITO View Post
    I tried to rate your post or add reputation to you but it says "You must spread some Reputation around before giving it to jmcilhinney again."
    It's the thought that counts.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: How to compare combobox item to the first 10 digits value in textbox

    Quote Originally Posted by jmcilhinney View Post
    You should start by creating objects that have two properties: one for the value you want to display in the ComboBox and the other for the value you want to compare to the TextBox, e.g.
    vb.net Code:
    1. Public Class Model
    2. Public Property Name As String
    3. Public Property Code As String
    4. End Class
    You can then create a list of instances of that type and bind them to your ComboBox, e.g.
    vb.net Code:
    1. Dim models = {New Model With {.Name = "PBGS01 (MR SENSOR)", .Code = "11150PP20000"},
    2. New Model With {.Name = "PBGE01 (ECU BOARD)", .Code = "38700BD20101"},
    3. New Model With {.Name = "PBGM01 (MCU BOARD)", .Code = "12110PP20000"}}
    4.  
    5. With cmbModel
    6. .DisplayMember = "Name"
    7. .ValueMember = "Code"
    8. .DataSource = models
    9. End With
    You can then compare the selection to the TextBox like this:
    vb.net Code:
    1. Dim modelCode = CStr(cmbModel.SelectedValue)
    2.  
    3. If txtSerialNo.Text.StartsWith(modelCode) Then
    Am I right thinking selected value returns an object reference so you need to cast it as type String in order to treat it as a String?

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

    Re: How to compare combobox item to the first 10 digits value in textbox

    Quote Originally Posted by ident View Post
    Am I right thinking selected value returns an object reference so you need to cast it as type String in order to treat it as a String?
    That is correct. The SelectedValue will return the contents of the property/column of the SelectedItem whose name is specified in the ValueMember. Because that property/column contents can be anything, SelectedValue must be type Object. It is indeed up to you to know what type it is and cast as that type.

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