We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,610 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Listview Limitation

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
20 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
bazzer14
Newbie Poster
12 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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")
Reverend Jim
Illigitimae non carborundum
Moderator
3,748 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33

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

Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 15

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.

Reverend Jim
Illigitimae non carborundum
Moderator
3,748 posts since Aug 2010
Reputation Points: 585
Solved Threads: 470
Skill Endorsements: 33

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

bazzer14
Newbie Poster
12 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 39
Solved Threads: 77
Skill Endorsements: 7

Thank you, it's worked.

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

bazzer14
Newbie Poster
12 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Reverend Jim, poojavb and Mitja Bonca

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0980 seconds using 2.72MB