Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello, Can I use rich text box 1

Status
Not open for further replies.

ssruprai

Programmer
Apr 24, 2002
16
IN
Hello,
Can I use rich text box to display and print reports. My problem is I don't know how to add text to RTF Box at runtime using different fonts and colors and at a particular line.

For example, at runtime If I do something like this:

rtf.Font.Size = 15
rtf.Text = "Hello"
rtf.Font.Name = "Arial"
rtf.Font.Size = 20
rtf.Text = rtf.Text & " How"

Then both words are displayed using Size 20 and font arial. But I wanted that only second word
should be of size 20 and font "Arial".

I want to retrieve record from a database and then display information contained in a record's fields one by one and line by line but using different fonts and colors.

For example, If want to create a report to display employee records and I want that employee name should be Bold and centred, other fields should use different color, indentation etc. I need a way to go a particular row and particular column and then write text there and change only that text's font and colors etc. and then go to next row, write other information and change text attributes of that line and so on.

I cannot add text to rtf directly. So I need a way where when I add more text, formatting of old text of rtf box doesn't change.

For example if I do rtf.text = rtf.text+"Hello" than I don't want that properties of text (fonts, colors
etc) which is already there in rtf.text does change.

So main problem is going to a particular line/column, add text and then change font etc of added text only and that too at runtime.

How can I achieve it when entering text using code at runtime?
 
this should get you started...
Code:
Option Explicit

Private Sub Form_Load()
  With RichTextBox1
    .Text = ""
    .SelBold = True
    .SelColor = vbBlue
    .SelText = "bold-blue"
    .SelStart = 64& * 1024 ' move to end
    .SelColor = vbRed
    .SelBold = False
    .SelItalic = True
    .SelText = vbCrLf & "italic-red"
  End With
End Sub
 
Excellent Solution. Only thing I could not understand was:

.SelStart = 64& * 1024

If you could please explain what 64& * 1024 does it'll be a great help.What I have to consider(change) when I use some other text? Is it related to size of "bold-blue"?
 
you could get rid of that line
i was not sure if i needed to move the insertion point to the end of existing text before setting different formatting


from MSDN:
Setting SelStart greater than the text length sets the property to the existing text length; changing SelStart changes the selection to an insertion point and sets SelLength to 0.
 
Thank you very much sir.
But if you want then please tell me what 64& * 1024 does. What is 64& and why it is multiplied by 1024.
 
.SelStart = 64& * 1024 ' move to end

here i was just setting selstart to 64K (65,536)
without the &

.SelStart = 64 * 1024 ' move to end

i would get run-time error 6: 'overflow'

because both numbers (64 and 1,024) are within the limits of an 'Integer' (-32,768 to 32,767), VB expects the result to be an 'Integer' also

to avoid this i add the suffix '&' to 64 to tell VB that this number is a 'Long'
VB would then expect the result to be a 'Long' also
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top