Solved

How to get the width of a string in pixels.

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

13 Experts available now in Live!

Get 1:1 Help Now