当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  为ComboBox控件添加图片

 为ComboBox控件添加图片

点击次数:20 次 发布日期:2008-11-26 11:51:11 作者:源代码网
源代码网推荐      .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());
源代码网推荐  
源代码网推荐   }
源代码网推荐   }
源代码网推荐  }
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华