【问题标题】:Display Icon from ListView Item into PictureBox将 ListView 项中的图标显示到 PictureBox 中
【发布时间】:2017-03-27 21:02:49
【问题描述】:

如何获取包含图标和文本的 listView,然后显示在图片框中选择的 listView 图标?代码在进程中有一个按钮加载,在listView中有它们的图标。

listView 如下所示:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (listView1.SelectedIndices.Count <= 0)
    {
        return;
    }
    int intselectedindex = listView1.SelectedIndices[0];
    if (intselectedindex >= 0)
    {
        textBox1.Text = listView1.Items[intselectedindex].Text;
        //pictureBox1.Image = ???
    }
}
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
    var query = "SELECT ProcessId, Name, ExecutablePath FROM Win32_Process";
    using (var searcher = new ManagementObjectSearcher(query))
    using (var results = searcher.Get())

    {
        var processes = results.Cast<ManagementObject>().Select(x => new
        {
            ProcessId = (UInt32)x["ProcessId"],
            Name = (string)x["Name"],
            ExecutablePath = (string)x["ExecutablePath"]
        });
        foreach (var p in processes)
        {
            if (System.IO.File.Exists(p.ExecutablePath))
            {
                listView1.LargeImageList = imageList1;
                var icon = Icon.ExtractAssociatedIcon(p.ExecutablePath);
                var key = p.ProcessId.ToString();
                this.imageList1.Images.Add(key, icon.ToBitmap());
                this.listView1.Items.Add(p.Name, key);
            }
        }
    }
}

【问题讨论】:

    标签: c# .net winforms listview icons


    【解决方案1】:

    由于您使用图像键将图像分配给项目,因此您可以通过这种方式获取所选项目的图像:

    if (listView1.SelectedItems.Count == 1)
    {
        var item = listView1.SelectedItems[0];
        if (item.ImageList != null)
            pictureBox1.Image = item.ImageList.Images[item.ImageKey];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多