-
Jun 25th, 2018, 10:56 PM
#1
Thread Starter
Lively Member
[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.
-
Jun 25th, 2018, 11:46 PM
#2
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:
Public Class Model Public Property Name As String Public Property Code As String End Class
You can then create a list of instances of that type and bind them to your ComboBox, e.g.
vb.net Code:
Dim models = {New Model With {.Name = "PBGS01 (MR SENSOR)", .Code = "11150PP20000"}, New Model With {.Name = "PBGE01 (ECU BOARD)", .Code = "38700BD20101"}, New Model With {.Name = "PBGM01 (MCU BOARD)", .Code = "12110PP20000"}} With cmbModel .DisplayMember = "Name" .ValueMember = "Code" .DataSource = models End With
You can then compare the selection to the TextBox like this:
vb.net Code:
Dim modelCode = CStr(cmbModel.SelectedValue) If txtSerialNo.Text.StartsWith(modelCode) Then
-
Jun 26th, 2018, 12:58 AM
#3
Thread Starter
Lively Member
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 !
-
Jun 26th, 2018, 05:27 AM
#4
Re: How to compare combobox item to the first 10 digits value in textbox
Originally Posted by BONITO
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.
Originally Posted by BONITO
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.
-
Jun 26th, 2018, 02:48 PM
#5
Re: How to compare combobox item to the first 10 digits value in textbox
Originally Posted by jmcilhinney
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:
Public Class Model Public Property Name As String Public Property Code As String End Class
You can then create a list of instances of that type and bind them to your ComboBox, e.g.
vb.net Code:
Dim models = {New Model With {.Name = "PBGS01 (MR SENSOR)", .Code = "11150PP20000"}, New Model With {.Name = "PBGE01 (ECU BOARD)", .Code = "38700BD20101"}, New Model With {.Name = "PBGM01 (MCU BOARD)", .Code = "12110PP20000"}} With cmbModel .DisplayMember = "Name" .ValueMember = "Code" .DataSource = models End With
You can then compare the selection to the TextBox like this:
vb.net Code:
Dim modelCode = CStr(cmbModel.SelectedValue) 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?
-
Jun 26th, 2018, 05:38 PM
#6
Re: How to compare combobox item to the first 10 digits value in textbox
Originally Posted by ident
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|