Metal Nerd
Metal Nerd

Reputation: 1

Check if item exist in arraylist using a combobox VB.Net

I'm new to this, I'm busy with my Pre Prac examples for my practical exam in VB.Net

We must create a Windows Form Application where you have 2 TextBoxes, 2 Comboboxes and 2 buttons to add the word typed in the textbox to the desired combo box. Now when you press the button to add a word and the textbox is empty, then a message is shown that it is empty, that's fine because thats easy, when the word is added to the combobox, the word is added to an arraylist, but when I enter the same word, a message must be shown that the word already exists in the arraylist, but it adds the same word to the combobox again

attached is my code so far, what am I missing

Public Class Form1
    Dim arAnim As ArrayList = New ArrayList
    Dim arVerb As ArrayList = New ArrayList

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub btnAddVerb_Click(sender As Object, e As EventArgs) Handles btnAddVerb.Click
    Dim result1 As DialogResult

    If txtBoxVerb.Text Is "" Then
        result1 = MessageBox.Show("Please type a word", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    Else
        If arVerb.Contains(cmbBoxVerb) Then
            result1 = MessageBox.Show("Word Exists", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

        Else
            cmbBoxVerb.Items.Add(txtBoxVerb.Text)
            arVerb.Add(cmbBoxVerb)
        End If
        txtBoxVerb.Clear()
    End If


End Sub
End Class 

Upvotes: 0

Views: 551

Answers (2)

Metal Nerd
Metal Nerd

Reputation: 1

Ok guys I figured it out

This is the code, it works as I want it to

Public Class Form1
Dim arAnim As ArrayList = New ArrayList
Dim arVerb As ArrayList = New ArrayList

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub btnAddVerb_Click(sender As Object, e As EventArgs) Handles btnAddVerb.Click
    Dim result1 As DialogResult

    If txtBoxVerb.Text Is "" Then
        result1 = MessageBox.Show("Please type a word", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    Else
        If arVerb.Contains(txtBoxVerb.Text) Then
            result1 = MessageBox.Show("Word Exists", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

        Else
            cmbBoxVerb.Items.Add(txtBoxVerb.Text)
            arVerb.Add(txtBoxVerb.Text)
        End If
        txtBoxVerb.Clear()
    End If
End Sub
End Class

Upvotes: 0

FloatingKiwi
FloatingKiwi

Reputation: 4506

You need to do:

If arVerb.Contains(txtBoxVerb.Text) Then ...

Instead of

If arVerb.Contains(cmbBoxVerb) Then

Upvotes: 1

Related Questions