C# ListBox学习笔记

本文介绍C# ListBox,以及介绍为 MeasureItem 事件创建事件处理程序,以在 Dra
首页 新闻资讯 行业资讯 C# ListBox学习笔记

学习C# ListBox使用,仅当 DrawMode 属性设置为 DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时,才引发该事件。可以使用该事件来执行在C# ListBox 中绘制项所需的任务。如果具有大小可变的项(当 DrawMode 属性设置为 DrawMode.OwnerDrawVariable 时),在绘制项前,引发 MeasureItem 事件。可以为 MeasureItem 事件创建事件处理程序,以在 DrawItem 事件的事件处理程序中指定要绘制的项的大小。有关处理事件的更多信息,请参见 使用事件。

重写C# Listbox的drawitem事件

复制

privatevoidlistBox1_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgse){  //SettheDrawModepropertytodrawfixedsizeditems.  listBox1.DrawMode=DrawMode.OwnerDrawFixed;  //DrawthebackgroundoftheListBoxcontrolforeachitem.  e.DrawBackground();  //Definethedefaultcolorofthebrushasblack.  BrushmyBrush=Brushes.Black;  //Determinethecolorofthebrushtodraweachitembasedontheindexoftheitemtodraw.  switch(e.Index){  case0:myBrush=Brushes.Red;break;  case1:myBrush=Brushes.Orange;break;  case2:myBrush=Brushes.Purple;break;  }  //DrawthecurrentitemtextbasedonthecurrentFontandthecustombrushsettings.  e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font,myBrush,  e.Bounds,StringFormat.GenericDefault);  //IftheListBoxhasfocus,drawafocusrectanglearoundtheselecteditem.  e.DrawFocusRectangle();  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

从这个例子,我们发现在C#下面重画控件,比在vc++6.0中定义自绘方便多了

【编辑推荐】

  1. 概述C#实现文件下载

  2. C#搞定网站验证码的方法

  3. 浅谈C# String对象

  4. C#命名空间学习笔记

  5. 浅析C#调用ActiveX控件

12    2009-08-12 15:50:40    C# ListBox