How would I be able to take a listView containing icons and text, and then display the listView icon chosen in a pictureBox? The code has a button load in the processes and their icons in the listView.
The listView looks like this:
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);
}
}
}
}
Since you are using image key to assign images to items, you can get the image of selected item this way:
if (listView1.SelectedItems.Count == 1)
{
var item = listView1.SelectedItems[0];
if (item.ImageList != null)
pictureBox1.Image = item.ImageList.Images[item.ImageKey];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.