pibcat
pibcat

Reputation: 94

How can i show full text of a combobox selecteditem in windowsforms?

I am stuck with a problem while doing windows project. Thinking you guys can help me in finding some ways to come around it.

Issue - I have dropdowns on a form which contains list of items with variable length. sometimes the width of the text exceeds the width of dropdown and text getting truncated. I want to come up with a way to show the full text. I tried including tooltip, its working fine when the dropdown expands but the tooltip is hidden when the dropdown state is collapsed. I want to show the full text of selected item of the drop down either a tooltip or some other way.

Thank you for the help.

Upvotes: 2

Views: 9055

Answers (2)

Icemanind
Icemanind

Reputation: 48686

Try this:

private SizeF GetMaxSize(List<string> items)
{
    Graphics g = CreateGraphics();
    SizeF size;
    SizeF oldSize = new Size(0f,0f);

    foreach(string item in items)
    {
        size = g.MeasureString(item, myComboBox.Font);
        if (size.Width > oldSize.Width) {
            oldSize.Width = size.Width
            oldSize.Height = size.Height
        }
    }

    return oldSize;
}

Just pass it a List<T> collection of all your strings you'll be populating in the combo box and it will find the largest width in the strings. You can then resize your combo boxes to the width accordingly. myComboBox.Width = GetMaxSize().Width

Upvotes: 1

Jegan
Jegan

Reputation: 1237

There are number of ways,

1) most obvious one make the combo box bigger

2) Use the tool tip as you are doing now but extend this to combo Box mouse over event

3) create a label with your desired length and height (label can wrap text too). use the Combobox MouseHover, MouseLeave, DropDown event to maneuver your label with desired text and use the label visible property to show and hide as you needed.

In addition using a label over tooltip, you will have more functionality and flexibility.

Upvotes: 2

Related Questions