【问题标题】:How can I include icons in my ListBox?如何在我的 ListBox 中包含图标?
【发布时间】:2011-11-30 21:37:28
【问题描述】:

我知道之前已经在这里提出过类似的问题,但它们都导致same codeproject article 不起作用。有人知道带图标的工作列表框吗?

【问题讨论】:

  • 您应该添加指向该 CodeProject 文章的链接。
  • 并提及 CodeProject 文章中不适用的内容。

标签: c# .net winforms c#-4.0


【解决方案1】:

ListView 对你有用吗?这就是我使用的。更容易,你可以让它看起来像ListBox。此外,MSDN 上有大量文档可供入门。

如何:显示 Windows 窗体 ListView 控件的图标
Windows 窗体 ListView 控件可以显示三个图像中的图标 列表。 List、Details 和 SmallIcon 视图显示来自 SmallImageList 属性中指定的图像列表。大图标 view 显示在指定的图像列表中的图像 大图像列表属性。列表视图还可以显示额外的 一组图标,在 StateImageList 属性中设置,位于大号或 小图标。有关图像列表的更多信息,请参阅 ImageList 组件(Windows 窗体)和如何:使用 Windows 窗体 ImageList 组件。

How to: Display Icons for the Windows Forms ListView Control插入

【讨论】:

    【解决方案2】:

    如果您不想将 ListBox 更改为 ListView,您可以为 DrawItemEvent 编写一个处理程序。例如:

    private void InitializeComponent()
    {
        ...
        this.listBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox_DrawItem);
        ...
     }
    private void listBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
                return;
            // Draw the background of the ListBox control for each item.
            e.DrawBackground();
            var rect = new Rectangle(e.Bounds.X+10, e.Bounds.Y+8, 12, 14);
           //assuming the icon is already added to project resources
            e.Graphics.DrawIconUnstretched(YourProject.Properties.Resources.YouIcon, rect);
            e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
                e.Font, Brushes.Black, new Rectangle(e.Bounds.X + 25, e.Bounds.Y + 10, e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);
            // If the ListBox has focus, draw a focus rectangle around the selected item.
            e.DrawFocusRectangle();
        }
    

    您可以使用矩形来设置图标的正确位置

    【讨论】:

      【解决方案3】:

      如果您被困在 WinForms 中工作,那么您将不得不对您的项目进行所有者绘制。

      请参阅DrawItem event 的示例。

      【讨论】:

        【解决方案4】:

        有点不同的方法 - 不要使用列表框。 我没有使用将我限制在其有限的属性和方法集的控件,而是创建了我自己的列表框。

        这并不像听起来那么难:

        int yPos = 0;    
        Panel myListBox = new Panel();
        foreach (Object object in YourObjectList)
        {
            Panel line = new Panel();
            line.Location = new Point(0, Ypos);
            line.Size = new Size(myListBox.Width, 20);
            line.MouseClick += new MouseEventHandler(line_MouseClick);
            myListBox.Controls.Add(line);
        
            // Add and arrange the controls you want in the line
        
            yPos += line.Height;
        }
        

        myListBox 事件处理程序示例 - 选择一行:

        private void line_MouseClick(object sender, MouseEventArgs)
        {
            foreach (Control control in myListBox.Controls)
                if (control is Panel)
                    if (control == sender)
                        control.BackColor = Color.DarkBlue;
                    else
                        control.BackColor = Color.Transparent;      
        }
        

        上面的代码示例没有经过测试,但是使用了描述的方法,发现非常方便和简单。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-13
          • 2014-10-16
          • 2021-02-01
          • 2013-02-05
          • 1970-01-01
          相关资源
          最近更新 更多