如何将组合框项目与文本框中的前10位数值进行比较 [英] How to compare combobox item to the first 10 digits value in textbox

查看:116
本文介绍了如何将组合框项目与文本框中的前10位数值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想创建一个程序来比较所选的组合框的项目到文本框的前10位数值,用户可以输入
最多24位数序列号。从文本框中。我将默认值设置为组合框的项目。示例请参阅下面的代码。 


我不知道如果使用什么,或者选择案例, 

请帮我提前谢谢。 


 Public Class Form1 
Private Sub btnSave_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnSave.Click
'Case 1
if cmbModel.SelectedItem.text< ;> ""然后
MsgBox("选择模型")
结束如果
'案例2
如果cmbModel.SelectedItem.text =" PBGS01(MR SENSOR)"那么
如果txtSerialNo.Text =" 11150PP20000%"然后
MsgBox("成功")

其他
MsgBox("模型和序列号不匹配!")
结束如果
结束如果
'情况3
如果cmbModel.SelectedItem.text =" PBGE01(ECU BOARD)"然后
如果txtSerialNo.Text =" 38700BD20101%"然后
MsgBox("成功")

否则
MsgBox("你选择错误的模型")
结束如果
结束如果
'case 4
如果cmbModel.SelectedItem.text =" PBGM01(MCU BOARD)"那么
如果txtSerialNo.Text =" 12110PP20000%"然后
MsgBox("成功")

否则
MsgBox("你选择错误的模型")
结束如果
结束如果
结束次级
结束等级

先谢谢你。

解决方案




我想制作一个程序,将所选的组合框项目与文本框的前10位数值进行比较


你需要使用Substring方法。 这将允许您仅使用比较中文本框值的前十位数字。在尝试获取前十位数字之前,您应检查文本框条目的长度是否至少为十位数。 
在开始测试之前,您应该只需要进行长度测试并获取子字符串一次。请参阅:

https:// msdn。 microsoft.com/en-us/library/aka44szs%28v=vs.110%29.aspx


使用案例并没有太大区别或多个IF语句。 有什么区别的是创建一个由模型名称和序列组成的类,并为每个模型创建该类的实例。 如果您执行
,则可以创建这些类实例的集合(例如List),并迭代该列表以进行检查。 您的代码将如下所示(如果
模型模型实例的列表):

 

对于模型中的每个M作为模型
   如果cmbModel.SelectedItem.text = M.Name则
        如果SerialNumber =  M.Serial则
             MsgBox("成功")
         否则
             MsgBox("模型和序列号不匹配!")
        结束如果
        退出对于    结束如果
下一步








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.

解决方案


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


这篇关于如何将组合框项目与文本框中的前10位数值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆