none
如何在ComboBox项目旁添加小图标 RRS feed

答案

全部回复

  • 这个需要自定义COMBOBX,你要重写COMBOBOX里面的事件,

    这里有个DEMO.你参照下

    http://www.codeproject.com/Articles/10670/Image-ComboBox-Control


    http://blog.csdn.net/zx13525079024


    2012年6月13日 10:16
  • 您介绍的那个可以直接拿来用了,谢谢!

    另外我还在网上找到一个自定义组合框,更简单,只是组合框的文本框只能显示文本,没有小图标,请问应该怎样改,文本框才能显示小图标+文本?

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

    namespace MyProject
    {
       class ComboBoxEx : ComboBox
       {
          private ImageList imageList;

          public ImageList ImageList
          {
             get { return imageList; }
             set { imageList = value; }
          }

          public ComboBoxEx()
          {
             DrawMode = DrawMode.OwnerDrawFixed;
          }

          protected override void OnDrawItem(DrawItemEventArgs ea)
          {
             ea.DrawBackground();
             ea.DrawFocusRectangle();

             ComboBoxExItem item;
             Size imageSize = imageList.ImageSize;
             Rectangle bounds = ea.Bounds;

             try
             {
                item = (ComboBoxExItem)Items[ea.Index];

                if (item.ImageIndex != -1)
                {
                   imageList.Draw(ea.Graphics, bounds.Left, bounds.Top,
                    item.ImageIndex);
                   ea.Graphics.DrawString(item.Text, ea.Font,
        new SolidBrush(ea.ForeColor), bounds.Left +
        imageSize.Width, bounds.Top);
                }
                else
                {
                   ea.Graphics.DrawString(item.Text, ea.Font,
        new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
                }
             }
             catch
             {
                if (ea.Index != -1)
                {
                   ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font,
         new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
                }
                else
                {
                   ea.Graphics.DrawString(Text, ea.Font,
        new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
                }
             }

             base.OnDrawItem(ea);
          }
       }

       class ComboBoxExItem
       {
          private string _text;
          public string Text
          {
             get { return _text; }
             set { _text = value; }
          }

          private int _imageIndex;
          public int ImageIndex
          {
             get { return _imageIndex; }
             set { _imageIndex = value; }
          }

          public ComboBoxExItem() : this("")
          {
          }

          public ComboBoxExItem(string text)
             : this(text, -1)
          {
          }

          public ComboBoxExItem(string text, int imageIndex)
          {
             _text = text;
             _imageIndex = imageIndex;
          }

          public override string ToString()
          {
             return _text;
          }
       }
    }

    2012年6月14日 3:54
  • dear

    可參考

    http://www.dotblogs.com.tw/yc421206/archive/2011/03/15/21858.aspx


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/

    2012年6月14日 4:33