Link to home
Start Free TrialLog in
Avatar of newyuppie
newyuppieFlag for Ecuador

asked on

combobox text longer than combobox width

vb 2005

is there any way to have a dropdown combobox which has very long text items to show the full text when mouse is over the item?
thanks
Avatar of ericwong27
ericwong27
Flag of Singapore image

  Add ToolTip control into the form

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.ComboBox1.Items.Add("1. This is a very looooooooooooooooooooooooooooong text")
        Me.ComboBox1.Items.Add("2. This is a very looooooooooooooooooooooooooooong text")
        Me.ComboBox1.Items.Add("3. This is a very looooooooooooooooooooooooooooong text")

    End Sub

    Private Sub ComboBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ComboBox1.MouseMove
        If Me.ComboBox1.Text.Length > 0 Then
            Me.ToolTip1.SetToolTip(Me.ComboBox1, Me.ComboBox1.Text)
        Else
            Me.ToolTip1.SetToolTip(Me.ComboBox1, String.Empty)
        End If
    End Sub

Avatar of newyuppie

ASKER

eric,
using your philosophy, how could i check if the length of the text is longer than the width of the combobox to set the tooltip?
(independent of the font used and stuff. i mean, i could have different comboboxes with different font styles and sizes, how could i compare the widths of text and control?)

thanks
ASKER CERTIFIED SOLUTION
Avatar of ericwong27
ericwong27
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok we are onto something here.

my problem with that code is that i have to actually select one of the items to have the tooltip displayed. but what if the items are not completely shown because the combobox is too small, i need to display the tooltip when i hover each item, without necesarily having to click one of them. how would i capture that?



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ended up using combination of your code and the code on the link.

thank you!

had to adapt the code because my combobox is data bound, and had to use DataRowView for the items loop

** code

    Private Sub ComboBox_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox.DropDown
        Dim senderCBO As ComboBox = Nothing
        senderCBO = CType(sender, ComboBox)
        Dim g As Graphics = senderCBO.CreateGraphics()
        Dim mFont As Font = senderCBO.Font
        Dim width As Integer = senderCBO.Width
        Dim vertScrollBarWidth As Integer = (senderCBO.Items.Count > senderCBO.MaxDropDownItems) * SystemInformation.VerticalScrollBarWidth
       

        Dim newWidth As Integer
        For Each drv As DataRowView In senderCBO.Items
            newWidth = CType(g.MeasureString(drv("category").ToString, mFont).Width, Integer) + vertScrollBarWidth
            If width < newWidth Then
                width = newWidth
            End If
        Next
        senderCBO.DropDownWidth = width
    End Sub

    Private Sub ComboBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ComboBox.MouseMove
        Dim senderCBO As ComboBox = Nothing
        senderCBO = CType(sender, ComboBox)
        Dim g As Graphics = senderCBO.CreateGraphics()
        Dim mFont As Font = senderCBO.Font

        If Fix(g.MeasureString(senderCBO.Text, mFont).Width) > senderCBO.Width Then
            Me.ToolTip1.SetToolTip(senderCBO, senderCBO.Text)
        Else
            Me.ToolTip1.SetToolTip(senderCBO, String.Empty)
        End If
    End Sub