[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get the width of a string in pixels.

Posted on 2006-11-05
5
Medium Priority
?
800 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 64

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 500 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 64

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 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