Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

combobox text longer than combobox width

Posted on 2006-06-04
6
Medium Priority
?
477 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:newyuppie
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:ericwong27
ID: 16830012
  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

0
 
LVL 13

Author Comment

by:newyuppie
ID: 16832887
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
0
 
LVL 10

Accepted Solution

by:
ericwong27 earned 2000 total points
ID: 16833878
Try this

   Private Sub ComboBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ComboBox1.MouseMove

        Dim g As Graphics = Me.ComboBox1.CreateGraphics()
        If Fix(g.MeasureString(Me.ComboBox1.Text, Font).Width) > Me.ComboBox1.Width Then
            Me.ToolTip1.SetToolTip(Me.ComboBox1, Me.ComboBox1.Text)
        Else
            Me.ToolTip1.SetToolTip(Me.ComboBox1, String.Empty)
        End If

    End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:newyuppie
ID: 16837252
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?



0
 
LVL 10

Assisted Solution

by:ericwong27
ericwong27 earned 2000 total points
ID: 16838363

The MouseMove events only fire on the combobox not the dropdown list

Alternative way
http://www.codeproject.com/cs/combobox/ComboBoxAutoWidth.asp

0
 
LVL 13

Author Comment

by:newyuppie
ID: 16838645
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question