Solved

ComboBox drop down width when font is bold

Posted on 2012-03-21
3
514 Views
Last Modified: 2012-03-25
Hello,

I am using Windows Forms, Visual Studio 2010.

I have several combo boxes with items longer than the combo is. So I've created a function which makes drop down width wide as the widest string is. Here is my function:

Public Sub WideComboDropDown()
        Dim conKontrola As ComboBox
        conKontrola = ComboBox2 'in this example I am using ComboBox2. Otherwise, it's
'conKontrola = Me, because whole class inherits ComboBox
       
        If conKontrola.Items.Count > 0 Then            
            Dim lengthHolding As Int32 = 0
            Dim stringWidth As Int32 = 0
            Dim g As Graphics = conKontrola.CreateGraphics
 'seeking for the longest item
            For Each myItem As Object In conKontrola.Items                              
                    lengthHolding = g.MeasureString(myItem, conKontrola.Font).Width + 15
             
                If lengthHolding > stringWidth Then
                    stringWidth = lengthHolding
                End If
            Next

            conKontrola.DropDownWidth = stringWidth + 15  '15 for scroll bar
End Sub

This function works perfect if use unbolded items. When I use bold font, width is not correctly calculated and I don't know what to do about it. Here are snap shots with text unbolded and bolded (I am from Slovenia, so text is Slovene).

Items are not in bolded text, string width is correct
Bolded text - width is not correct, string is much longer when text is bold

Best regards, Frenky
0
Comment
Question by:AntonioRodrigo
  • 2
3 Comments
 
LVL 39

Expert Comment

by:appari
ID: 37746114
try changing this line

lengthHolding = g.MeasureString(myItem, conKontrola.Font).Width + 15

to

lengthHolding = g.MeasureString(myItem, New Font(conKontrola.Font, conKontrola.Font.Style)).Width + 15
0
 

Accepted Solution

by:
AntonioRodrigo earned 0 total points
ID: 37746141
Thanks for answer, but I was lucky on this one - I've found solution by myself. The problem was method g.MeasuerItem - this method calculates wrong.

I've used:

TextRenderer.MeasureText(string, font) - this one does the job correctly.

Helpful links:

http://stackoverflow.com/questions/263614/calculate-the-display-width-of-a-string-in-c#263637

http://msdn.microsoft.com/en-us/library/y4xdbe66.aspx
0
 

Author Closing Comment

by:AntonioRodrigo
ID: 37762434
TextRenderer.MesureText is the method you should use for measuring string sizes in combo boxes.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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