RichTextBox WordWrap

  • Follow


I cannot get my RTB to wrap properly.
I am set for vert scrollbar or both with (fiddled with each) 
The text does wrap bu not at the end of the actual proper point.
Several words ar off screen right.
As I resize, the text wrap changes but still leaves several words off the 
right side.
I tried setting right margin with no luck.
Must be something else I amd not doing.
I tried different fonts: MSSANSERIF, Arial and Lucida Console(preferred). 
The text is loaded from a .rtf file created in MS WordPad since I do use 
Bold and Underline in the text.  The text has also been through Word 2003 and 
also OpenOffice Writer.  Does that do something bad?

If I run through Notepad I will loose all my text formatting so I would 
rather not do that.

0
Reply Utf 4/3/2010 9:17:01 PM

I have tried these two methods and neither one works.
Both still leave text past the vertical scrollbar.  No horz scrollbar.

(A)
Public Sub RTBWordWrap(RTB As RichTextBox, Optional bSet As Boolean = True)

    If bSet Then
        ' then to set word wrap, use this :
        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 0
    Else
        ' and to do the oposite use this one :
        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 1
    End If

End Sub 'RTBWordWrap

(B) in _Resize
     RichTextBox.RightMargin = RichTextBox.Width
If I do
     RichTextBox.RightMargin = RichTextBox.Width * .75
this woks better for wide RTB but when the RTB gets narrow, words again go 
past the vert scrollbar.

Does this have to do with thiings like Bold etc that may not be accounted 
for correctly?

I am using rich text not plain text.

0
Reply Utf 4/3/2010 10:33:01 PM


Do you want the RTB to wrap text at the right-edge (or before the left
edge of the vertical scrollbar) no matter what the width of the RTB?
If so, setting the RightMargin property to 0 works for me. If this
doesn't work for you, what OS are you using? What version of
richtx32.ocx do you have?
0
Reply JPB 4/4/2010 2:25:56 AM

|        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 0
|    Else
|        ' and to do the oposite use this one :
|        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 1


  That version works for me. I'm using RichEdit v. 2/3 rather
than the OCX, but I doubt that matters.
    You said you have no horiz. scrollbar. How would you *not*
have wordwrap if there's no horiz. scrollbar? And how could you
provide an option if there's no horiz. scrollbar?


0
Reply Mayayana 4/4/2010 4:22:25 AM

  I just looked up what I'd used when I was using the
OCX. Like JPB, I was setting RightMargin to 0 for
wrap and setting it to 50,000 for no wrap. I don't
think you have to deal with resizing.

According to the docs:

The default for the RightMargin property is 0 and will cause the control to 
set text wrapping equal to the right most part of the RichTextBox control so 
all text is viewable.

  If you're doing
RichTextBox.RightMargin = RichTextBox.Width * .75
that's giving you 3/4 of the RTB width beyond the
RTB for text. If you have some long lines that can
cause partial wordwrap. 


0
Reply Mayayana 4/4/2010 4:32:28 AM

I I use plain text, then there is no problem.
But if I use richtext, it is then that the word wrap is beyond the right 
edge of the control.  The richtrxt contains bold, underline, tabs, ad a 
single but larger font size.   I have tried different fonts but that does not 
change anything.
I have modified my algo a little to use the SQR() to give aqn exponention 
factor and this seems to keep the right wrpa approximately the same as the 
RTB is sized.
Otherwise the right wrap moves fartyre to the left proportionally as the box 
is expanded.



"Bee" wrote:

> I have tried these two methods and neither one works.
> Both still leave text past the vertical scrollbar.  No horz scrollbar.
> 
> (A)
> Public Sub RTBWordWrap(RTB As RichTextBox, Optional bSet As Boolean = True)
> 
>     If bSet Then
>         ' then to set word wrap, use this :
>         SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 0
>     Else
>         ' and to do the oposite use this one :
>         SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 1
>     End If
> 
> End Sub 'RTBWordWrap
> 
> (B) in _Resize
>      RichTextBox.RightMargin = RichTextBox.Width
> If I do
>      RichTextBox.RightMargin = RichTextBox.Width * .75
> this woks better for wide RTB but when the RTB gets narrow, words again go 
> past the vert scrollbar.
> 
> Does this have to do with thiings like Bold etc that may not be accounted 
> for correctly?
> 
> I am using rich text not plain text.
> 
0
Reply Utf 4/4/2010 2:29:01 PM

Bee,

there is something weird in your app.
Try a new test app, simple put a RTB there, don't set _any_ properies
for the RTB. Save any Word document you have (say a letter) as rtf
and load _this_ rtf file into the RTB. I bet it will display ok!
Then test it with your special rtf files. If they display ok, it's one of the
settings in your app.

Helmut.


"Bee" <Bee@discussions.microsoft.com> schrieb im Newsbeitrag 
news:DB5E2736-2DA9-48F0-805A-BC8E116C9707@microsoft.com...
>I I use plain text, then there is no problem.
> But if I use richtext, it is then that the word wrap is beyond the right
> edge of the control.  The richtrxt contains bold, underline, tabs, ad a
> single but larger font size.   I have tried different fonts but that does not
> change anything.
> I have modified my algo a little to use the SQR() to give aqn exponention
> factor and this seems to keep the right wrpa approximately the same as the
> RTB is sized.
> Otherwise the right wrap moves fartyre to the left proportionally as the box
> is expanded.
>
>
>
> "Bee" wrote:
>
>> I have tried these two methods and neither one works.
>> Both still leave text past the vertical scrollbar.  No horz scrollbar.
>>
>> (A)
>> Public Sub RTBWordWrap(RTB As RichTextBox, Optional bSet As Boolean = True)
>>
>>     If bSet Then
>>         ' then to set word wrap, use this :
>>         SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 0
>>     Else
>>         ' and to do the oposite use this one :
>>         SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 1
>>     End If
>>
>> End Sub 'RTBWordWrap
>>
>> (B) in _Resize
>>      RichTextBox.RightMargin = RichTextBox.Width
>> If I do
>>      RichTextBox.RightMargin = RichTextBox.Width * .75
>> this woks better for wide RTB but when the RTB gets narrow, words again go
>> past the vert scrollbar.
>>
>> Does this have to do with thiings like Bold etc that may not be accounted
>> for correctly?
>>
>> I am using rich text not plain text.
>>


0
Reply Helmut 4/4/2010 4:46:19 PM

"Bee" <Bee@discussions.microsoft.com> wrote in message 
news:D748AC50-305E-41D7-8DF6-C01F8D98BCCF@microsoft.com...

> I cannot get my RTB to wrap properly. I have tried these
> two methods and neither one works. Both still leave text
> past the vertical scrollbar. The text does wrap but not at the
> end of the actual proper point. As I resize, the text wrap
> changes but still leaves several words off the right side.
> [some stuff snipped]
> RichTextBox.RightMargin = RichTextBox.Width
> the following woks better for wide RTB but when the
> RTB gets narrow, words again go past the vert scrollbar.
> RichTextBox.RightMargin = RichTextBox.Width * .75

If you just want the contents to wrap properly regardless of the current 
Width of the RTB, whatever you to its size it to, then setting its Multiline 
property to True and its RightMargin property to zero at design time (or in 
code) should do the trick. You should not need to keep doing it every time 
you resize. If that is not what is happening at your end then perhaps you 
are using and old version of the RichTextBox control? The RTB that shipped 
with VB5 had problems with a zero RightMargin setting, causing it to fail to 
wrap unless you set the RightMargin to a positive value. Is that why you are 
repeatedly setting the RightMargin  each time you resize it? Are you using 
an old RTB in VB5?

If you are using VB5 then have you checked whether there is an updated RTB 
that you can download for it that does not have the zero RightMargin 
problem? Otherwise, if you are stuck with setting its RightMargin to a real 
positive value in order to overcome the bug then you need to set it to the 
correct value (this would also apply if for some reason you were setting the 
RightMargin to a real positive value in the VB6 RTB).

In your code samples you are setting the RightMargin to the Width of the 
RTB, which of course is its overall width, including its borders and the 
vertical scroll bar (if you are displaying one). That's probably why some of 
the text is going past the right edge of its available client area, which is 
of course narrower than its overall width. To get the correct value you need 
to check its client width. You can get this using the GetClientRect API, 
which will return a RECT containing the size of the RTB's client area. Then 
subtract a couple of pixels from that value (because the RTB by default has 
at least a one pixel left margin) and use that calculated value to set the 
RTB's RightMargin property. You'll need to convert the calculated pixel 
value into the scale units of the RTB's container (your Form or whatever) 
because the RightMargin property of the RTB is the distance in container 
scale units from the left edge of the RTB, and NOT the distance in twips 
from the right edge of the RTB as stated in the VB6 help files!).

Mike










>
> (A)
> Public Sub RTBWordWrap(RTB As RichTextBox, Optional bSet As Boolean = 
> True)
>
>    If bSet Then
>        ' then to set word wrap, use this :
>        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 0
>    Else
>        ' and to do the oposite use this one :
>        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 1
>    End If
>
> End Sub 'RTBWordWrap
>
> (B) in _Resize
>     RichTextBox.RightMargin = RichTextBox.Width
> If I do
>     RichTextBox.RightMargin = RichTextBox.Width * .75
> this woks better for wide RTB but when the RTB gets narrow, words again go
> past the vert scrollbar.
>
> Does this have to do with thiings like Bold etc that may not be accounted
> for correctly?
>
> I am using rich text not plain text.
> 

0
Reply Mike 4/4/2010 9:03:54 PM

This first thing I did was to check the version.
About shows it as V6 SP6.

I copied a RTB from a different part of the app (on a different form) over 
this the misbehaving form.
Hoping that some parameter would be the problem.
The copied RTB was using plain text set to Lucida Consols and it was 
wrapping correctly.
After the copy and rename, the copied form FAILED! just like the one taht 
replaced it.  I searched the code source for any RTB settings and found 
nothing.
I made sure I set the Multiline and RightMargin=0 as was before.
The wrap occurs several words past the right vertical scroll bar not just 
into the vert scroll bar.
The wrap changes location relative to the width the RTB is resized.  i.e. 
narrow has less distance to the right side and wide has a lot of distance.
It must have something to do with the rich text that theRTB is trying to 
display.
This rich text was edited in a number of editors but always saves as .rtf 
but ???
I used WordPad, Word 2003 and OpenOffice Writer (latest).
I am wondering if some hidden control characters are fooling the wrap algo.

I will try loading some plain text and see.
I will take the rich text and run it through Notepad to remove all the 
hidden stuff.
(load in Wordpad, select all, copy, paste into Notepad).


"Mike Williams" wrote:

> "Bee" <Bee@discussions.microsoft.com> wrote in message 
> news:D748AC50-305E-41D7-8DF6-C01F8D98BCCF@microsoft.com...
> 
> > I cannot get my RTB to wrap properly. I have tried these
> > two methods and neither one works. Both still leave text
> > past the vertical scrollbar. The text does wrap but not at the
> > end of the actual proper point. As I resize, the text wrap
> > changes but still leaves several words off the right side.
> > [some stuff snipped]
> > RichTextBox.RightMargin = RichTextBox.Width
> > the following woks better for wide RTB but when the
> > RTB gets narrow, words again go past the vert scrollbar.
> > RichTextBox.RightMargin = RichTextBox.Width * .75
> 
> If you just want the contents to wrap properly regardless of the current 
> Width of the RTB, whatever you to its size it to, then setting its Multiline 
> property to True and its RightMargin property to zero at design time (or in 
> code) should do the trick. You should not need to keep doing it every time 
> you resize. If that is not what is happening at your end then perhaps you 
> are using and old version of the RichTextBox control? The RTB that shipped 
> with VB5 had problems with a zero RightMargin setting, causing it to fail to 
> wrap unless you set the RightMargin to a positive value. Is that why you are 
> repeatedly setting the RightMargin  each time you resize it? Are you using 
> an old RTB in VB5?
> 
> If you are using VB5 then have you checked whether there is an updated RTB 
> that you can download for it that does not have the zero RightMargin 
> problem? Otherwise, if you are stuck with setting its RightMargin to a real 
> positive value in order to overcome the bug then you need to set it to the 
> correct value (this would also apply if for some reason you were setting the 
> RightMargin to a real positive value in the VB6 RTB).
> 
> In your code samples you are setting the RightMargin to the Width of the 
> RTB, which of course is its overall width, including its borders and the 
> vertical scroll bar (if you are displaying one). That's probably why some of 
> the text is going past the right edge of its available client area, which is 
> of course narrower than its overall width. To get the correct value you need 
> to check its client width. You can get this using the GetClientRect API, 
> which will return a RECT containing the size of the RTB's client area. Then 
> subtract a couple of pixels from that value (because the RTB by default has 
> at least a one pixel left margin) and use that calculated value to set the 
> RTB's RightMargin property. You'll need to convert the calculated pixel 
> value into the scale units of the RTB's container (your Form or whatever) 
> because the RightMargin property of the RTB is the distance in container 
> scale units from the left edge of the RTB, and NOT the distance in twips 
> from the right edge of the RTB as stated in the VB6 help files!).
> 
> Mike
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> >
> > (A)
> > Public Sub RTBWordWrap(RTB As RichTextBox, Optional bSet As Boolean = 
> > True)
> >
> >    If bSet Then
> >        ' then to set word wrap, use this :
> >        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 0
> >    Else
> >        ' and to do the oposite use this one :
> >        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 1
> >    End If
> >
> > End Sub 'RTBWordWrap
> >
> > (B) in _Resize
> >     RichTextBox.RightMargin = RichTextBox.Width
> > If I do
> >     RichTextBox.RightMargin = RichTextBox.Width * .75
> > this woks better for wide RTB but when the RTB gets narrow, words again go
> > past the vert scrollbar.
> >
> > Does this have to do with thiings like Bold etc that may not be accounted
> > for correctly?
> >
> > I am using rich text not plain text.
> > 
> 
> .
> 
0
Reply Utf 4/5/2010 2:10:01 AM

OK.  Still not sure what the real problem is however ...

if I use the pure text (taken directly from the richtext) as outlined above 
and load it into the RTB as always, renamed .rtf (though really a .txt file) 
it works as follows:

(1) using .RightMargin = .Width the wrap occurs in the vert scrollbar but 
only by a few characters (not many word).  Still not very nice.

(2) using the sendmessage of RTBWordWrap() the wrap occurs well inside 
(really nice) the vert scrollbar.

But of course this is not the richtext I want!
Looks like the hidden formatting stuff is influencing the wrap position 
incorrectly.

Of course M$ will fix that in the next realease of VB6.  lol.  (really 
crying).

Now looking ofr the best workaround.
Is there such a thing as a richtext cleaner?


"Mike Williams" wrote:

> "Bee" <Bee@discussions.microsoft.com> wrote in message 
> news:D748AC50-305E-41D7-8DF6-C01F8D98BCCF@microsoft.com...
> 
> > I cannot get my RTB to wrap properly. I have tried these
> > two methods and neither one works. Both still leave text
> > past the vertical scrollbar. The text does wrap but not at the
> > end of the actual proper point. As I resize, the text wrap
> > changes but still leaves several words off the right side.
> > [some stuff snipped]
> > RichTextBox.RightMargin = RichTextBox.Width
> > the following woks better for wide RTB but when the
> > RTB gets narrow, words again go past the vert scrollbar.
> > RichTextBox.RightMargin = RichTextBox.Width * .75
> 
> If you just want the contents to wrap properly regardless of the current 
> Width of the RTB, whatever you to its size it to, then setting its Multiline 
> property to True and its RightMargin property to zero at design time (or in 
> code) should do the trick. You should not need to keep doing it every time 
> you resize. If that is not what is happening at your end then perhaps you 
> are using and old version of the RichTextBox control? The RTB that shipped 
> with VB5 had problems with a zero RightMargin setting, causing it to fail to 
> wrap unless you set the RightMargin to a positive value. Is that why you are 
> repeatedly setting the RightMargin  each time you resize it? Are you using 
> an old RTB in VB5?
> 
> If you are using VB5 then have you checked whether there is an updated RTB 
> that you can download for it that does not have the zero RightMargin 
> problem? Otherwise, if you are stuck with setting its RightMargin to a real 
> positive value in order to overcome the bug then you need to set it to the 
> correct value (this would also apply if for some reason you were setting the 
> RightMargin to a real positive value in the VB6 RTB).
> 
> In your code samples you are setting the RightMargin to the Width of the 
> RTB, which of course is its overall width, including its borders and the 
> vertical scroll bar (if you are displaying one). That's probably why some of 
> the text is going past the right edge of its available client area, which is 
> of course narrower than its overall width. To get the correct value you need 
> to check its client width. You can get this using the GetClientRect API, 
> which will return a RECT containing the size of the RTB's client area. Then 
> subtract a couple of pixels from that value (because the RTB by default has 
> at least a one pixel left margin) and use that calculated value to set the 
> RTB's RightMargin property. You'll need to convert the calculated pixel 
> value into the scale units of the RTB's container (your Form or whatever) 
> because the RightMargin property of the RTB is the distance in container 
> scale units from the left edge of the RTB, and NOT the distance in twips 
> from the right edge of the RTB as stated in the VB6 help files!).
> 
> Mike
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> >
> > (A)
> > Public Sub RTBWordWrap(RTB As RichTextBox, Optional bSet As Boolean = 
> > True)
> >
> >    If bSet Then
> >        ' then to set word wrap, use this :
> >        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 0
> >    Else
> >        ' and to do the oposite use this one :
> >        SendMessageLong RTB.hwnd, EM_SETTARGETDEVICE, 0, 1
> >    End If
> >
> > End Sub 'RTBWordWrap
> >
> > (B) in _Resize
> >     RichTextBox.RightMargin = RichTextBox.Width
> > If I do
> >     RichTextBox.RightMargin = RichTextBox.Width * .75
> > this woks better for wide RTB but when the RTB gets narrow, words again go
> > past the vert scrollbar.
> >
> > Does this have to do with thiings like Bold etc that may not be accounted
> > for correctly?
> >
> > I am using rich text not plain text.
> > 
> 
> .
> 
0
Reply Utf 4/5/2010 2:26:01 AM

"Bee" <Bee@discussions.microsoft.com> wrote in message 
news:668E1CA5-6D2C-421D-A968-8B7564B86AF1@microsoft.com...

> I copied a RTB from a different part of the app (on a different form)
> over this the misbehaving form. Hoping that some parameter would
> be the problem. The copied RTB was using plain text set to Lucida
> Consols and it was wrapping correctly. After the copy and rename,
> the copied form FAILED! just like the one taht  I searched the code
> source for any RTB settings and found nothing. I made sure I set the
> Multiline and RightMargin=0 as was before.

I'm still not sure what you are saying there, especially regarding "copy and 
rename". Somebody here told you to create a new and completely separate VB 
project containing just one Form with one RTB set to RightMargin zero and 
Multiline True and Scrollbar set to rtfVertical and load the "problem" .rtf 
file into it. Have you tried that? If so, what happens?

> The wrap occurs several words past the right vertical scroll bar
> not just  into the vert scroll bar. The wrap changes location relative
> to the width the RTB is resized.

Well I noticed in one of your code examples you were setting the rightmargin 
to a percentage of the Width. Are you still doing that?

> It must have something to do with the rich text
> that theRTB is trying to display.

That could well be, but you still need to check the simple basic stuff 
first. If you've checked all the basics, and specifically if you have tried 
what I said above (one RTB in a brand new otherwise completely empty project 
with the specific problem rtf file loaded into it), then you're probably 
right and the rtf probably does contain some fancy formatting information. 
In that case (AFTER you have tried what I said above) then you can try 
altering the paragraph formatting of the entire contents of the RTB, just to 
check what happens. To do that, start a completely new VB project with a 
Form containing one RTB and one Command Button. Leave the RTB at its default 
settings of RightMargin Zero and MultiLine True and set it to have a 
vertical scroll bar. Manually resize the RTB to a reasonably large size in 
the IDE. Then paste in the following code and change the hard coded 
path/file name in the Command1_Click event so that it loads the specific rtf 
file you are having problems with. Run the project and click the button. In 
this specific example you should end up with the rtf in the RichTextBox but 
all the paragraphs should be fully justified (straight edges at both the 
left and right sides of each paragraph) and there should be a 50 pixel blank 
left margin and a 50 pixel blank right margin. Is that what you get?

Mike

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias _
  "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
  ByVal wParam As Long, lParam As Any) As Long
Private Const MAX_TAB_STOPS As Long = 32
Private Type PARAFORMAT2
  cbsize As Integer
  pad As Integer
  dwMask As Long
  wNumbering As Integer
  wReserved As Integer
  dxStartIndent As Long
  dxRightIndent As Long
  dxOffset As Long
  wAlignment As Integer
  cTabCount As Integer
  lTabstops(0 To MAX_TAB_STOPS - 1) As Long
  dySpaceBefore As Long
  dySpaceAfter As Long
  dyLineSpacing As Long
  sStyle As Integer
  bLineSpacingRule As Byte
  bOutlineLevel As Byte
  wShadingWeight As Integer
  wShadingStyle As Integer
  wNumberingStart As Integer
  wNumberingStyle As Integer
  wNumberingTab As Integer
  wBorderSpace As Integer
  wBorderWidth As Integer
  wBorders As Integer
End Type
Private Const WM_USER As Long = &H400
Private Const EM_SETTYPOGRAPHYOPTIONS As Long = (WM_USER + 202)
Private Const EM_GETPARAFORMAT As Long = (WM_USER + 61)
Private Const EM_SETPARAFORMAT As Long = (WM_USER + 71)
Private Const TO_ADVANCEDTYPOGRAPHY As Long = &H1
Private Const PFM_ALIGNMENT As Long = &H8
Private Const PFM_OFFSET As Long = &H4
Private Const PFM_OFFSETINDENT As Long = &H80000000
Private Const PFM_RIGHTINDENT As Long = &H2
Private Const PFA_LEFT As Long = &H1
Private Const PFA_RIGHT As Long = &H2
Private Const PFA_CENTER As Long = &H3
Private Const PFA_JUSTIFY As Long = &H4

Private Sub SelFullyJustify(RTB1 As RichTextBox, _
        Justification As Long, leftMargin As Long, _
        rightMargin As Long)
Dim PF2 As PARAFORMAT2, retVal As Long
PF2.cbsize = Len(PF2)
retVal = SendMessage(RTB1.hwnd, _
        EM_SETTYPOGRAPHYOPTIONS, _
        TO_ADVANCEDTYPOGRAPHY, _
        ByVal TO_ADVANCEDTYPOGRAPHY)
If retVal <> 0 Then
  SendMessage RTB1.hwnd, EM_GETPARAFORMAT, 0&, PF2
  PF2.dwMask = PFM_ALIGNMENT Or PFM_OFFSET _
          Or PFM_OFFSETINDENT Or PFM_RIGHTINDENT
  PF2.wAlignment = Justification
  PF2.dxStartIndent = leftMargin * Screen.TwipsPerPixelX
  PF2.dxRightIndent = rightMargin * Screen.TwipsPerPixelX
  PF2.dxOffset = 0
  SendMessage RTB1.hwnd, EM_SETPARAFORMAT, 0&, PF2
End If
End Sub

Private Sub Command1_Click()
RichTextBox1.LoadFile "c:\temp\const.rtf"
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = Len(RichTextBox1.Text)
SelFullyJustify RichTextBox1, PFA_JUSTIFY, 50, 50
RichTextBox1.SelStart = 0
End Sub




0
Reply Mike 4/5/2010 8:35:45 AM

"Mike Williams" <Mike@WhiskyAndCoke.com> wrote in message 
news:%23P4u9rJ1KHA.224@TK2MSFTNGP06.phx.gbl...


By the way, in the code I posted in my previous response I called the 
function:

  Private Sub SelFullyJustify(RTB1 As RichTextBox  . . etc

That was the name I originally gave it and, as you will see from the 
example, I have since modified it so that it will do whatever justification 
(left, right, centre, full) and whatever left and right margins you pass to 
it, so if it works for you and if you keep it in your code you might wish to 
change its name accordingly. Perhaps you could call it:

  Sub SetTheCurrentSelectionMarginsAndJustification (RTB1 . . etc

I like long names ;-)

Mike



0
Reply Mike 4/5/2010 10:16:36 AM

Ignored questions:

"JPB" <jasonpeterbrown@gmail.com> wrote in message 
news:b2c5830f-cef1-44ef-855b-e3f615985119@10g2000yqq.googlegroups.com...
> what OS are you using? What version of
> richtx32.ocx do you have?



0
Reply Nobody 4/5/2010 2:09:06 PM

Wow!.  Mike you did it again.
Our posts must have crossed photons so ignore my previous last post.
Your code example WORKS PERFECTLY!

Many thanks.


"Mike Williams" wrote:

> "Mike Williams" <Mike@WhiskyAndCoke.com> wrote in message 
> news:%23P4u9rJ1KHA.224@TK2MSFTNGP06.phx.gbl...
> 
> 
> By the way, in the code I posted in my previous response I called the 
> function:
> 
>   Private Sub SelFullyJustify(RTB1 As RichTextBox  . . etc
> 
> That was the name I originally gave it and, as you will see from the 
> example, I have since modified it so that it will do whatever justification 
> (left, right, centre, full) and whatever left and right margins you pass to 
> it, so if it works for you and if you keep it in your code you might wish to 
> change its name accordingly. Perhaps you could call it:
> 
>   Sub SetTheCurrentSelectionMarginsAndJustification (RTB1 . . etc
> 
> I like long names ;-)
> 
> Mike
> 
> 
> 
> .
> 
0
Reply Utf 4/5/2010 5:11:29 PM

"Bee" <Bee@discussions.microsoft.com> wrote in message 
news:890253D9-9F9D-4F2F-946A-97672070B740@microsoft.com...

> Your code example WORKS PERFECTLY!

You're welcome. By the way, I used full justification in order that you 
could easily see the right edge of the text from any point in your document 
so that you could easily see whether your problem had been solved or not. 
You can of course change that to left justification or whatever else you 
wish. Also, and this is particularly important, I used a left and right 
paragraph indent of 50 pixels for all paragraphs in the document, again in 
order that you could easily see the results. Setting paragraph indents for 
the whole document in this way is not what you would normally want to do 
(there are other ways of setting "display" margins in the RTB if that is 
what you need to do). The reason I say that it is not normally wise to do it 
in the way I have done in the example is because if you save the document as 
rtf using RTB.SaveFile then those 50 pixel left and right indents (0.521 
inches on a computer running at the standard 96 dpi setting) will be part of 
the rtf document itself (rather than merely an effect of the visible RTB 
display) and therefore when you load the saved document into a word 
processor then those left and right indents of 0.521 inches will effecively 
be "added to" the standard default left and right page margins of your word 
processor. In other words, if your copy of Micro$oft Word or whatever 
normally uses a 1 inch left and right margin (as is typical in the UK) then 
you will end up with apparent 1.521 inch left and right margins (0.521 
inches of which are actually paragraph indents). This is fine if that is 
what you want, but it is not the normal requirement. Setting left and right 
paragraph indents in the way that I have done in the code I posted is 
normally what you would do to just one or two (or more) paragraphs of a 
document when you specifically want those paragraphs to be indented in at 
either side, differently from the rest of the paragraphs in the document. I 
just thought I would mention that point for completeness.

Mike


0
Reply Mike 4/5/2010 8:28:45 PM

And so it begs the question, how do I set the margins without making it 
"permanent"?

In one case I don't care since it is for display only.  Your sub is perfect 
for that.
In another case, the user edits the document and saves and reloads it later, 
maybe many times.
I am assuming that in order to use the justification, I need to set the 
non-permanent margins through the SendMessage or can I just use the standard 
RTB  margin settings? 

So for now I can set the margins to 0 thru your sub.



"Mike Williams" wrote:

> "Bee" <Bee@discussions.microsoft.com> wrote in message 
> news:890253D9-9F9D-4F2F-946A-97672070B740@microsoft.com...
> 
> > Your code example WORKS PERFECTLY!
> 
> You're welcome. By the way, I used full justification in order that you 
> could easily see the right edge of the text from any point in your document 
> so that you could easily see whether your problem had been solved or not. 
> You can of course change that to left justification or whatever else you 
> wish. Also, and this is particularly important, I used a left and right 
> paragraph indent of 50 pixels for all paragraphs in the document, again in 
> order that you could easily see the results. Setting paragraph indents for 
> the whole document in this way is not what you would normally want to do 
> (there are other ways of setting "display" margins in the RTB if that is 
> what you need to do). The reason I say that it is not normally wise to do it 
> in the way I have done in the example is because if you save the document as 
> rtf using RTB.SaveFile then those 50 pixel left and right indents (0.521 
> inches on a computer running at the standard 96 dpi setting) will be part of 
> the rtf document itself (rather than merely an effect of the visible RTB 
> display) and therefore when you load the saved document into a word 
> processor then those left and right indents of 0.521 inches will effecively 
> be "added to" the standard default left and right page margins of your word 
> processor. In other words, if your copy of Micro$oft Word or whatever 
> normally uses a 1 inch left and right margin (as is typical in the UK) then 
> you will end up with apparent 1.521 inch left and right margins (0.521 
> inches of which are actually paragraph indents). This is fine if that is 
> what you want, but it is not the normal requirement. Setting left and right 
> paragraph indents in the way that I have done in the code I posted is 
> normally what you would do to just one or two (or more) paragraphs of a 
> document when you specifically want those paragraphs to be indented in at 
> either side, differently from the rest of the paragraphs in the document. I 
> just thought I would mention that point for completeness.
> 
> Mike
> 
> 
> .
> 
0
Reply Utf 4/5/2010 11:19:35 PM

"Bee" <Bee@discussions.microsoft.com> wrote in message 
news:1F5258D6-152F-4576-9D94-DC896C76E04E@microsoft.com...

> And so it begs the question, how do I set the margins
> without making it "permanent" [1]?

Before I answer that question let me just say something about the code I 
posted yesterday. If you run the code and click the button then it will 
fully justify the contents of the RTB and it will set up the desired left 
and right paragraph indents for the entire document. However, with the code 
as it currently stands, if you click the button twice you will find that it 
increases the left indent (instead of simply setting it to the same value) 
and each time you click the button the left indent will get bigger. That is 
because I had inadvertently used the wrong constant. I had used 
PFM_OFFSETINDENT, which is a relative adjustment to the left indent, whereas 
I should have used PFM_STARTINDENT, which is an absolute left indent. So, in 
order to fix that problem add the following to your declarations:

    Private Const PFM_STARTINDENT As Long = &H1

.. . and change the mask line in the example code to:

    PF2.dwMask = PFM_ALIGNMENT Or PFM_OFFSET _
          Or PFM_STARTINDENT Or PFM_RIGHTINDENT

In fact I used the EM_SETPARAFORMAT method so that I could use 
ADVANCEDTYPOGRAPHY in order to set full justification, something which 
cannot be done using native RTB properties or methods and I included the 
indent settings in that method merely because I was using it anyway to set 
full justification. If you do not want to set full justification and you do 
not want to do anything else that cannot be done with the native VB methods 
then you can simply use the RTB SelIndent and SelRightIndent and 
SelHangingIndent properties to set your indents, instead of the 
EM_SETPARAFORMAT code.

> And so it begs the question, how do I set the margins
> without making it "permanent" [2]?

As you will now be aware, the indents mentioned above apply to the rtf 
document itself (either to all its paragraphs or to just some of its 
paragraphs, whichever you desire) and as such they will be part of the rtf 
document when it is saved from your RTB. So, if you set those indents and 
the user edits and saves the document and then later loads it back (into 
either an RTB or Micros$oft Word or whatever) then the indents will still be 
there in the document. Presumably, that is what you mean by "permanent".

The above of course is often exactly what you want to do, but if you do not 
want to do that and if you want to set up "margins" purely so as to adjust 
the way your RTB displays the document (without them becoming part of the 
document itself) then you need to do it a different way. The RTB has a 
RightMargin  property which is for that precise purpose. When you set the 
RTB.RightMargin then the RTB will display your document with the specified 
margin as the user sees it in the RTB, but the margin will not actually 
become part of the document. The problem is that the RTB does not have an 
equiavlent LeftMargin property, so you need to find a different way of 
causing the RTB to display a left margin that is not actually part of the 
document. There are a number of ways of doing that:

One way of course would be to set the left and right indents for the entire 
document (as the code I posted yesterday does) and then allow your user to 
edit the document in the RTB and then when you come to finally save the 
edited document you can simply use the same code, but this time to set the 
left and right indents back to zero again before you save it.

Another way is to use a PictureBox with a white background as the container 
for the RTB. If you set the RTB's Appearance property to rtfFlat and its 
BorderStyle to rtfNoBorder and if you adjust the RTB's size so that it is 
the same height as the client area of the pictureBox but not quite as wide 
then you can position it within the pictureBox to give the appearance of 
whatever left and right margins you desire. There are some problems 
associated with that method because of the fact that a click in the "border 
area" will actually be a click in the PictureBox, but with suitable code you 
can take care of those problems.

One other way would be to send the RTB an EM_SETMARGINS message. That 
specific message was really only useful in standard TextBoxes and it failed 
to work properly in RichtextBoxes, but I've noticed in recent years that it 
seems to work okay on RTBs. I definitely remember it not working for many 
years. Perhaps recent incarnations of the RichTextBox have overcome that 
problem? Anyway, it certainly works for me on my current system. I've tried 
a number of different ways to "break it" and it still seems to work. For 
what it's worth you can give it a try. Perhaps others here will confirm 
whether it works or not on their own systems, and whether they can "break 
it" by doing certain things? It seems odd that it works now whereas it did 
not work properly before without any kind of official comment appearing 
anywhere.

So, you can use any one of the above methods, and perhaps others here will 
have other methods that can be used. For what it's worth, here is some code 
for the EM_SETMARGINS method:

Mike

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias _
  "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
  ByVal wParam As Long, lParam As Any) As Long
Private Const EM_SETMARGINS As Long = &HD3
Private Const EC_LEFTMARGIN As Long = &H1
Private Const EC_RIGHTMARGIN As Long = &H2

Private Sub Command1_Click()
Dim leftmargin As Long, rightmargin As Long, margins As Long
leftmargin = 50 ' pixels from left of RTB to left of text
rightmargin = 50 ' pixels from tight of RTB to right of text
margins = leftmargin + rightmargin * &H10000
SendMessage RichTextBox1.hwnd, EM_SETMARGINS, _
  EC_LEFTMARGIN Or EC_RIGHTMARGIN, _
  ByVal margins
End Sub




0
Reply Mike 4/6/2010 2:10:28 PM

Don't know why there is so much discussion about wordwrapping, it's clearly 
written out in the VB6 help file.

Use RightMargin property to control wordwrapping. This value indicates the 
difference in twips between the right border of the displayed wordwrapped 
text and the right border of RichTextBox control, so a value of zero will 
always break at the control's right border, and a value of, f.e., 999999 
will set the breakpoint far enough outside the right border so that no line 
should be broken and only CRs will do.

For precise calculations where wordwrapping is done you might have to take 
into account the control's border width (eventually as well as the width of 
a vertical scroll bar) . A simple form with a RTB having some text loaded 
will easily show it.

In my projects I only use values of zero and 999999, but if I do remember 
right then I once had to use a negative value to move the wordwrap to the 
left into the visible area. 

0
Reply Horst 4/7/2010 3:22:44 PM

"Horst Heinrich Dittgens" <hhd71@sofort-mail.de> wrote in message 
news:hpi83f$gg9$02$1@news.t-online.com...

> Don't know why there is so much discussion
> about wordwrapping . . .

Perhaps it is because many people, such as yourself for example, do not 
undertsand it.

> Use RightMargin property to control wordwrapping. This
> value indicates the difference in twips . .

Wrong.

> . . between the right border of the displayed wordwrapped text and the 
> right border of RichTextBox control

Wrong again!

> For precise calculations where wordwrapping is done you
> might have to take into account the control's border width

Well, for precise wrapping the very first thing you need to take into 
account is the basics of how it works, and you clearly do not understand 
that yet.

In any case, the subject was not just about word wrapping, which is perhaps 
just as well because if you had answered it yourself then you would have 
given the OP all the wrong information. It was about a problem the OP was 
having when his rtf was failing to wrap as he expected it to even after he 
had set the Rightmargin to the appropriate value, and it was in fact more to 
do with certain paragraph indents in his rtf that needed correcting.

Mike



0
Reply Mike 4/7/2010 6:11:20 PM

This will help with future work.
And I will incorporate your sage advice.
I my initial case, the text is only loaded once and therefore the relative 
vs absolute issue did not show up.  But I will fix that and now know better.

Thanks again Mike.


"Mike Williams" wrote:

> "Bee" <Bee@discussions.microsoft.com> wrote in message 
> news:1F5258D6-152F-4576-9D94-DC896C76E04E@microsoft.com...
> 
> > And so it begs the question, how do I set the margins
> > without making it "permanent" [1]?
> 
> Before I answer that question let me just say something about the code I 
> posted yesterday. If you run the code and click the button then it will 
> fully justify the contents of the RTB and it will set up the desired left 
> and right paragraph indents for the entire document. However, with the code 
> as it currently stands, if you click the button twice you will find that it 
> increases the left indent (instead of simply setting it to the same value) 
> and each time you click the button the left indent will get bigger. That is 
> because I had inadvertently used the wrong constant. I had used 
> PFM_OFFSETINDENT, which is a relative adjustment to the left indent, whereas 
> I should have used PFM_STARTINDENT, which is an absolute left indent. So, in 
> order to fix that problem add the following to your declarations:
> 
>     Private Const PFM_STARTINDENT As Long = &H1
> 
> .. . and change the mask line in the example code to:
> 
>     PF2.dwMask = PFM_ALIGNMENT Or PFM_OFFSET _
>           Or PFM_STARTINDENT Or PFM_RIGHTINDENT
> 
> In fact I used the EM_SETPARAFORMAT method so that I could use 
> ADVANCEDTYPOGRAPHY in order to set full justification, something which 
> cannot be done using native RTB properties or methods and I included the 
> indent settings in that method merely because I was using it anyway to set 
> full justification. If you do not want to set full justification and you do 
> not want to do anything else that cannot be done with the native VB methods 
> then you can simply use the RTB SelIndent and SelRightIndent and 
> SelHangingIndent properties to set your indents, instead of the 
> EM_SETPARAFORMAT code.
> 
> > And so it begs the question, how do I set the margins
> > without making it "permanent" [2]?
> 
> As you will now be aware, the indents mentioned above apply to the rtf 
> document itself (either to all its paragraphs or to just some of its 
> paragraphs, whichever you desire) and as such they will be part of the rtf 
> document when it is saved from your RTB. So, if you set those indents and 
> the user edits and saves the document and then later loads it back (into 
> either an RTB or Micros$oft Word or whatever) then the indents will still be 
> there in the document. Presumably, that is what you mean by "permanent".
> 
> The above of course is often exactly what you want to do, but if you do not 
> want to do that and if you want to set up "margins" purely so as to adjust 
> the way your RTB displays the document (without them becoming part of the 
> document itself) then you need to do it a different way. The RTB has a 
> RightMargin  property which is for that precise purpose. When you set the 
> RTB.RightMargin then the RTB will display your document with the specified 
> margin as the user sees it in the RTB, but the margin will not actually 
> become part of the document. The problem is that the RTB does not have an 
> equiavlent LeftMargin property, so you need to find a different way of 
> causing the RTB to display a left margin that is not actually part of the 
> document. There are a number of ways of doing that:
> 
> One way of course would be to set the left and right indents for the entire 
> document (as the code I posted yesterday does) and then allow your user to 
> edit the document in the RTB and then when you come to finally save the 
> edited document you can simply use the same code, but this time to set the 
> left and right indents back to zero again before you save it.
> 
> Another way is to use a PictureBox with a white background as the container 
> for the RTB. If you set the RTB's Appearance property to rtfFlat and its 
> BorderStyle to rtfNoBorder and if you adjust the RTB's size so that it is 
> the same height as the client area of the pictureBox but not quite as wide 
> then you can position it within the pictureBox to give the appearance of 
> whatever left and right margins you desire. There are some problems 
> associated with that method because of the fact that a click in the "border 
> area" will actually be a click in the PictureBox, but with suitable code you 
> can take care of those problems.
> 
> One other way would be to send the RTB an EM_SETMARGINS message. That 
> specific message was really only useful in standard TextBoxes and it failed 
> to work properly in RichtextBoxes, but I've noticed in recent years that it 
> seems to work okay on RTBs. I definitely remember it not working for many 
> years. Perhaps recent incarnations of the RichTextBox have overcome that 
> problem? Anyway, it certainly works for me on my current system. I've tried 
> a number of different ways to "break it" and it still seems to work. For 
> what it's worth you can give it a try. Perhaps others here will confirm 
> whether it works or not on their own systems, and whether they can "break 
> it" by doing certain things? It seems odd that it works now whereas it did 
> not work properly before without any kind of official comment appearing 
> anywhere.
> 
> So, you can use any one of the above methods, and perhaps others here will 
> have other methods that can be used. For what it's worth, here is some code 
> for the EM_SETMARGINS method:
> 
> Mike
> 
> Option Explicit
> Private Declare Function SendMessage Lib "user32" Alias _
>   "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
>   ByVal wParam As Long, lParam As Any) As Long
> Private Const EM_SETMARGINS As Long = &HD3
> Private Const EC_LEFTMARGIN As Long = &H1
> Private Const EC_RIGHTMARGIN As Long = &H2
> 
> Private Sub Command1_Click()
> Dim leftmargin As Long, rightmargin As Long, margins As Long
> leftmargin = 50 ' pixels from left of RTB to left of text
> rightmargin = 50 ' pixels from tight of RTB to right of text
> margins = leftmargin + rightmargin * &H10000
> SendMessage RichTextBox1.hwnd, EM_SETMARGINS, _
>   EC_LEFTMARGIN Or EC_RIGHTMARGIN, _
>   ByVal margins
> End Sub
> 
> 
> 
> 
> .
> 
0
Reply Utf 4/7/2010 10:44:01 PM

19 Replies
796 Views

(page loaded in 0.409 seconds)

Similiar Articles:































7/23/2012 1:14:04 AM


Reply: