[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Replace text on an RTF file with text within a textbox (winform) vb 2008 EE

Hi Experts,

I've created a winform in VB 2008 Express Ed. The form has 4 text boxes... NAME / ADDRESS / TEL / Dob

These text boxes are spread out over 4 Tab-pages, each tab page has a NEXT (Button) to jump to the next tab... the 5th Tab page has an RTF box (docked)

On the 4th Tab,,, if I click on END button...  it jumps to tab 5 & also loads a pre-saved RTF file into the RTF box from c:\folder\ (see Snippet ID=473725) for reference.  The RTF file was created in Ms Word 2003, which has a table on it with rows/columns... please see 'original RFT' (File ID=217813) attachment image for guidance.

NOW: this is where the problem lies...

I want to replace letters (e.g. NNNNN) which are embedded in the saved RTF file, with the text from Textbox1.text on the 1st tab, and repeated for each textbox on each Tab.

If I just wish to load the saved RTF file into the RTF box... then it looks like (attachmemt: frm-001.JPG (34 KB)File ID=217819)... which is not my total objective...

If Button 4 "END"... if clicked... with the following code adding to Button 4:

  ' Detect & Replace
        If RichTextBox1.Text.IndexOf("nnnnn") > 0 Or RichTextBox1.Text.IndexOf("NNNNN") >= 0 Then
                RichTextBox1.Text = RichTextBox1.Text.Replace("NNNNN", TextBox1.Text)
        End If

it alters the RTF box (see attachment: frm-002.JPG (30 KB)File ID=217820)... I wish to keep the format of the RTF and replace NNNNN with the textbox1.text, but keep the row/columns.

I hope this hasn't totally confused you by now.

Please help :)

Roberto
Private Sub Btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn4.Click

         (Me.TabControl1.SelectedTab = Me.TabPage5)

        Dim ESOLIAF As String = "form.rtf"
        If Len(Dir("c:\folder\form.rtf")) > 0 Then
            RichTextBox1.LoadFile("c:\esol\form.rtf", RichTextBoxStreamType.RichText)
            RichTextBox1.RightMargin = 0
        Else
            RichTextBox1.Text = "File not found"
        End If

    End Sub

Open in new window

form.JPG
frm-001.JPG
frm-002.JPG
0
RobertoFreemano
Asked:
RobertoFreemano
  • 4
  • 4
1 Solution
 
käµfm³d 👽Commented:
You might try doing a replace on the actual RTF encoding rather than just the text. For this, switch

    RichTextBox1.Text

with

    RichTextBox1.Rtf

As a side note, for your "Detect and replace," you are checking for either "nnnnn" or "NNNNN", but you are only replacing "NNNNN" if either is found. In this case, if you find a "nnnnn", and no "NNNNN" exists, no replacement will occur.  :)
0
 
käµfm³d 👽Commented:
Come to think of it

       If RichTextBox1.Text.IndexOf("nnnnn") > 0 Or RichTextBox1.Text.IndexOf("NNNNN") >= 0 Then
                RichTextBox1.Text = RichTextBox1.Text.Replace("NNNNN", TextBox1.Text)
        End If

is redundant. A replace will do a replace if the search string exists in the string to be searched, so there really is no need for the If condition.
0
 
RobertoFreemanoAuthor Commented:
Hi kaufmed,

I replaced the code as suggested: RichTextBox1.Text = RichTextBox1.Rtf.Replace("NNNNN", TextBox1.Text

and the following happens - see new attachment.

Did I do something wrong? :(
frm-003.JPG
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RobertoFreemanoAuthor Commented:
Oops,

I see what I did...
RichTextBox1.Rtf = RichTextBox1.Rtf.Replace("NNNNN", TextBox1.Text

Now, it has kept the formatting :)
0
 
RobertoFreemanoAuthor Commented:
kaufmed,

one last thing... any idea why the embedded Rtf doesn't show the the full table? it appears to be cut off on the right hand side?

I tried to adjust the  RichTextBox1.RightMargin = 1, but this cause more problems.

Cheers,
Roberto
0
 
käµfm³d 👽Commented:
I don't think it is missing, per se, but it may hard-coded to a certain width in the RTF. You'll notice in your ss of the Word window, that the box takes over 3/4 the width of a printed page. I assume that your application window is not this wide.

I am actually a little surprised that you do not have a scrollbar at the bottom of the window. Perhaps enabling the scrollbars for the RichTextBox through the properties window will confirm the above.
0
 
RobertoFreemanoAuthor Commented:
Thank's a million kaufmed
0
 
käµfm³d 👽Commented:
NP. Glad to help :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now