Solved

ComboBox drop down width when font is bold

Posted on 2012-03-21
3
510 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now