Results 1 to 6 of 6

Thread: [RESOLVED] Why is my line length wrong.

  1. #1

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

    Resolved [RESOLVED] Why is my line length wrong.

    Hi,

    I'm attempting to tidy the display of dozens of words in a Textbox, the words are all listed in groups by the number of letters in the word and then in alphabetical order within each group. The TextBox font is monospaced, all the characters are therefore the same width, so...
    I measure the width of xxx, subtract the width of xx which gives me the width of one character. (measuring just one x gives a different, wider result, adding (say) 10 x's to both measurements makes no difference to the result.)
    Next, I divide the width of the TextBox by the width of a character to find the maximum number of characters I can place in one line, I subtract 2 just to be on the right side.
    That's the theory.

    I have a function to measure the character width: -
    Code:
        Public Function MeasureText(ByVal txt As String) As Integer
            Using gr As Graphics = Form2.TextBox1.CreateGraphics
                Return CInt(gr.MeasureString(txt, Form2.TextBox1.Font).Width)
            End Using
        End Function
    (The function isn't in the same form as the TextBox).

    This is the relavent part of the subroutine which is supposed to ensure that each line of text terminates before any two word entry can break up the line following. (Only the letters are counted, so for example 'pea pod' or 'not-out' are counted as a 6 letter words).

    tbxW: integer variable containing TextBox1.Width.
    tbtxt: string containing the next line for TextBox1.
    nxt: string containing the next word for tbtxt.
    Code:
      If tbtxt.Length + nxt.Length > tbxW Then
                            Display1.Text &= tbtxt & vbNewLine & "   " 
                            tbtxt = nxt
                        Else
                            tbtxt &= nxt 
                        End If
    Well that's the theory, in practice each line terminates some 20 odd characters short.
    I don't understand why this should be. The form and the TextBox are 'Shown'... And 'Refreshed' before it's width or the character width is measured.
    Code:
     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            WindowState = FormWindowState.Maximized 
            FormBorderStyle = FormBorderStyle.None
            Me.Show()
            Me.Refresh()
            TextBox1.Clear()
            tbxW = MeasureText("xxx")
            tbxW -= MeasureText("xx")
            tbxW = (TextBox1.Width \ tbxW) - 2  ' Maximum characters in string in TextBox1. (Form2)
    Oh! TextBox1 has a vertical scrollbar, I wonder if that makes any difference ?

    Poppa
    Last edited by Poppa Mintin; Aug 5th, 2022 at 07:13 PM. Reason: Added extra information.
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Why is my line length wrong.

    Could you provide an example of the input and the outputs you are getting and expect?

  3. #3

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

    Re: Why is my line length wrong.

    Quote Originally Posted by jmcilhinney View Post
    Could you provide an example of the input and the outputs you are getting and expect?
    Sure...

    The input might be:-
    abreact, abreast, aerates, banters, bearcat, beaters, berates, cabaret, canters, careens, carnets, caserne, abreacts, acerbate, arsenate, bank rate, bearcats, cabarets, cabernet, cesarean, reenacts, acer, acre, arcs, area, arse, arts, bare, barn, bars, bear, beer, brae, bran, bras, brat, carb, care, cars, acerb, acers, acres, areas, areca, arena, aster, bares, barns, baser, bears, beers, beret, brace, bract, acerbates, aerate, arcane, arecas, arenas, astern, banter, barest, beater, benter, berate, berets, braces, bracts, braes, brats, caner, carat, cares, caret, carne, carte, carts, cater, crabs, crane, crate, crest, earns, breast, caners, canter, carats, careen, carets, carnet, cartes, caster, caters, censer, center, centre, cart, cern, crab, cran, earn, ears, eras, erne, erse, narc, near, race, rant, rata, rate, rats, rent, rest, centers, cerates, creates, crenate, cretans, earnest, eastern, nearest, nectars, rebates, recants, reenact, cerate, cetera, cranes, crates, crease, create, eaters, enters, erects, nacres, neater, nectar, nester, easer, eater, enter, erase, erect, ester, nacre, nears, races, rants, rates, react, rebec, recta, rents, reacts, rebate, recant, recast, recent, reseat, resent, scarab, screen, seater, secret, sterna, teaser, reset, saber, sabre, sacra, saner, saran, scare, snare, sneer, stare, steer, stere, stern, tares, tarns, rete, scar, sear, seer, sera, sere, star, tare, tarn, tars, tear, tern, tree, tsar, seabear, trances, tears, terns, terse, trace, trees, tenser, traces, trance.


    And I'd expect the out put to be:-
    (I can't find a monospace font option for the output so I've left the default, so it's not quite as tidy as it would be).

    acer, acre, arcs, area, arse, arts, bare, barn, bars, bear, beer, brae, bran, bras, brat, carb, care, cars,
    cart, cern, crab, cran, earn, ears, eras, erne, erse, narc, near, race, rant, rata, rate, rats, rent, rest,
    rete, scar, sear, seer, sera, sere, star, tare, tarn, tars, tear, tern, tree, tsar.

    acerb, acers, acres, areas, areca, arena, aster, bares, barns, baser, bears, beers, beret, brace, bract,
    braes, brats, caner, carat, cares, caret, carne, carte, carts, cater, crabs, crane, crate, crest, earns,
    easer, eater, enter, erase, erect, ester, nacre, nears, races, rants, rates, react, rebec, recta, rents,
    reset, saber, sabre, sacra, saner, saran, scare, snare, sneer, stare, steer, stere, stern, tares, tarns,
    tears, terns, terse, trace, trees.

    aerate, arcane, arecas, arenas, astern, banter, barest, beater, benter, berate, berets, braces, bracts,
    breast, caners, canter, carats, careen, carets, carnet, cartes, caster, caters, censer, center, centre,
    cerate, cetera, cranes, crates, crease, create, eaters, enters, erects, nacres, neater, nectar, nester,
    reacts, rebate, recant, recast, recent, reseat, resent, scarab, screen, seater, secret, sterna, teaser,
    tenser, traces, trance.

    abreact, abreast, aerates, banters, bearcat, beaters, berates, cabaret, canters, careens, carnets, caserne,
    centers, cerates, creates, crenate, cretans, earnest, eastern, nearest, nectars, rebates, recants, reenact,
    seabear, trances.

    abreacts, acerbate, arsenate, bank rate, bearcats, cabarets, cabernet, cesarean, reenacts.

    acerbates.


    Except (in this example) even the longest line (starting 'abreact') is still 14 characters too short for the textbox that it's in; and the shortest (starting 'aerate') is 17 characters short. Obviously there is going to be a difference due to differences in word length, but I'd expect at least the 4 letter words (6 characters) to include two more words per line, even the six letter words (8 characters) should be two words longer.

    Add a monospace font option to the list... Something to consider ?


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

  4. #4

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

    Re: Why is my line length wrong.

    Just a quick up-date,

    I removed the scrollbar to see if it made any difference...
    It didn't.


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

  5. #5
    Lively Member
    Join Date
    Aug 2014
    Posts
    65

    Re: Why is my line length wrong.

    Code:
       Public Function MeasureText(ByVal txt As String) As Integer
            Using gr As Graphics = Form2.TextBox1.CreateGraphics
                Return CInt(gr.MeasureString(txt, Form2.TextBox1.Font).Width)
            End Using
        End Function
    This parts round off the number. I get results of 6 and 7 (Courrier new) where the real number is 6.7998 (rounded off)
    Code:
    Public Function MeasureText(ByVal txt As String) As Double 'changed integer to double
            Using gr As Graphics = Form2.TextBox1.CreateGraphics
                Return (gr.MeasureString(txt, Form2.TextBox1.Font).Width) 'removed cint
            End Using
    End Function
    I changed to integer to double and removed the Cint. Also make sure you declare the variable tbxW As Double. And then it gives me a number with a , in it. To be fair it still isn't completely the same but it should be close enough for your program (diffence between the highest number and lowest number = 0.0003). And I think you already noticed it but the first character is off so you need to do xxx - xx for the correct number.

    Also the scrollbar might indeed give a small problem. The width of the textbox is indeed with the scrollbar. Change "tbxW = (TextBox1.Width \ tbxW) - 2" to "tbxW = (TextBox1.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth \ tbxW) - 2" and it should just use to white space to calculate the available text space.
    Last edited by THIEF15; Aug 6th, 2022 at 05:41 PM.

  6. #6

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

    Re: [RESOLVED] Why is my line length wrong.

    Thanks for your help guys... I've got it fixed. I was trying to tidy-up an old application.
    There was nothing wrong with my theory, it all works beautifully...
    Especially when I don't change the flamin' font size of the relevant textboxes half-way through running the routines !!

    I'm sorry for wasting your time, I should've looked more closely at the rest of the coding.

    Oh! and yes, Courier New IS a monospace font, It just didn't seem to work as one when I used 'Preview Post'.


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

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