Wei Chen Chen
Wei Chen Chen

Reputation: 88

Long ComboBox Text, Can't be Displayed Fully

As the image shown, the combo box text in my case is very long.

Without changing the width of the combo box, is there any method to show full text when the user hovers on it?

enter image description here

Upvotes: 0

Views: 1188

Answers (2)

Rajan Shukla
Rajan Shukla

Reputation: 1

You can use tool tip on mouse hover, or you have to change the width of combobox, or increase the font size of the combobox.

Upvotes: 0

dr.null
dr.null

Reputation: 4660

Without changing the width of the combo box, is there any method to show full text when the user hovers on it?

You can use the ToolTip component to show the items with long text when the mouse pointer hovers over the control. Whenever the SelectedIndex changes, measure the selected item by the TextRenderer.MeasureText method, and if the string's width is greater than the editbox's, set the long string as a tooltip for the control.


Note: The width of the EditBox = ComboBox.Width - ButtonWidth. You can get the ButtonWidth from the SystemInformation.VerticalScrollBarWidth property.


To apply this feature, let's create a little class and inherit the ComboBox control.

C#

// +
// using System.ComponentModel;

[DesignerCategory("Code")]
public class ComboBoxEx : ComboBox
{
    private readonly ToolTip ttp;

    public ComboBoxEx() : base() => ttp = new ToolTip();

    private bool _showItemToolTip = true;
    [DefaultValue(true)]
    public bool ShowItemToolTip
    {
        get => _showItemToolTip;
        set
        {
            if (_showItemToolTip != value)
            {
                _showItemToolTip = value;
                ttp.SetToolTip(this, string.Empty);  
            }
        }
    }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        base.OnSelectedIndexChanged(e);

        if (ShowItemToolTip)
        {
            string str = string.Empty;

            if (SelectedIndex >= 0)
            {
                var sz = TextRenderer.MeasureText(Text, Font);

                if (sz.Width > Width - SystemInformation.VerticalScrollBarWidth)
                    str = Text;
            }

            ttp.SetToolTip(this, str);
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing) ttp.Dispose();
        base.Dispose(disposing);
    }
}

VB.Net

' +
' Imports System.ComponentModel

<DesignerCategory("Code")>
Public Class ComboBoxEx
    Inherits ComboBox

    Private ReadOnly ttp As ToolTip

    Sub New()
        MyBase.New
        ttp = New ToolTip()
    End Sub

    Private _showItemToolTip As Boolean = True
    <DefaultValue(True)>
    Public Property ShowItemToolTip As Boolean
        Get
            Return _showItemToolTip
        End Get
        Set(value As Boolean)
            If (_showItemToolTip <> value) Then
                _showItemToolTip = value
                ttp.SetToolTip(Me, String.Empty)
            End If
        End Set
    End Property

    Protected Overrides Sub OnSelectedIndexChanged(e As EventArgs)
        MyBase.OnSelectedIndexChanged(e)

        If ShowItemToolTip Then
            Dim str = ""

            If SelectedIndex >= 0 Then
                Dim sz = TextRenderer.MeasureText(Text, Font)

                If sz.Width > Width - SystemInformation.VerticalScrollBarWidth Then
                    str = Text
                End If
            End If

            ttp.SetToolTip(Me, str)
        End If
    End Sub

    Protected Overrides Sub Dispose(disposing As Boolean)
        If disposing Then ttp.Dispose()
        MyBase.Dispose(disposing)
    End Sub

End Class

You can use the same approach if you don't want to subclass. Drop a ToolTip component and handle the ComboBox.SelectedIndexChanged event as shown above.

Upvotes: 2

Related Questions