Click here to Skip to main content
15,889,429 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm creating a program that downloads file from ftp server.
Putting all downloaded files and downloading files in a ListView.
I want to prioritize the downloading file so I want to add the item in first index.

I try using this sample code but it doesn't work.
VB
'FIRST DOWNLOAD
With lstDownload.Items.Add("Sample", 0)
        .SubItems.Add(2)
        .SubItems.Add(3)
        .SubItems.Add(4)
        .SubItems.Add(5)
        .SubItems.Add(6)
        .SubItems.Add(listDownload.Count - 1)
    End With
'SECOND DOWNLOAD
    With lstDownload.Items.Add("Sample1", 0)
        .SubItems.Add(2)
        .SubItems.Add(3)
        .SubItems.Add(4)
        .SubItems.Add(5)
        .SubItems.Add(6)
        .SubItems.Add(listDownload.Count - 1)
    End With


In that example I want to display the "Sample1" first in the ListView.

Can anyone post a code/suggestion to this.

Thanks in advanced... totally appreciated
Posted
Updated 7-Sep-16 22:33pm

Hello,

A solution is to use Insert instead of Add.

VB
'FIRST DOWNLOAD
With lstDownload.Items.Add("Sample", 0)
    .SubItems.Add(2)
    .SubItems.Add(3)
    .SubItems.Add(4)
    .SubItems.Add(5)
    .SubItems.Add(6)
    .SubItems.Add(listDownload.Count - 1)
End With
'SECOND DOWNLOAD
With lstDownload.Items.Insert(0, "Sample1", 0)
    .SubItems.Add(2)
    .SubItems.Add(3)
    .SubItems.Add(4)
    .SubItems.Add(5)
    .SubItems.Add(6)
    .SubItems.Add(listDownload.Count - 1)
End With



Valery
 
Share this answer
 
you could try something like

VB
Dim listDownloads as New List(of ListViewItem)

Dim lvItem as ListViewItem

lvitem = New ListViewItem()
lvitem.text = "Sample"
lvitem.subitems.add(2)
lvitem.subitems.add(3)
lvitem.subitems.add(4)
lvitem.subitems.add(5)
lvitem.subitems.add(6)
'Set your colour and stuff here
'lvitem.backcolor

listDownloads.add( lvitem ) 'Add the item to an array

'Now add the items to the listview
Dim iCount as integer

For iCount = yourlistview.items.count -1 to 0 step -1
  yourlistview.items.add( lvitem )
Next
 
Share this answer
 
C#
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
        ListViewItem lvi = new ListViewItem("Add "+DateTime.Now);
        ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
        lvsi.Text = DateTime.Now.ToString();
        lvi.SubItems.Add(lvsi);
        _list.Insert(0, lvi);
        listView1.Invalidate();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900