C# ListBox学习笔记

开发 后端
本文介绍C# ListBox,以及介绍为 MeasureItem 事件创建事件处理程序,以在 DrawItem 事件的事件处理程序中指定要绘制的项的大小。

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

重写C# Listbox的drawitem事件

  1. privatevoidlistBox1_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgse){  
  2. //SettheDrawModepropertytodrawfixedsizeditems.  
  3. listBox1.DrawMode=DrawMode.OwnerDrawFixed;  
  4. //DrawthebackgroundoftheListBoxcontrolforeachitem.  
  5. e.DrawBackground();  
  6. //Definethedefaultcolorofthebrushasblack.  
  7. BrushmyBrush=Brushes.Black;  
  8. //Determinethecolorofthebrushtodraweachitembasedontheindexoftheitemtodraw.  
  9. switch(e.Index){  
  10. case0:myBrush=Brushes.Red;break;  
  11. case1:myBrush=Brushes.Orange;break;  
  12. case2:myBrush=Brushes.Purple;break;  
  13. }  
  14. //DrawthecurrentitemtextbasedonthecurrentFontandthecustombrushsettings.  
  15. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font,myBrush,  
  16. e.Bounds,StringFormat.GenericDefault);  
  17. //IftheListBoxhasfocus,drawafocusrectanglearoundtheselecteditem.  
  18. e.DrawFocusRectangle();  

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

【编辑推荐】

  1. 概述C#实现文件下载
  2. C#搞定网站验证码的方法
  3. 浅谈C# String对象
  4. C#命名空间学习笔记
  5. 浅析C#调用ActiveX控件
责任编辑:佚名 来源: 百度空间
相关推荐

2009-08-19 11:21:02

C# ListBox控

2009-08-14 14:19:50

Enhanced LiC#构建

2009-09-08 15:39:00

2009-04-12 08:55:18

Symbian诺基亚移动OS

2009-08-20 15:02:57

C# If语句

2009-09-08 17:37:54

C# listbox控

2009-08-12 09:28:36

C# WiteOne

2009-08-13 18:21:52

C#学习笔记

2009-09-08 16:34:49

C# Listbox

2009-08-25 15:26:03

C#调用ImOK

2009-08-21 17:53:28

C#查询结果

2009-08-12 10:46:54

C#命名空间

2009-08-24 14:30:49

C# WMI封装

2009-09-10 12:00:09

C# listbox

2009-09-08 16:10:03

C# ListBox

2009-09-08 15:12:07

C# ListBox

2009-08-26 10:48:44

C# SQL命令

2009-08-31 15:27:33

C# TreeView

2009-08-24 15:46:46

C# SmartPho

2009-09-08 14:54:40

C# listBox控
点赞
收藏

51CTO技术栈公众号