简体   繁体   English

如何检查输入框值是否在VB中的数组内

[英]How to check if an inputbox value is within an array in VB

I have used this code to fix my problem, but when I use an InputBox it doesn't work. 我已经使用代码解决了我的问题,但是当我使用InputBox时,它不起作用。

Private MenuMakanan() As String = {"Sate Kambing", "Sate Ayam", "Gulai", "Nasi"}

Private Sub btn_Pesan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Pesan.Click
    Dim myMenu() As String = {}
    Dim jml As Short = InputBox("Mau Pesan Berapa Makanan ? ")
    If jml > 0 Then
        ReDim myMenu(jml - 1)
        For s = 0 To UBound(myMenu)
            myMenu(s) = InputBox("Mau Pesan Makanan Apa ?")
            If MenuMakanan.Contains(myMenu(s)) Then
                lisb_Pesanan.Items.Add(myMenu(s))
            End If
        Next
        MessageBox.Show("Terima Kasih" & vbCrLf & "Mohon tunggu sejenak", "Pesanan", MessageBoxButtons.OK)
    End If
End Sub

If I enter value on InputBox, it will check that it is within the MenuMakanan() array or not, if yes it will add to lisb_Pesanan. 如果在InputBox上输入值,它将检查它是否在MenuMakanan()数组内,如果是,它将添加到lisb_Pesanan。

Do you really need the array you are working with? 您是否真的需要使用的阵列?

Especially the way you are working with it ( in the sense that, in case it's not in the menu list, the text will still be added and the lisbox will contain 1 item less than originally ordered?)? 尤其是您使用它的方式(从某种意义上说,如果它不在菜单列表中,那么仍然会添加文本,并且lisbox中包含的项目比原始订购的少1个)? I cannot infer the meaning of the text (inside your code, I could use google tranlate, but). 我无法推断文字的含义(在您的代码中,我可以使用google tranlate,但是)。

However, your code was checking already (just case sensitive). 但是,您的代码已经在检查(区分大小写)。 Now, if you want it Case Insensitive, then you could just go through it with a simple Where statement like 现在,如果您希望它不区分大小写,那么您可以使用一个简单的Where语句来处理它,例如

Private MenuMakanan() As String = {"Sate Kambing", "Sate Ayam", "Gulai", "Nasi"}

Private Sub btn_Pesan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Pesan.Click
    Dim jml As Short = 0
    Dim requested As String = Microsoft.VisualBasic.InputBox("Mau Pesan Berapa Makanan ? ")

    If Not Short.TryParse(requested, jml) OrElse jml <= 0 Then
        ' throw enter a positive number...
        Return
    End If
    If jml > 0 Then
        Do
            Dim menuText As String = Microsoft.VisualBasic.InputBox("Mau Pesan Makanan Apa ?")
            Dim match As String = MenuMakanan.Where(Function(i) String.Equals(i, menuText, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault() ' contains either the matching menuItem or the default string (nothing)
            If Not String.IsNullOrWhiteSpace(match) Then
                lisb_Pesanan.Items.Add(match)
                jml -= 1
            Else
                ' incorrect entry, throw a message, let the user re-enter the value
                MessageBox.Show("Warning ...", "Warning ...", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Loop While jml > 0
        MessageBox.Show("Terima Kasih" & vbCrLf & "Mohon tunggu sejenak", "Pesanan", MessageBoxButtons.OK)
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM