Solved

How to get the width of a string in pixels.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now