Feb 17th, 2010, 04:29 PM
#1
Thread Starter
Lively Member
[RESOLVED] Saving extra information in a rtf file
Ok, so basicly i have a VB.net word processor application, With a;
Header Label(as system.windows.forms.label)
Footer Label(as system.windows.forms.label)
The printing is no problem, but the problem im having is saving the header and footer label's text into the RTF file and also loading these into the header and footer labels.
Ive been trying to do this for hours, if you want me to make a demo project of what im trying to do, ill make one and send it to you.
Thanks in advance.
Any solutions to my problem are accepted
Tom1859
Feb 17th, 2010, 04:51 PM
#2
Re: Saving extra information in a rtf file
Can you save IO.File.AppendAllText() the header to the RTF prior to the contents of the RTB, then do save IO.File.AppendAllText() the footer.
That will yield in the file: Header, Text, Footer.
Last edited by Bruce Fox; Feb 17th, 2010 at 04:55 PM .
Feb 17th, 2010, 05:00 PM
#3
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Thanks, ill give it a shot. BTW will this keep all formmating of the header and footer? e.g, colour, font size?
Thanks
Tom1859
Feb 17th, 2010, 05:01 PM
#4
Re: Saving extra information in a rtf file
Hmmm, that doesn't work...
Feb 17th, 2010, 05:03 PM
#5
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Do you have any ideas of how i can get that information again when the document has be closed an re-opened
Thanks in advanced
Tom1859
Feb 17th, 2010, 05:17 PM
#6
Re: Saving extra information in a rtf file
Working on the formatting issue
Feb 17th, 2010, 07:05 PM
#7
Re: Saving extra information in a rtf file
How about using two (header & footer) RTBs that appear as Labels?
Feb 18th, 2010, 06:48 AM
#8
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Thankyou, i will try that now, im currently using Labels and textboxes. Any idea how to get this information back out of the RTF when it loads?
Thanks
Tom1859
Feb 18th, 2010, 03:37 PM
#9
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Feb 18th, 2010, 04:11 PM
#10
Re: Saving extra information in a rtf file
Ok. I used a fourth RTB as a buffer to append the Header, Main, and Footer formatted text. This fourth RTB is hidden from the user (if required) and that is used to write out the formatted text into a TextFile!. The TextFile has the formatting, then that can be rebadged as a RTF that will display correcly.
The RTB has an AppendText method, however that contains NO formatting. It would be nice if it had a AppendRTF method
Credit to circuits2 for working out the Copy Paste action to emulate AppendRTF.
This was tested successfully:
vb Code:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
' Add the Header RTB (emulating a Label) to the Hidden RTB maintaining formatting
Me.rtbHeader.SelectAll()
Me.rtbHeader.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Me.rtbHidden.AppendText(Environment.NewLine)
' Add the Main RTB to the Hidden RTB maintaining formatting
Me.rtbMain.SelectAll()
Me.rtbMain.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Me.rtbHidden.AppendText(Environment.NewLine)
' Add the Footer RTB (emulating a Label) to the Hidden RTB maintaining formatting
Me.rtbFooter.SelectAll()
Me.rtbFooter.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
' Remove previous instance of file (if pressent)
If IO.File.Exists("C:\myfile.rtf") Then IO.File.Delete("C:\myfile.rtf")
' Save the out as text (which HAS formatting)
Me.rtbHidden.SaveFile("C:\myfile.txt")
' Rename file txt extension to rtf
IO.File.Copy("C:\myfile.txt", "C:\myfile.rtf")
IO.File.Delete("C:\myfile.txt")
End Sub
Last edited by Bruce Fox; Feb 18th, 2010 at 04:17 PM .
Feb 18th, 2010, 07:53 PM
#11
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Thankyou
Feb 18th, 2010, 07:54 PM
#12
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Wouldnt
vb Code:
Me.rtbHidden.SaveFile("C:\myfile.rtf")
work? instead of saving, then changing the extension?
or
VB Code:
Dim sfd as new system.windows.forms.savefiledialog
sfd.text = "Please select where you want to save your file to"
sfd.filter = Rich Text Files|*.rtf
dim result = sfd.showdialog
if result = dialogresult.OK then
rtbhidden.savefile(sfd.filename)
end if
sfd.dispose
^^ ive just written that entirley from memory, so im sorry if there is a mistake
Thanks
Tom1859
Last edited by tom1859; Feb 18th, 2010 at 08:03 PM .
Feb 18th, 2010, 08:07 PM
#13
Re: Saving extra information in a rtf file
Originally Posted by
tom1859
Wouldnt
vb Code:
Me.rtbHidden.SaveFile("C:\myfile.rtf")
work? instead of saving, then changing the extension?
No, it doesn't If you do that you loose the formatting, whereas as .txt you get the formatting aswell as the text.
The issue is with Appending the formatted text. As rtf has open and close braces, the first text is the only thing seen.
As far as the second question goes, you can use the SFD to select the location, just change the rtf to txt (intermittently) in you code prior to the actual Save.
Feb 18th, 2010, 08:09 PM
#14
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Thanks for the quick reply.
i have my own file format for my application(.pwd)(still set a rich text)
will this still work
Thanks
Tom1859
Feb 18th, 2010, 08:16 PM
#15
Re: Saving extra information in a rtf file
All that matters is the association you have for that given file extension (ie Open With).
Other users, unless configured, would not have Word or WordPad (apps that can display RT) open a pwd file natively.
Feb 18th, 2010, 08:18 PM
#16
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
ok. do you have any ideas of how to get the formmated header and footers out the file when it loads?
is there a way to search for a string in the rtf e.g //beginheader//(headertext)//endheader//
and get the (headertext) in to the header richtextbox?
Thanks
Feb 18th, 2010, 08:23 PM
#17
Re: Saving extra information in a rtf file
Not with standing the question in post #16, I just wanted to demonstrate:
vb Code:
Dim sfd as new system.windows.forms.savefiledialog
sfd.text = "Please select where you want to save your file to"
sfd.filter = Rich Text Files|*.rtf
dim result = sfd.showdialog
if result = dialogresult.OK then
rtbhidden.savefile(sfd.filename) ' <<<< do the txt to rtf and the save here (as filename will contain rtf, use replace for example)
end if
sfd.dispose
Feb 18th, 2010, 08:28 PM
#18
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Ok thanks
i was thinking i could prehaps use my find and replace code to find the //beginheader//
VB Code:
#Region "Find"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim StartPosition As Integer
Dim SearchType As CompareMethod
If chkMatchCase.Checked = True Then
SearchType = CompareMethod.Binary
Else
SearchType = CompareMethod.Text
End If
StartPosition = InStr(1, frmMain.rtbdoc.Text, txtsearchtermf.Text, SearchType)
If StartPosition = 0 Then
MessageBox.Show("String: '" & txtsearchtermf.Text.ToString() & "' not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Exit Sub
End If
frmMain.rtbdoc.Select(StartPosition - 1, txtsearchtermf.Text.Length)
frmMain.rtbdoc.ScrollToCaret()
frmMain.Focus()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim StartPosition As Integer = frmMain.rtbdoc.SelectionStart + 2
Dim SearchType As CompareMethod
If chkMatchCase.Checked = True Then
SearchType = CompareMethod.Binary
Else
SearchType = CompareMethod.Text
End If
StartPosition = InStr(StartPosition, frmMain.rtbdoc.Text, txtsearchtermf.Text, SearchType)
If StartPosition = 0 Then
MessageBox.Show("String: '" & txtsearchtermf.Text.ToString() & "' not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Exit Sub
End If
frmMain.rtbdoc.Select(StartPosition - 1, txtsearchtermf.Text.Length)
frmMain.rtbdoc.ScrollToCaret()
frmMain.Focus()
End Sub
#End Region
#Region "Find And Replace"
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim StartPosition As Integer
Dim SearchType As CompareMethod
If chkmatchcasefar.Checked = True Then
SearchType = CompareMethod.Binary
Else
SearchType = CompareMethod.Text
End If
StartPosition = InStr(1, frmMain.rtbdoc.Text, txtsearchtermfar.Text, SearchType)
If StartPosition = 0 Then
MessageBox.Show("String: '" & txtsearchtermfar.Text.ToString() & "' not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Exit Sub
End If
frmMain.rtbdoc.Select(StartPosition - 1, txtsearchtermfar.Text.Length)
frmMain.rtbdoc.ScrollToCaret()
frmMain.Focus()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim StartPosition As Integer = frmMain.rtbdoc.SelectionStart + 2
Dim SearchType As CompareMethod
If chkmatchcasefar.Checked = True Then
SearchType = CompareMethod.Binary
Else
SearchType = CompareMethod.Text
End If
StartPosition = InStr(StartPosition, frmMain.rtbdoc.Text, txtsearchtermfar.Text, SearchType)
If StartPosition = 0 Then
MessageBox.Show("String: '" & txtsearchtermfar.Text.ToString() & "' not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Exit Sub
End If
frmMain.rtbdoc.Select(StartPosition - 1, txtsearchtermfar.Text.Length)
frmMain.rtbdoc.ScrollToCaret()
frmMain.Focus()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If frmMain.rtbdoc.SelectedText.Length <> 0 Then
frmMain.rtbdoc.SelectedText = txtreplacementtextfar.Text
End If
Dim StartPosition As Integer = frmMain.rtbdoc.SelectionStart + 2
Dim SearchType As CompareMethod
If chkmatchcasefar.Checked = True Then
SearchType = CompareMethod.Binary
Else
SearchType = CompareMethod.Text
End If
StartPosition = InStr(StartPosition, frmMain.rtbdoc.Text, txtsearchtermfar.Text, SearchType)
If StartPosition = 0 Then
MessageBox.Show("String: '" & txtsearchtermfar.Text.ToString() & "' not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Exit Sub
End If
frmMain.rtbdoc.Select(StartPosition - 1, txtsearchtermfar.Text.Length)
frmMain.rtbdoc.ScrollToCaret()
frmMain.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim currentPosition As Integer = frmMain.rtbdoc.SelectionStart
Dim currentSelect As Integer = frmMain.rtbdoc.SelectionLength
frmMain.rtbdoc.Rtf = Replace(frmMain.rtbdoc.Rtf, Trim(txtsearchtermfar.Text), Trim(txtreplacementtextfar.Text))
frmMain.rtbdoc.SelectionStart = currentPosition
frmMain.rtbdoc.SelectionLength = currentSelect
frmMain.Focus()
End Sub
#End Region
Thats the code for the find and replace form
Feb 18th, 2010, 08:37 PM
#19
Re: Saving extra information in a rtf file
Originally Posted by
Bruce Fox
No, it doesn't
If you do that you loose the formatting, whereas as .txt you get the formatting aswell as the text.
I stand corrected, I have used the following block in place od my rtf to txt delete, rename etc..
vb Code:
Dim sfd As New SaveFileDialog()
sfd.Title = "Please select where you want to save your file to"
sfd.Filter = "Rich Text Files (*.rtf)|*.rtf"
sfd.FilterIndex = 2
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
rtbHidden.SaveFile(sfd.FileName)
End If
sfd.Dispose()
Feb 18th, 2010, 08:38 PM
#20
Re: Saving extra information in a rtf file
Here is the full code:
VB Code:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
' Add the Header RTB (emulating a Label) to the Hidden RTB maintaining formatting
Me.rtbHeader.SelectAll()
Me.rtbHeader.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Me.rtbHidden.AppendText(Environment.NewLine)
' Add the Main RTB to the Hidden RTB maintaining formatting
Me.rtbMain.SelectAll()
Me.rtbMain.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Me.rtbHidden.AppendText(Environment.NewLine)
' Add the Footer RTB (emulating a Label) to the Hidden RTB maintaining formatting
Me.rtbFooter.SelectAll()
Me.rtbFooter.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Dim sfd As New SaveFileDialog()
sfd.Title = "Please select where you want to save your file to"
sfd.Filter = "Rich Text Files (*.rtf)|*.rtf"
sfd.FilterIndex = 1
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
rtbHidden.SaveFile(sfd.FileName)
End If
sfd.Dispose()
End Sub
Feb 18th, 2010, 08:47 PM
#21
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Thankyou
Microsoft really need to update there rich text control because, to be honest it is crap. they should update it to include drawing images and textboxes, headers and footers, etc
anyway enough with my moan i think i might of come up with a way to get the header text when the doc is opened. only thing is i dont know how to code it
so basicly on a document load event, go to the first line in the document, copy the rtf (richtextbox.rtf.copy) then delete that line (because we dont need it while the document is beign edited because it will be in the header rtb above.
then go to the last line of the richtextbox, copy the rtf, paste into the footer rtf the delete the last line in the document.
Thats my theory. is there a way to code this?
Thanks
Tom1859
Feb 18th, 2010, 08:55 PM
#22
Re: Saving extra information in a rtf file
Yep, but I'm off home. I will get on in a few hours.
Give it a go, you have the concept down
Feb 18th, 2010, 08:57 PM
#23
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Ok thanks, well im going to bed in a minuite because its 2:00 in the moring here. Speak to you tommorow and thanks for all your effots
+Rep
Tom1859
Feb 19th, 2010, 03:38 AM
#24
Re: Saving extra information in a rtf file
The following will Save the contents of the Three RTB's. The Header, Main and Footer.
Also, it can Load the pre-saved contents from an RTF (or .pwd) into the Three above mentioned RTB's:
VB Code:
Option Explicit On
Option Strict On
Public Class Form1
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
' Add the Header RTB (emulating a Label) to the Hidden RTB maintaining formatting
Me.rtbHeader.SelectAll()
Me.rtbHeader.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Me.rtbHidden.AppendText(Environment.NewLine)
' Add the Main RTB to the Hidden RTB maintaining formatting
Me.rtbMain.SelectAll()
Me.rtbMain.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Me.rtbHidden.AppendText(Environment.NewLine)
' Add the Footer RTB (emulating a Label) to the Hidden RTB maintaining formatting
Me.rtbFooter.SelectAll()
Me.rtbFooter.Copy()
Me.rtbHidden.DeselectAll()
Me.rtbHidden.Paste()
Dim sfd As New SaveFileDialog()
sfd.Title = "Please select where you want to save your file to"
sfd.Filter = "Rich Text Files (*.rtf)|*.rtf"
sfd.FilterIndex = 1
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
rtbHidden.SaveFile(sfd.FileName)
End If
sfd.Dispose()
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
' Load the Hidden RTB for processing
Dim ofd As New OpenFileDialog()
ofd.Title = "Please select the file to open"
ofd.Filter = "Rich Text Files (*.rtf)|*.rtf"
ofd.FilterIndex = 1
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.rtbHidden.Clear()
Me.rtbHidden.LoadFile(ofd.FileName)
End If
ofd.Dispose()
' Extract the Header
Me.rtbHidden.Select(0, Me.rtbHidden.Text.IndexOf(vbLf))
Me.rtbHidden.Copy()
Me.rtbHeader.Clear()
Me.rtbHeader.Paste()
' Extract the Main
Me.rtbHidden.Select(Me.rtbHidden.Text.IndexOf(vbLf) + 1, Me.rtbHidden.Text.LastIndexOf(vbLf) - Me.rtbHidden.Text.IndexOf(vbLf))
Me.rtbHidden.Copy()
Me.rtbMain.Clear()
Me.rtbMain.Paste()
' Extract the Footer
Me.rtbHidden.Select(Me.rtbHidden.Text.LastIndexOf(vbLf) + 1, Me.rtbHidden.Text.Length - Me.rtbHidden.Text.IndexOf(vbLf) + 1)
Me.rtbHidden.Copy()
Me.rtbFooter.Clear()
Me.rtbFooter.Paste()
End Sub
End Class
Here is an image:
Attached Images
Last edited by Bruce Fox; Feb 19th, 2010 at 03:44 AM .
Feb 19th, 2010, 07:51 AM
#25
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Thank you very much.
Want a link to your website(if you have one) or something like that in the about section?
Feb 19th, 2010, 09:11 AM
#26
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Ok, one problem, when it saves it doesnt overite, does the hidden rtb need .Clear() after every save?
Thanks
Feb 19th, 2010, 04:49 PM
#27
Re: Saving extra information in a rtf file
Maybe; if you think it needs one - Try it and see if it fixes your problem.
What do you think adding a Clear() at that stage will do?
Feb 19th, 2010, 04:53 PM
#28
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
well say if you save the file. then 10 mins later save it again, it will cause duplicated text, off and i have something to show you, its nerly done
Feb 19th, 2010, 05:24 PM
#29
Re: Saving extra information in a rtf file
Well it need to be Cleared then .
Great job.
Feb 19th, 2010, 06:15 PM
#30
Thread Starter
Lively Member
Re: Saving extra information in a rtf file
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width