0

Hi.....
How do i set a listview to only add one item from a combobox?

Example:
In the text file containing the list are "Chicken, Beef, Buffalo", that already exists in listview is "Cow" then the listview will only add "Chicken, Buffalo".

i have here a code for adding items in a listview:

For Each ListedString As String In CType(iData.GetData(DataFormats.Text), String).Split(Environment.NewLine)
    For Each listItem As ListViewItem In MyListView1.Items
        If listItem.SubItems(2).Text = ListedString Then
            Return
        End If
    Next

    If UrlIsValid(ListedString) = True Then
        Dim lvi As New ListViewItem("Normal")
        lvi.SubItems.Add(MyListView1.Items.Count + 1)
        lvi.SubItems.Add(ListedString)
        lvi.SubItems.Add(GetFileNameFromURL(ListedString))
        MyListView1.Items.Add(lvi)
    End If
Next

Please, Help.

Thanks,

Bazzer Respeto

4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by bazzer14
Featured Replies
  • Unfortunately, there is no easy method to use for this. The Contains and ContainsKey (or even Find) methods do not search the listview for a given string. However, you can roll your own as follows: Private Function InListView(lvw, text) As Boolean For Each item As ListViewItem In lvw.items If item.Text … Read More

  • Whatever Jim has given it works...add few more lines of code Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click 'checked on button click Dim found As Boolean = InListView(ListView1, ComboBox1.Text) If found = True Then MsgBox("Exists") Else 'MsgBox("No") ListView1.Items.Add(ComboBox1.Text) End If End Sub Private Function InListView(lvw, text) As … Read More

1

Unfortunately, there is no easy method to use for this. The Contains and ContainsKey (or even Find) methods do not search the listview for a given string. However, you can roll your own as follows:

Private Function InListView(lvw, text) As Boolean

    For Each item As ListViewItem In lvw.items
        If item.Text = text Then Return True
    Next

    Return False

End Function

If you pass this function a listview control and a string it will return True if that string appears in any cell in the first column, False otherwise. For example

Dim found As Boolean = InListView(MyListView, "Buffalo")
0

I dont get your point. What would you like to do exactly? Tell us the pattern why not adding a Beef as well?

0

I think the OP is saying that if (for example) Buffalo has already been added to the listview then it should not be added again.

0

thanks for the solution, Jim.
As I have tried before, the methods that you given didn't work.

1

Whatever Jim has given it works...add few more lines of code

Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click 'checked on button click
    Dim found As Boolean = InListView(ListView1, ComboBox1.Text)
    If found = True Then
        MsgBox("Exists")
    Else
        'MsgBox("No")
        ListView1.Items.Add(ComboBox1.Text)
    End If
End Sub

Private Function InListView(lvw, text) As Boolean
    For Each item As ListViewItem In lvw.items
        If item.Text = text Then Return True
    Next
    Return False
End Function
0

Thank you, it's worked.

I place the Jim code in wrong place, :).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.