Solved

ComboBox drop down width when font is bold

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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