0

I have used this code to fix my problem, but when I use an InputBox it doesn't work.

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.

1 Answer 1

0

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?)? I cannot infer the meaning of the text (inside your code, I could use google tranlate, but).

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

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
Sign up to request clarification or add additional context in comments.

1 Comment

sorry, i just realize that my code is works but it case sensitive, so thanks for remind me.. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.