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.
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
We need add following event handler for handling MeasureItem and DrawItem events.
Now we are ready with our code so lets build, run and test.
Tags: Showing full item text in Combo Box, .NET, C#, Windows Application
but i don’t want to increase the width of the combobox.it effect my design and expand the page so this solution is not good
Is it possible to see selected text from the start? Now (if the items text is longer than the combo box width) I see only the end of the item. It works if I set StartingPosition and SelectionLength, but it works only for the first item in the combo box. As soon as I change item (with text longer than the combo) it shows only the end of the text.