Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ListView items in VB.NET 2005

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
How do I add items to a multi-column listview control in VB.NET 2005? I am getting a list of files using the 486 extension in a specific directory, reading two bits of data from them one at a time, and then attempting to post that data in pairs into a listview control. The code is pasted below, but I keep getting the error pasted below the code. Thanks in advance for any advice.
[code]
'This method handles initialization upon startup
Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim loopInt As Integer
Dim fileString As String()
Dim itemLVI As New ListViewItem

'Set the program status
Me.mainToolStripStatusLabel.Text() = "Loading configurations..."
Me.Cursor() = Cursors.WaitCursor()

'Get an array of the configuration filenames
fileString = Directory.GetFiles(".Config", "*.486")

'Loop through and load the individual configuration names
Me.mainToolStripProgressBar.Maximum() = fileString.Length()
Me.mainToolStripProgressBar.Minimum() = 0
Me.mainToolStripProgressBar.Value() = 0
For loopInt = 0 To fileString.Length() Step 1
itemLVI.Name() = "Demo"
itemLVI.SubItems.Add("8086")
Me.mainListView.Items.Add(itemLVI)
Me.mainToolStripProgressBar.Value = loopInt
Next loopInt

'Populate the listview

'Reset the status and return
Me.Cursor() = Cursors.Default()
Me.mainToolStripStatusLabel.Text() = "Idle"
Me.mainToolStripProgressBar.Maximum() = 100
Me.mainToolStripProgressBar.Minimum() = 0
Me.mainToolStripProgressBar.Value() = 0
End Sub
[/code]
[code]
Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.
[/code]

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    Alright I figured out one problem, but still have another. I have to use "itemLVI.Text()", not "itemLVI.Name()". However, now I have the problem of it claiming that I am attempting to add the same item in two places. The updated loop code is below. Do I have to create a new ListViewItem for every item that I add or something? If so, how the heck would I create that array based on varying amounts of files that could be returned?
    [code]
    'This method generates the list of applications
    Private Sub GenerateList(ByVal modeShort As Short)
    Dim loopInt As Integer
    Dim fileString As String()
    Dim itemLVI As New ListViewItem

    'Set the program status
    Me.mainToolStripStatusLabel.Text() = "Loading configurations..."
    Me.Cursor() = Cursors.WaitCursor()

    'Get an array of the configuration filenames
    fileString = Directory.GetFiles(".Config", "*.486")

    'Loop through and load the individual configuration names
    Me.mainToolStripProgressBar.Maximum() = fileString.Length()
    Me.mainToolStripProgressBar.Minimum() = 0
    Me.mainToolStripProgressBar.Value() = 0
    For loopInt = 0 To fileString.Length() Step 1
    itemLVI.Text() = "Demo" & loopInt.ToString()
    itemLVI.SubItems.Add("808" & loopInt.ToString())
    Me.mainListView.Items.Add(itemLVI)
    Me.mainToolStripProgressBar.Value = loopInt
    itemLVI.SubItems.Clear()
    Next loopInt

    'Reset the status and return
    Me.Cursor() = Cursors.Default()
    Me.mainComboBox.SelectedIndex() = 0
    Me.mainToolStripStatusLabel.Text() = "Idle"
    Me.mainToolStripProgressBar.Maximum() = 100
    Me.mainToolStripProgressBar.Minimum() = 0
    Me.mainToolStripProgressBar.Value() = 0
    End Sub
    [/code]
    Thanks for any help you can offer, and again, I merely want to read in two strings of data from each file and post it.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    Still having a problem with this. I generate a list of filenames in a specific directory, then loop through them, reading in a name and an architecture, and add that data to the listview. The problem is that the listview sorts the items alphabetically, which I need, but causes problems. You see, I have three testing files to play with, named "Test.xxx", "Test2.xxx", and "Test3.xxx". The way Windows lists the files is for "Test.xxx" to come after the other two, which is where my problem lies. "Test3.xxx" contains a label that starts with numbers instead of letters, so the listview instantly lists it at the top of the list. This means that the other two get shifted from locations 0 and 1 to 1 and 2. Now how the heck do I locate the new item? I can't afford to loop through checking "ToString()" comparisons because the listview will probably wind-up with a few thousand entries when the program is completed, and that would destroy any kind of performance. Below is my current code.
    [code]
    'Get an array of the configuration filenames
    listString = Directory.GetFiles(".Config", "*.xxx")

    'Loop through and load the individual configuration names
    Me.mainToolStripProgressBar.Maximum() = listString.Length()
    Me.mainToolStripProgressBar.Minimum() = 0
    Me.mainToolStripProgressBar.Value() = 0
    loopInt = 0
    For Each fileString In listString
    'Attempt to load each configuration (if it fails, offer to delete the bad file?)
    If Not Me.activeObject.Load(fileString) Then Exit For

    'Apply CPU architecture filter
    Select Case modeInt
    Case 1
    If Me.activeObject.Architecture() = "8086" Then
    Me.mainListView.Items.Add(Me.activeObject.Name)
    Me.mainListView.Items.Item(loopInt).SubItems.Add(Me.activeObject.Architecture)
    Me.mainListView.Items.Item(loopInt).Name = fileString
    loopInt += 1
    End If
    Exit Select

    Case 2
    If Me.activeObject.Architecture() = "286" Then
    Me.mainListView.Items.Add(Me.activeObject.Name)
    Me.mainListView.Items.Item(loopInt).SubItems.Add(Me.activeObject.Architecture)
    Me.mainListView.Items.Item(loopInt).Name = fileString
    loopInt += 1
    End If
    Exit Select

    Case 3
    If Me.activeObject.Architecture() = "386" Then
    Me.mainListView.Items.Add(Me.activeObject.Name)
    Me.mainListView.Items.Item(loopInt).SubItems.Add(Me.activeObject.Architecture)
    Me.mainListView.Items.Item(loopInt).Name = fileString
    loopInt += 1
    End If
    Exit Select

    Case 4
    If Me.activeObject.Architecture() = "486" Then
    Me.mainListView.Items.Add(Me.activeObject.Name)
    Me.mainListView.Items.Item(loopInt).SubItems.Add(Me.activeObject.Architecture)
    Me.mainListView.Items.Item(loopInt).Name = fileString
    loopInt += 1
    End If
    Exit Select

    Case 5
    If Me.activeObject.Architecture() = "Pentium" Then
    Me.mainListView.Items.Add(Me.activeObject.Name)
    Me.mainListView.Items.Item(loopInt).SubItems.Add(Me.activeObject.Architecture)
    Me.mainListView.Items.Item(loopInt).Name = fileString
    loopInt += 1
    End If
    Exit Select

    Case 6
    If Me.activeObject.Architecture() = "Pentium II" Then
    Me.mainListView.Items.Add(Me.activeObject.Name)
    Me.mainListView.Items.Item(loopInt).SubItems.Add(Me.activeObject.Architecture)
    Me.mainListView.Items.Item(loopInt).Name = fileString
    loopInt += 1
    End If
    Exit Select

    Case Else
    Me.mainListView.Items.Add(Me.activeObject.Name)
    Me.mainListView.Items.Item(loopInt).SubItems.Add(Me.activeObject.Architecture)
    Me.mainListView.Items.Item(loopInt).Name = fileString
    loopInt += 1
    Exit Select
    End Select

    'Increment the counter and continue
    Me.mainToolStripProgressBar.Value = loopInt
    Next fileString
    [/code]
    Oh and the "xxx" extension does not mean it's pornographic, I am simply using it as a temporary extension to avoid reading in the few thousand regular extension files!

    *EDIT*

    I found a fix. If I disable sorting while the list is generated, then enable sorting when done, it sorts them alphabetically after everything has been properly entered, so all is good.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.