Link to home
Start Free TrialLog in
Avatar of crf
crf

asked on

text wrapping

say if at first the richtextbox only have its height enough for 1 line now when it reaches the end of the right it proceed to the next new line as it wrap the rtb willl wrap the text...now i wonder whether i can let the richtextbox automatically adjust the height to fit in the first and second line(just like insert text in powerpoint.) or maybe is there a way to for us to know that the text is wrap(jump to the next line)i mean is there any code which tell us that the richtextbox has wrap the text (jump to the next line)
Avatar of clifABB
clifABB

After assigning the text to the RichTextBox (or in the change event), use this code:
  RichTextBox1.Height = Form1.TextHeight("X") * (Int(Form1.TextWidth(RichTextBox1.Text) / RichTextBox1.Width + 0.5) * 1.6)

The only caveat is to make the Form and the RichTextBox have the same font.
Avatar of crf

ASKER

SORRY CLIFABB I THINK U DO NOT REALLY KNOW WHAT I AM LOOKING FOR WHAT I WANT IS THAT THE RICHTEXTBOX WILL AUTOMATICALLY ADJUST ITS HEIGHT WHEN IT AUTO WRAP THE TEXT TO THE NEXT NEW LINE AND MY RICHTEXTBOX CONTAINS CHARACTER OF DIFFERENT FONTS..
I don't think there really is a way to do this. The only hint I can give you is to use the Line() function of the rtb control. it gives you the line number of the ##th character.
My idea would be to continually check the length of the text string.  When the length equals a certain amount, then increase the height of the textbox.  The certain amount mentioned above would be the number of characters it takes to fill a line.  You could have a select case statement in the text change event that would check to see if the text string length has reached certain points yet - and if so, increase the textbox height by X amount.


The following code works.  You have to set the right margin of the text box correctly.  In my example, when the text length reaches 20, then increase the height of the text box.  You could have multiple If Thens or a Select Case.

Private Sub RichTextBox1_Change()
If Len(RichTextBox1.Text) = 20 Then
    RichTextBox1.Height = 750
Else
End If
End Sub
Avatar of crf

ASKER

the problem now is i will have different fontsize so when the fontsize is not the same your codes does not work anymore....
Hmmm...  very true.  I assumed you would have a basic fixed-width font.  Guess it will work if you do it that way, but if you are loading something like a Word document into the rich textbox, it will hit the next line before it reaches the number of characters it would take to fill the line.  The only thing I can think of is to come up with some kind of ratio.  If the text font size is equal to a certain number, then the number of characters in one line of text before the textbox would increase in height would change.

Sounds like a difficult problem to solve if you are using multiple fonts and font sizes.

I'll keep working on it and get back to you soon..
What about making the textbox width wide enough to accept any font so that the first, say, 50 characters were always on the first line, no matter what happened?

You would also run into trouble with carriage returns.

Guess you should reopen the question for others because I'm out of ideas... sorry..
I know how to do this but I am too tired. :)

Hey ClifABB!!!  Think "TextMetrics"!


My gift to you.  ;)
I worked on a app where I clicked a cursor and the position in the RTF box was noted.... even though this may have aparently (absolutely!!!) nothing to do with your problem, It may be adaptable to solve your problem....

Avatar of crf

ASKER

yes maverick i think that can help me do what i can can u kindly provide me with more informations on the app u r refering to ???thank you...../
Avatar of crf

ASKER

Adjusted points to 215
The original app was taken during a hard drive crash!
I will try to write the code suitable to your purpose as soon as time permits!
The concept is the important part!!!

Avatar of crf

ASKER

do u noe how can u detect the position of the rich text box
Avatar of crf

ASKER

Adjusted points to 230
Try the 'ScaleY' method to find out how many pixels per character there is?
Avatar of crf

ASKER

kewl how do i do it????
To be frank, I also faced this kind of problem lately.  I have created a document, in MS-Word, and saved it as RTF file format.  Next, in VB, using RichTextBox control, I have loaded this file.  Actually, while in Word, the lines were truncated automatically, when they exceeded the horizontal width of the page.  But when loaded onto the RichTextBox, the line is not truncating and going to the next line, instead, horizontal scroll bars appeared, and the line is printed as one single line.  I have to move right through the horizontal bar in-order to view the line completely.  Solution to this problem, I think will solve your problem also.

ASKER CERTIFIED SOLUTION
Avatar of MAVERICK
MAVERICK
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial