问题描述
我正在使用 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:有一个名为 DataGridViewImageColumn
的 ColumnType,您可以使用它来显示您的状态图标.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!