This is a migrated thread and some comments may be shown as answers.

How to apply this code into Telerik radListView?

1 Answer 211 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Alberto
Top achievements
Rank 1
Alberto asked on 01 May 2016, 09:56 PM
Im trying to use this code to get each listview item with the default system's icon. Can get it to work with common listview, but not with radListView. Any idea on how to make it work?

 

ListView listView1;
ImageList imageList1;
 
public void ExtractAssociatedIconEx()
{
    // Initialize the ListView, ImageList and Form.
    listView1 = new ListView();
    imageList1 = new ImageList();
    listView1.Location = new Point(37, 12);
    listView1.Size = new Size(151, 262);
    listView1.SmallImageList = imageList1;
    listView1.View = View.SmallIcon;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.listView1);
    this.Text = "Form1";
 
    // Get the c:\ directory.
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\");
 
    ListViewItem item;
    listView1.BeginUpdate();
 
    // For each file in the c:\ directory, create a ListViewItem
    // and set the icon to the icon extracted from the file.
    foreach (System.IO.FileInfo file in dir.GetFiles())
    {
        // Set a default icon for the file.
        Icon iconForFile = SystemIcons.WinLogo;
 
        item = new ListViewItem(file.Name, 1);
        iconForFile = Icon.ExtractAssociatedIcon(file.FullName);
 
        // Check to see if the image collection contains an image
        // for this extension, using the extension as a key.
        if (!imageList1.Images.ContainsKey(file.Extension))
        {
            // If not, add the image to the image list.
            iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName);
            imageList1.Images.Add(file.Extension, iconForFile);
        }
        item.ImageKey = file.Extension;
        listView1.Items.Add(item);
    }
    listView1.EndUpdate();
}

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 02 May 2016, 11:40 AM
Hello Alberto,

Thank you for writing.

You can use the same approach with RadListView. Here is the code:
RadListView listView1;
ImageList imageList1;
 
public void ExtractAssociatedIconEx()
{
    // Initialize the ListView, ImageList and Form.
    listView1 = new RadListView();
    imageList1 = new ImageList();
    listView1.Location = new Point(37, 12);
    listView1.Size = new Size(151, 262);
    listView1.ImageList = imageList1;
    listView1.ViewType = ListViewType.IconsView;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.listView1);
    this.Text = "Form1";
 
    // Get the c:\ directory.
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\");
 
    ListViewDataItem item;
    listView1.BeginUpdate();
 
    // For each file in the c:\ directory, create a ListViewItem
    // and set the icon to the icon extracted from the file.
    foreach (System.IO.FileInfo file in dir.GetFiles())
    {
        // Set a default icon for the file.
        Icon iconForFile = SystemIcons.WinLogo;
 
        item = new ListViewDataItem(file.Name);
        iconForFile = Icon.ExtractAssociatedIcon(file.FullName);
 
        // Check to see if the image collection contains an image
        // for this extension, using the extension as a key.
        if (!imageList1.Images.ContainsKey(file.Extension))
        {
            // If not, add the image to the image list.
            iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName);
            imageList1.Images.Add(file.Extension, iconForFile);
        }
        item.ImageKey = file.Extension;
        listView1.Items.Add(item);
    }
    listView1.EndUpdate();
}

Let me know if I can assist you further.

Regards,
Dimitar
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
ListView
Asked by
Alberto
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or