Solved

text wrapping

Posted on 1998-05-26
19
619 Views
Last Modified: 2008-02-01
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)
0
Comment
Question by:crf
  • 7
  • 4
  • 3
  • +5
19 Comments
 
LVL 6

Expert Comment

by:clifABB
ID: 1461642
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.
0
 

Author Comment

by:crf
ID: 1461643
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..
0
 
LVL 1

Expert Comment

by:lbk
ID: 1461644
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.
0
 
LVL 1

Expert Comment

by:EvanL
ID: 1461645
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.


0
 
LVL 1

Expert Comment

by:EvanL
ID: 1461646
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
0
 

Author Comment

by:crf
ID: 1461647
the problem now is i will have different fontsize so when the fontsize is not the same your codes does not work anymore....
0
 
LVL 1

Expert Comment

by:EvanL
ID: 1461648
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..
0
 
LVL 1

Expert Comment

by:EvanL
ID: 1461649
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..
0
 
LVL 4

Expert Comment

by:zsi
ID: 1461650
I know how to do this but I am too tired. :)

Hey ClifABB!!!  Think "TextMetrics"!


My gift to you.  ;)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:MAVERICK
ID: 1461651
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....

0
 

Author Comment

by:crf
ID: 1461652
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...../
0
 

Author Comment

by:crf
ID: 1461653
Adjusted points to 215
0
 
LVL 1

Expert Comment

by:MAVERICK
ID: 1461654
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!!!

0
 

Author Comment

by:crf
ID: 1461655
do u noe how can u detect the position of the rich text box
0
 

Author Comment

by:crf
ID: 1461656
Adjusted points to 230
0
 
LVL 1

Expert Comment

by:KEWL
ID: 1461657
Try the 'ScaleY' method to find out how many pixels per character there is?
0
 

Author Comment

by:crf
ID: 1461658
kewl how do i do it????
0
 

Expert Comment

by:vamsi_k
ID: 1461659
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.

0
 
LVL 1

Accepted Solution

by:
MAVERICK earned 230 total points
ID: 1461660
Refer to my homepage:
http://www.geocities.com/SiliconValley/Haven/4011/
There will be a VB project 'Jon4'
Run it and it should solve your problem!

For the record, Here's the Code section
Private Sub RichTextBox1_Change()
a = RichTextBox1.GetLineFromChar(Len(RichTextBox1.Text))
'Text1.Text = a
If a < 5 Then ' Allows the box to scroll after 6 lines
RichTextBox1.Height = 400 + (a * 200)
b = RichTextBox1.SelStart
RichTextBox1.SelStart = 0 ' The following lines prevent the text from
RichTextBox1.SetFocus     ' Scrolling Top
RichTextBox1.SelStart = b
RichTextBox1.SetFocus
End If

End Sub

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

20 Experts available now in Live!

Get 1:1 Help Now