Results 1 to 11 of 11

Thread: [RESOLVED] Measure the length of a string in pixels.

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [RESOLVED] Measure the length of a string in pixels.

    Hi,
    I have a problem measuring the length of a string in pixels.
    Currently I'm using this code:
    Code:
        Private Function MeasureText(ByVal txt As String) As Integer
            Dim wdth As Integer
            Using gr As Graphics = TextBox1.CreateGraphics
                wdth = gr.MeasureString(txt, TextBox1.Font).Width
            End Using
            Return wdth
        End Function 'Measure length of 'txt' in pixels.
    This is fine up to a point, sadly, if there's a vbTab in the measured string it will only count as one character and I get a false measurement.
    I can't find way around this. I considered adding the length of a vbTab, (measure the difference between two strings, one with one vbTab and the other with two) but of course when a vbTab is used the new length of string is only up to the next tab position.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Measure the length of a string in pixels.

    Try replacing a tab with four spaces

  3. #3

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Measure the length of a string in pixels.

    Quote Originally Posted by .paul. View Post
    Try replacing a tab with four spaces
    Sorry, this is my fault, I should've explained what I'm trying to do.
    I'm reading in a whole load of data strings consisting of four separate sections per string, these sections can be of any length, but I want to display and print 'em to a text file in four distinct columns.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Measure the length of a string in pixels.

    Quote Originally Posted by Poppa Mintin View Post
    I want to display and print 'em to a text file in four distinct columns.
    Text files don't support columns, well not with good results. If you wanted to put them in a ListBox, I can show you columns. As for the textfile, you'd be better off with csv

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Measure the length of a string in pixels.

    Here's a ListBox with three columns...

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        <DllImport("user32.dll")> _
      Private Shared Function SendMessage( _
        ByVal hWnd As IntPtr, _
        ByVal wMsg As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
        End Function
    
        Private Sub SetTabStops(ByVal listBox As ListBox, ByVal tabStops() As Integer)
            Const LB_SETTABSTOPS As Integer = &H192
            Dim pinnedValues As GCHandle
            pinnedValues = GCHandle.Alloc(tabStops, GCHandleType.Pinned)
            Dim ptr As IntPtr = pinnedValues.AddrOfPinnedObject()
            SendMessage(listBox.Handle, LB_SETTABSTOPS, New IntPtr(tabStops.Length), ptr)
            pinnedValues.Free()
            listBox.Refresh()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Add("Product" & vbTab & "Description" & vbTab & "Price")
            ListBox1.Items.Add("BLT" & vbTab & "Sandwich with bacon, lettece and tomatoes" & vbTab & "7")
            ListBox1.Items.Add("PZVES" & vbTab & "Pizza with tomatosauce, cheese and ham" & vbTab & "12")
            ListBox1.Items.Add("BRGCH" & vbTab & "Cheese Burger" & vbTab & "9")
            ListBox2.Items.AddRange(ListBox1.Items)
            SetTabStops(ListBox2, New Integer() {70, 240})
        End Sub
    
    End Class
    EDIT: Actually, it's two ListBoxes, to compare the differently displayed items

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Measure the length of a string in pixels.

    You can also do columns in a TextBox, with String.Format - Control spacing

    https://docs.microsoft.com/en-us/dot...t?view=net-5.0

  7. #7

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Measure the length of a string in pixels.

    Quote Originally Posted by .paul. View Post
    Text files don't support columns, well not with good results. If you wanted to put them in a ListBox, I can show you columns. As for the textfile, you'd be better off with csv
    Thanks .Paul,

    I know text files don't support columns, that's the problem, but I'm getting there, the first two columns are fine, and the fourth would be too if I could manage the sort out the third column. Just struggling with the tab.

    I measure the width of each first element, then store those elements in a list of string each padded out with spaces to be the same length as each other, then add a tab so that the next element will be vertically aligned.
    So far, I'm breaking down each element of a data string into it's elements as strings and trying to do as described above to each. Then I try to concatenate 'em all into one string to display and print.

    Still struggling a bit, I was (am) hoping someone can suggest a way to measure a string that has a tab in it.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Measure the length of a string in pixels.

    reading the whole thread twice, i still dont get it.
    do you want to print (to a printer) or do you want to display it on screen or do you want to write it to a text file?
    you mentioned all these.
    printing would prob. be the only reason why you should be interested in pixel width.
    all others, you should prob. do with padding with spaces.
    for diplay in controls you might want to try a fixedsize font like "lucida console"

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Measure the length of a string in pixels.

    Quote Originally Posted by digitalShaman View Post
    reading the whole thread twice, i still dont get it.
    do you want to print (to a printer) or do you want to display it on screen or do you want to write it to a text file?
    you mentioned all these.
    printing would prob. be the only reason why you should be interested in pixel width.
    all others, you should prob. do with padding with spaces.
    for diplay in controls you might want to try a fixedsize font like "lucida console"
    Hi Shaman, I want to be able to do all of the above.

    I really don't like unisize fonts, although I know they can make some things easier.

    In the utility that I'm writing I use the same font in my TextBox that I use in Notepad, displaying the text on screen allows me to see the progress and to check that everything is OK before I save it to a text file.
    After the the text has been displayed, I have the option of producing a text file or not, choosing to save the file also displays the new text file on screen in Notepad and closes the app.


    Poppa

    PS, I have a solution.

    Pop
    Along with the sunshine there has to be a little rain sometime.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Measure the length of a string in pixels.

    Thanks for your help guys,

    I have a working solution which produces a neat text file an example of which I've just deleted from this post because when it displays in here (in a Quote) all the formatting is removed

    Taking a copy and paste into my Notepad (Comic Sans MS Regular at 14 point), the formatting is still removed.

    So... for anyone who may be interested there is a sample on OneDrive: Here
    To see it properly it should be viewed in the font above.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: [RESOLVED] Measure the length of a string in pixels.

    Get the width of a vbTab... then multiply it by the number of times vbTab appears int the string.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width