打印

[asp.net教程] 为combobox控件添加图片

为combobox控件添加图片

.net自带的combobox控件没有添加图片的功能,我们可以自己进行扩展,下面就是扩展类的代码:
   
  结果如下:
   
   
   
  comboboxex.cs
   
  using system;
  using system.windows.forms;
  using system.windows.forms.design;
  using system.drawing;
   
  namespace mengxianhui.comboboxex
  {
   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;
   }
   }
   
  }
  使用方法
   
  using system;
  using system.drawing;
  using system.collections;
  using system.componentmodel;
  using system.windows.forms;
  using system.data;
   
  namespace mengxianhui.comboboxex
  {
   /// <summary>
   /// summary description for form1.
   /// </summary>
   public class form1 : system.windows.forms.form
   {
   private comboboxex combobox1;
   private system.componentmodel.icontainer components;

   
   public form1()
   {
   //
   // required for windows form designer support
   //
   initializecomponent();
   
   //
   // todo: add any constructor code after initializecomponent call
   //
   }
   
   /// <summary>
   /// clean up any resources being used.
   /// </summary>
   protected override void dispose( bool disposing )
   {
   if( disposing )
   {
   if (components != null)
   {
   components.dispose();
   }
   }
   base.dispose( disposing );
   }
   
   #region windows form designer generated code
   /// <summary>
   /// required method for designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void initializecomponent()
   {
   comboboxex combobox1 = new comboboxex();
   imagelist imagelist1 = new imagelist();
   imagelist1.images.add(image.fromfile(@"d:\favicon.ico"));
   imagelist1.images.add(image.fromfile(@"d:\favicon4.ico"));
   imagelist1.images.add(image.fromfile(@"d:\favicon5.ico"));
   combobox1.imagelist = imagelist1;
   
   combobox1.width = 200;
   
   combobox1.items.add(new comboboxexitem("【孟宪会之精彩世界】", 0));
   combobox1.items.add(new comboboxexitem("【孟宪会之精彩世界】", 1));
   combobox1.items.add(new comboboxexitem("【孟宪会之精彩世界】", 2));
   this.controls.addrange(new system.windows.forms.control[] {combobox1});
   
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(292, 273);
   this.controls.addrange(new system.windows.forms.control[] {combobox1});
   this.name = "form1";
   this.text = "form1";
   this.resumelayout(false);
   
   }
   #endregion
   
   /// <summary>
   /// the main entry point for the application.
   /// </summary>
   [stathread]
   static void main()
   {
   application.run(new form1());
   
   }
   }
  }

TOP

返回顶部
AYBlue

Processed in 0.052977 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 10:55 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP