Saturday, June 13, 2009

Show Long Text Item in ComboBox

Many times in our windows form we need to show data in Combo Box and items in combo box are have same length, so we face a problem that some item in the combo box is not shown fully, and increasing width of then combo might not always the good idea.

To avoid above mentioned problem we have a solution, we can increase the height of the combo box item at run time based on the length so the item can be read fully, so lets start building the example.

Open Visual Studio and Create a Windows Form Application and Add One label and one combo box and and some items in the combo box as shown in the below screen shot.

image

Now we are ready with design part, so lets start writing some code to increase the height of the combo box, for this we need to make use of the MeasureItem event of Combo box, in this item we can find out what is the length of each item and based on the length we can set the height, once you we set the item length we need to add the item in Combo Box using DrawItem event.

Normally the combo box draw style is OwnerDrawFixed so we cannot change any item at run time if it is OwnerDrawFixed so we need to change Combo Box’s DrawStyle to OwnerDrawVariable so that run time changes are allowed.

Please add following code in Form_Load, in below code I am setting DrawStyle and assigning MeasureIteam and DrawItem events to Combo Box

image

We need add following event handler for handling MeasureItem and DrawItem events.

image

Now we are ready with our code so lets build, run and test.

image

Tags: Showing full item text in Combo Box, .NET, C#, Windows Application

No comments:

Post a Comment

Post a Comment