Results 1 to 3 of 3

Thread: [RESOLVED] Size Rich Text Box using size of text

  1. #1

    Thread Starter
    Member CherryPickle's Avatar
    Join Date
    Aug 2009
    Location
    South Africa
    Posts
    40

    Resolved [RESOLVED] Size Rich Text Box using size of text

    Hi All,

    I have a rich text box that have a paragraph in it (the parragraph may change). The user is able to decide what type and/or size of font he whishes to view the text in. My problem is that not more that an average of four words per line should be displayed per line. My idea was to size the rtb so that it automatically wrap the text to and average of 4 words per line. This I would do by determining the average width of a word (number of characters) in the paragraph, multiplying it by the width in pixels or twips of a character and then multiplying by 4 to get the average required length of a line. It does not need to be exact as long as I can get as close as possible to 4 words per line.

    Sample text:

    My Father walks to the market every day to buy fresh bread and milk for the house.

    Result expected:

    My Father walks
    to the market every
    day to buy fresh
    bread and milk for
    the house.

    Notice some lines may be slightly longer as long as the average is 4 words per line.

    My problem is that I dont know how to determine the width of a character in order to ultimately determine the width of a sentence.

    I would appreciate some help.

    If there is a better way of solving the problem, I am open to suggestions.

    Regards

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

    Re: Size Rich Text Box using size of text

    try this:

    vb Code:
    1. Dim gr As Graphics = Graphics.FromHwnd(RichTextBox1.Handle)
    2. Dim wordSize As SizeF
    3. wordSize = gr.MeasureString("word", RichTextBox1.Font, RichTextBox1.Size, Drawing.StringFormat.GenericTypographic)

  3. #3

    Thread Starter
    Member CherryPickle's Avatar
    Join Date
    Aug 2009
    Location
    South Africa
    Posts
    40

    Re: Size Rich Text Box using size of text

    Thanx Paul,

    It did the trick.

    Your help was much appreciated.

Tags for this Thread

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