如何检查输入框值是否在 VB 中的数组内
How to check if an inputbox value is within an array in VB
我已经使用 this 代码解决了我的问题,但是当我使用 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
如果我在 InputBox 上输入值,它会检查它是否在 MenuMakanan() 数组中,如果是,它会添加到 lisb_Pesanan.
您真的需要您正在使用的阵列吗?
特别是您使用它的方式(从某种意义上说,如果它不在菜单列表中,文本仍将被添加并且 lisbox 将包含比最初订购的少 1 项?)?我无法推断文本的含义(在您的代码中,我可以使用 google 翻译,但是)。
但是,您的代码已经在检查(区分大小写)。现在,如果你想要它不区分大小写,那么你可以用一个简单的 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
我已经使用 this 代码解决了我的问题,但是当我使用 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
如果我在 InputBox 上输入值,它会检查它是否在 MenuMakanan() 数组中,如果是,它会添加到 lisb_Pesanan.
您真的需要您正在使用的阵列吗?
特别是您使用它的方式(从某种意义上说,如果它不在菜单列表中,文本仍将被添加并且 lisbox 将包含比最初订购的少 1 项?)?我无法推断文本的含义(在您的代码中,我可以使用 google 翻译,但是)。
但是,您的代码已经在检查(区分大小写)。现在,如果你想要它不区分大小写,那么你可以用一个简单的 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