RTF file to Rich Text Box

It seems to be simple to associate an RTF file to a rich text box.  But can anyone provide a solution to this one.

I want to have an rtf file that is separated in to hundreds of blocks.  The blocks should be numbered.

In a VB application I would like to be able to bring through a block of text from this file into a rich text box depending on which number the user has chosen.  i.e.  The user clicks something from a list with the index number 1032 and the piece of text in the rtf file numbered 1032 will be displayed in the box.

Does anyone have any idea how I might implement the blocking and displaying of the text?

Thanks
LVL 1
batdanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
There are a couple of ways you can do this...

Way #1
You could create an index file that has the offset and length of each block in the RTF file... Then open the file in binary mode and read the block in...


Way #2
You could write tags into the file so it looks like this:

     {TAG0001}RichTextData{TAG0002}RichTextData...

Then read the file looking for the correct tag...


Way #3
You could use a database to store your richtext blocks in...



Hope this helps!


Cheers!
0
batdanAuthor Commented:
mcrider:

Nice to be given a choice..........

Method 2 would be preferable.  In fact method 2 would be ideal!

Could you please however supply example code of how I would open the file & read a block into the rtb?  There are more points available if this is a big task.

Thanks

#Dan
0
mcriderCommented:
Here you go... First, I'll give you an example of the RTF file with the tags.  Just copy this to NOTEPAD and save it as C:\WINDOWS\DESKTOP\TESTME.RTF:


{TAG0001}
{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}
{\f1\froman\fcharset2 Symbol;}{\f2\fswiss MS Sans Serif;}}
{\colortbl\red0\green0\blue0;\red0\green0\blue255;}
\deflang1033\pard\plain\f2\fs17 This is \plain\f2\fs17\cf1\b Block #1\plain\f2\fs17
\par This is only a test 111111111111
\par TESTING 1 2 3
\par
\par }
{TAG0002}
{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}
{\f1\froman\fcharset2 Symbol;}{\f2\fswiss MS Sans Serif;}}
{\colortbl\red0\green0\blue0;\red0\green0\blue255;}
\deflang1033\pard\plain\f2\fs17 This is \plain\f2\fs17\cf1\b Block #2\plain\f2\fs17
\par This is only a test 222222222222
\par TESTING 4 5 6
\par
\par }
{TAG0003}
{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}
{\f1\froman\fcharset2 Symbol;}{\f2\fswiss MS Sans Serif;}}
{\colortbl\red0\green0\blue0;\red0\green0\blue255;}
\deflang1033\pard\plain\f2\fs17 This is \plain\f2\fs17\cf1\b Block #3\plain\f2\fs17
\par This is only a test 333333333333
\par TESTING 7 8 9
\par
\par }




Create a new project and add a RICHTEXTBOX, a regular TEXTBOX, and a COMMANDBUTTON to the form.

Paste the following into the DECLARATIONS SECTION of the form:

    Function GetRTFBlock(FileName As String, BlockNumber As Long) As String
        Dim fNum As Long
        Dim RTFBlock As String
        Dim lBuf As String
        Dim lBlockName As String
       
        lBlockName = "{TAG" + Format(BlockNumber, "0000") + "}"
        fNum = FreeFile
        Open FileName For Input Access Read As #fNum
        Do While Not EOF(fNum)
            Line Input #fNum, lBuf
            If lBuf = lBlockName Then
                Do While Not EOF(fNum)
                    Line Input #fNum, lBuf
                    If Left$(lBuf, 4) = "{TAG" Then Exit Do
                    RTFBlock = RTFBlock + lBuf + vbCrLf
                Loop
                GetRTFBlock = RTFBlock
                Exit Do
            End If
        Loop
        Close fNum
    End Function

    Private Sub Command1_Click()
        RichTextBox1.TextRTF = ""
        RichTextBox1.TextRTF = GetRTFBlock("C:\Windows\Desktop\test.rtf", CLng(Text1.Text))
    End Sub



Run the program.  Put a 1 in the textbox and click the button...  Block 1 will load into the RICHTEXTBOX.

Put a 2 in the textbox and click the button... Block 2 will load into the RICHTEXTBOX.


You get the idea... If you type in a number for a block that doesn't exist, the RICHTEXTBOX will be cleared.


(hoping for a good grade!  If you're feeling generous, you can create a new question with the title "FOR MCRIDER ONLY" and assign extra points to it... I will answer it.)


Merry Christmas!

Cheers!



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mcriderCommented:
OOPS!

For the example to work, the code for the command button should look like this:

    Private Sub Command1_Click()
        RichTextBox1.TextRTF = "" 
        RichTextBox1.TextRTF = GetRTFBlock("C:\Windows\Desktop\testme.rtf", CLng(Text1.Text))
    End Sub


Cheers!

0
batdanAuthor Commented:
>>(hoping for a good grade!  
--Don't worry on that score.
>>If you're feeling generous, you can create a new question with the title "FOR MCRIDER ONLY" and assign extra points to it...
--ooooooohhhhhhh the bare faced cheek!
>>I will answer it.)
--:-)

OK!  It's Christmas & your answer will not only save me a shed load of time but seems to be decent code as well!  Do you mind if I just slap some more points on this question rather that posting another?......Good.

Seriously though.  The answer is exactly what I was looking for.  Thanks mate.  Catch you next time.

#Dan
0
batdanAuthor Commented:
Did you recieve the 250 or just 150 for this question mcrider?  I tried to up the points but I'm not sure if it worked or not.  Let me know.....

Cheers
0
mcriderCommented:
Only received 150 points :-(

You can tell this because the question is now available to other users at a cost of 15 points... 10% of the original value...

Glad I could help you!

Cheers!
0
batdanAuthor Commented:
I've posted a null question for you.....

Catch you later.
0
mcriderCommented:
Found it... Thanks again!


Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.