Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ComboBox drop down width when font is bold

Posted on 2012-03-21
3
Medium Priority
?
538 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

618 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