C# 我可以在列表框中显示图像吗?

C# 我可以在列表框中显示图像吗?,c#,winforms,listbox,C#,Winforms,Listbox,C#在坚果壳中,我可以在列表框中显示图像吗?我有一个用户列表,我想在一些名字旁边显示一个绿色的勾号,这可能吗 谢谢在WPF中,它非常简单,但是如果您使用winforms,则无法使用System.Windows.Forms.ListBox控件。您可以使用ListView控件或第三方控件来完成此操作。System.Windows.Forms.ListView将很容易完成此操作。但是,如果希望列表显示在“详细信息”视图中,则可能需要比列表框更努力一些。下面的代码显示了如何在列表框中进行自定义绘图 us

C#在坚果壳中,我可以在列表框中显示图像吗?我有一个用户列表,我想在一些名字旁边显示一个绿色的勾号,这可能吗


谢谢

在WPF中,它非常简单,但是如果您使用winforms,则无法使用System.Windows.Forms.ListBox控件。您可以使用ListView控件或第三方控件来完成此操作。

System.Windows.Forms.ListView将很容易完成此操作。但是,如果希望列表显示在“详细信息”视图中,则可能需要比列表框更努力一些。

下面的代码显示了如何在列表框中进行自定义绘图

using System.Windows.Forms;
using System.Drawing;

namespace Toolset.Controls
{
    public class CustomDrawListBox : ListBox
    {
        public CustomDrawListBox()
        {
            this.DrawMode = DrawMode.OwnerDrawVariable; // We're using custom drawing.
            this.ItemHeight = 40; // Set the item height to 40.
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            // Make sure we're not trying to draw something that isn't there.
            if (e.Index >= this.Items.Count || e.Index <= -1)
                return;

            // Get the item object.
            object item = this.Items[e.Index];
            if (item == null)
                return;

            // Draw the background color depending on 
            // if the item is selected or not.
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                // The item is selected.
                // We want a blue background color.
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Bounds);
            }
            else
            {
                // The item is NOT selected.
                // We want a white background color.
                e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
            }

            // Draw the item.
            string text = item.ToString();
            SizeF stringSize = e.Graphics.MeasureString(text, this.Font);
            e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.White),
                new PointF(5, e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2));
        }
    }
}
使用System.Windows.Forms;
使用系统图;
命名空间工具集.控件
{
公共类CustomDrawListBox:ListBox
{
公共CustomDrawListBox()
{
this.DrawMode=DrawMode.OwnerDrawVariable;//我们正在使用自定义绘图。
this.ItemHeight=40;//将项目高度设置为40。
}
受保护的覆盖无效OnDrawItem(DrawItemEventArgs e)
{
//确保我们没有试图画一些不存在的东西。

如果(e.Index>=this.Items.Count | | e.Index)您使用的是Winforms还是WPF?Steve,这篇文章可能会为您指明正确的方向:我只想说,我强烈建议不要使用该教程。它有很多问题,给读者留下了太多的问题,而且代码也很糟糕。