如何将图像添加到System.Windows.Forms.ListBox? [英] How do add image to System.Windows.Forms.ListBox?

查看:107
本文介绍了如何将图像添加到System.Windows.Forms.ListBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发聊天程序,我使用ListBox管理昵称列表。但是,客户有昵称和州(在线,离开)

I'm developing chatting program using C# and I manage nickname list using ListBox. But, client have nickname, and state (online, away)

所以,为了管理状态,我想添加图像(在线 - 绿色圆圈,远离 - ListBox(这是我的想法)

So, in order to manage the state, I want to add image (online - green circle, away - red circle) to ListBox (it is my idea)

如何将图像添加到ListBox?请帮助我。

How can add image to ListBox? Please help me.

谢谢。

推荐答案

你不能在ListBox中轻松完成。使用Graphics绘制它们并不容易。我建议使用 DataGridView o ListView 控件。

You can't do that easily in the ListBox. And drawing them using Graphics is not easy at all. I suggest using a DataGridView o ListView control instead.

结果:

DataGridView :有一个名为的DataType DataGridViewImageColumn 您可以用来显示您的状态图标。

DataGridView: There's a ColumnType called DataGridViewImageColumn that you can use to show your status icon.

public void FillDataGridView()
{
    //images
    var greenImg = Resources.green;
    var redImg = Resources.red;
    var yellowImg = Resources.yellow;

    dataGridView1.Rows.Add(new object[] {"Vland", greenImg});
    dataGridView1.Rows.Add(new object[] {"John", yellowImg });
    dataGridView1.Rows.Add(new object[] {"Paul", greenImg});
    dataGridView1.Rows.Add(new object[] {"George", redImg});
    dataGridView1.Rows.Add(new object[] {"Ringo", redImg });
}

如何:在Windows窗体DataGridView控件的单元格中显示图像

ListView:可以添加由文字+图片组成的项目。如果你使用ListView,你需要在你的表单中添加一个imageList组件(包含你的图像)并调用他们的 imageKey 值,如示例中所示

ListView: can add items composed by text + image. If you use the ListView, you need to add an imageList component to your form (with your images in it) and call their imageKey value like shown in the example

public void fillListView()
{
    listView1.SmallImageList = imageList1;

    listView1.Items.Add("BombPenguin", "green");
    listView1.Items.Add("Vland", "yellow");
    listView1.Items.Add("John", "red");
    listView1.Items.Add("Paul", "green");
    listView1.Items.Add("Ringo", "red");
}

如何:显示Windows窗体ListView控件的图标

这篇关于如何将图像添加到System.Windows.Forms.ListBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆