---
title: "C#设置Listbox的ItemHeight"
author: "huanghuaibin"
date: "2011-01-20T14:59:36Z"
tags: ["C", "C#", "C++"]
categories: ["编程语言"]
slug: "c-listbox-itemheight"
draft: false
lastmod: "2011-01-20T14:59:36Z"
id: 889876
url: "https://ihhb.iteye.com/blog/889876"
---

这也是在使用Listbox的时候遇到的问题，ItemHeight的默认值是13，当在属性窗口里更改它时，改了又变回去了。后来上网查了下资料，原来Listbox默认是不能更改ItemHeight的。想要更改的话，需要这样做：  
      首先设置DrawMode属性为OwnerDrawVariable，自己画Listbox。  
      然后处理DrawItem和MeasureItem两个事件，DrawItem是在显示项时触发，MeasureItem是在要计算项的宽高时触发。在DrawItem里重新绘制项目，如下：    

![程序代码](http://www.lwolf.cn/blog/images/code.gif) 程序代码

            e.DrawBackground ();  
            e.DrawFocusRectangle ();  
            e.Graphics.DrawString (listBox1.Items\[e.Index].ToString(), e.Font, new SolidBrush (e.ForeColor), e.Bounds);

      可以在MeasureItem里设置ItemHeight      

![程序代码](http://www.lwolf.cn/blog/images/code.gif) 程序代码

            e.ItemHeight = 20;

      如果只是这样简单的设个值的话可以不用MeasureItem事件了，因为这里已经可以在属性窗口里更改ItemHeight的值了。