-1

How to add an icon to the listView in Windows Forms c#? I have found a NuGet called "ObjectListView" but I don't understand how to add items and sub-items to it. By using standard listview I am able to add images to the first column only.

ListViewItem item = new ListViewItem();

            item.ImageIndex = 0;

            this.listView1.Items.Add(item);

I'm trying to add buttons to remove/restart/etc. for USB devices like this MyProgram

I need those buttons/Images in "remove", "Restart, "Uninstall" columns as subitems.

By using this I can "switch" columns, but still cannot add images/buttons for multiple columns.

 listView1.Columns[0].DisplayIndex = 1;
      //  listView1.Items.Add("tekst3", 3);
       // listView1.Items.Add("tekst2", 2);
        //listView1.Items.Add("tekst1", 1);
        for (int j = 0; j < 10; j++)

        {

            ListViewItem item = new ListViewItem();

            item.ImageIndex = 0;

            this.listView1.Items.Add(item);

        }

An image list is added with loaded files(3 of them) attached to the list view small/large image list.

4

2 回答 2

1

您必须分配图像列表

listView1.SmallImageList = imageList;

    for (int i = 0; i < listView1.Items.Count; i++ )
    {
      
            listView1.Items[i].ImageIndex =i;
    }
于 2021-04-24T17:18:22.147 回答
-1

图像列表已分配:单击 您的代码将仅将图像添加到第一行。不同之处在于我必须单击第一列中的特定项目。

            for (int i = 0; i < listView1.Items.Count; i++)
            {

                listView1.Items[i].ImageIndex = i;

            }

        }

这将给出以下结果:Click1 我需要这样的:Click2

编辑你写的不是我的意思。我找到了解决方案:

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        // insert buttons to last two columns
        if (e.ColumnIndex == 1)
        {
            e.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Bounds);
        }

        else if (e.ColumnIndex == 2)
        {
            e.DrawBackground();
            var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, 60, 20);
            //  e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
            e.Graphics.DrawImage(Resources.Remove_PNG, imageRect);
        }
        else
        {
            e.DrawDefault = true;
        }
    }
于 2021-04-24T17:38:31.843 回答