Solved

How to get the width of a string in pixels.

Posted on 2006-11-05
5
779 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 62

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 62

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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