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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/472897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:47:32  来源:igfitidea点击:

C# Can I display images in a list box?

c#winformslistbox

提问by

C# In a nut shell can I display images in a list box? I have a list of users and I want to display a green tick next to some of the names, is this possible?

C# 简而言之,我可以在列表框中显示图像吗?我有一个用户列表,我想在某些名称旁边显示一个绿色勾号,这可能吗?

Thanks

谢谢

回答by gkrogers

Steve, this article might point you in the right direction:
http://www.codeproject.com/KB/combobox/glistbox.aspx

史蒂夫,这篇文章可能会为您指明正确的方向:http:
//www.codeproject.com/KB/combobox/glistbox.aspx

回答by Michael Meadows

In WPF it's quite simple, but if you're using winforms, you can't do it with the System.Windows.Forms.ListBox control. You cando it with the ListView control, or third party controls.

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

回答by Coincoin

System.Windows.Forms.ListView will do the trick very easily. You might have to work a little harder than a ListBox if you want the list in 'details' view though.

System.Windows.Forms.ListView 可以很容易地做到这一点。如果您希望在“详细信息”视图中显示列表,您可能需要比 ListBox 更努力地工作。

回答by Patrik Svensson

The following code displays how to do custom drawing in a listbox.

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

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));
        }
    }
}