Results 1 to 4 of 4

Thread: checking if an ITEM already exists in a listview

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    17

    checking if an ITEM already exists in a listview

    Sorry wrong forum -> should be VS 2005 instead of classic vb

    hi,

    I'm currenly trying to load two textfiles in a listview.
    afterwards i'm updating one of the columns where i have the following problem

    i donnot know if a certain index of the item already exist

    so for instance i want to update a subitem but i donnot know if the item itself exists.

    the method
    listview.item(2).exists

    does not seem to exist

    is there another way ?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: checking if an ITEM already exists in a listview

    Quote Originally Posted by sjorsmiltenburg
    Sorry wrong forum -> should be VS 2005 instead of classic vb
    Moved

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: checking if an ITEM already exists in a listview

    I'm not quite sure what you're asking. Are you saying that you want to know if the number of items in a ListView is at least a certain number? If so then you use the Count property like you do with every collection:
    VB Code:
    1. If myListView.Items.Count >= 3 Then
    2.     'There is an item at index 2.
    3. End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    17

    Re: checking if an ITEM already exists in a listview

    sorry for the slow update, but this is my problem

    i'm trying to use the same function to initially fill in a column with the lines from the textfile and afterwards i'm trying to use the same function to update one of the columns (i want to switch textfile and therefore update all the lines in one of the columns)

    this is my code
    VB Code:
    1. Private Function AppendLinesToColumn(ByVal ListViewColumnIndex As Integer, ByVal FilePath As String, ByVal FirstItemIndex As Integer) As Boolean
    2.         Dim SR As StreamReader
    3.         Dim NextItemIndex As Integer = FirstItemIndex
    4.  
    5.         If File.Exists(FilePath) = True Then
    6.             SR = File.OpenText(FilePath)
    7.  
    8.             Dim MyText As String = SR.ReadToEnd
    9.             Dim MyLines() As String = Strings.Split(MyText, Environment.NewLine) 'splits on newline character, into a strings array containg all the lines
    10.             SR.Close()
    11.  
    12.             For Each strContents As String In MyLines
    13.                 'insert data of first column "nr"
    14.                 If ListView1.Items.Count <= NextItemIndex Then
    15.                     ListView1.Items.Add(NextItemIndex)
    16.                     ListView1.Items(NextItemIndex).SubItems.Add(strContents)
    17.                 Else if
    18.                     ListView1.Items(NextItemIndex).SubItems(ListViewColumnIndex).Text = strContents
    19.                 End If
    20.  
    21.                 'insert data in other column
    22.  
    23.                 ListView1.Update()
    24.                 NextItemIndex += 1
    25.             Next
    26.  
    27.             Return NextItemIndex - 1
    28.         Else
    29.             MsgBox("Error reading file " & FilePath & ".")
    30.             Return False
    31.         End If
    32.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width