Solved

ComboBox drop down width when font is bold

Posted on 2012-03-21
3
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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