?
Solved

text wrapping

Posted on 1998-05-26
19
Medium Priority
?
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 460 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 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