Solved

How to get the width of a string in pixels.

Posted on 2006-11-05
5
781 Views
Last Modified: 2008-02-01
Dear All,

Can you please help me by finding out a solution to get the Width of a String in PIXEL?

I have gone through this link:
http://www.dotnet247.com/247reference/msgs/29/149906.aspx

The VB Code is working file with this code:
1. It is getting the longest string (gsLongestDisplayText) of a dropdown combobox (cboHaz) and then  getting it to twips
and then getting the pixels.
2. It is calling an API to show the longest test when user clicks the dropdown combo

get the longest

    With rsConditions
        While Not .EOF
            gsDisplayText = !Name & "(" & CStr(!Unno) & ")"
            cboHaz.AddItem gsDisplayText
            ' Get maximum length of field.
            If Len(gsDisplayText) > glLenOfHaz Then
                glLenOfHaz = Len(gsDisplayText)
                gsLongestDisplayText = gsDisplayText
            End If
            cboHaz.ItemData(cboHaz.NewIndex) = !ID
            .MoveNext
        Wend
        .Close
    End With

    '   Calculate maximum width of dropdown in pixels.
    If glLenOfHaz > 0 Then
        glLenOfHazInTwips = Me.TextWidth(gsLongestDisplayText)
        glLenOfHazInPixels = glLenOfHazInTwips / Screen.TwipsPerPixelX
    End If

    Call SendMessageByNum(cboHaz.hwnd, CB_SETDROPPEDWIDTH, glLenOfHazInPixels, 0)
==========================================================


I wanted to do this in VB.Net

I am aware that the we need to set the .DropDownWidth of the combobox with the pixel value of the long string (gsLongestDisplayText)

Can anybody please help me to giving some idea of link?

Thanks in advance

Regards,
Kishor
0
Comment
Question by:kctripathy
5 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17876298
It looks to me like the link you posted answers your own question: Use the Graphics.MeasureString() method.

Before I noticed that, I'd actually forgotten the name of the method so I asked Google for "VB .NET string width pixels" and got back another page discussing a different approach (which I actually have NOT used before): http://www.thescripts.com/forum/thread281118.html

Most of the time I suspect Graphics.MeasureString() will be available and can do exactly what you want.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17876429
Hi kctripathy;

Try replacing this in your code:

    '   Calculate maximum width of dropdown in pixels.
    If glLenOfHaz > 0 Then
        glLenOfHazInTwips = Me.TextWidth(gsLongestDisplayText)
        glLenOfHazInPixels = glLenOfHazInTwips / Screen.TwipsPerPixelX
    End If

    Call SendMessageByNum(cboHaz.hwnd, CB_SETDROPPEDWIDTH, glLenOfHazInPixels, 0)

With this code:

    If glLenOfHaz > 0 Then
        strSize = cboHaz.CreateGraphics.MeasureString(gsLongestDisplayText, cboHaz.Font)
        cboHaz.Width = CInt(strSize.Width + 0.9)
    End If


Fernando
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 125 total points
ID: 17876430
Heres something I use, it'll return the size of the string.

    Private Function Measure(ByVal BannerText As String, ByVal FontName As String, ByVal FontSize As Single) As Size
        Dim Bitmap As Bitmap
        Dim gr As Graphics
        Dim Font As New Font(FontName, FontSize)

        ' Compute the string dimensions in the given font
        Bitmap = New Bitmap(1, 1, PixelFormat.Format32bppArgb)
        gr = Graphics.FromImage(Bitmap)
        Dim stringSize As SizeF = gr.MeasureString(BannerText, Font)
        Dim rWidth = stringSize.Width
        Dim rHeight = stringSize.Height
        gr.Dispose()
        Bitmap.Dispose()

        Return New Size(rWidth, rHeight)
    End Function

Hope it helps.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17876438
Hi kctripathy;

Sorry I left out this in my last post.  The variable strSize is of type SizeF.

        Dim strSize As SizeF

Fernando
0
 
LVL 1

Author Comment

by:kctripathy
ID: 17879019
Dear Kinger247 and FernandoSoto,

Thank you very much for offering your helping hand towards me to derive the solution.

I am very much glad to get your response as I got through it what exactly I wanted.

Actually I was thinking for a function which will accept the string and will return the size. which Kinger provided me.

Thank you again Kinger

Regards,
Kishor

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

828 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