Open / Save file dialog

ronanjordan

Member
Joined
Apr 10, 2006
Messages
9
Programming Experience
Beginner
Hi All,

I just writed a wordpad in VB.NET2003 and the problem I have is to keep format as color, indentation,fonts on save and open file. Any help appresiated. Here is tcode:

Thanks all in advance

//Open File===================================

Private Sub mmMenuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmMenuFileOpen.Click



'Try and Catch Error
Try
If Me.ofdOpen.ShowDialog() = DialogResult.OK Then
Dim fs As New FileStream(ofdOpen.FileName, FileMode.Open, FileAccess.Read)
Dim m_streamReader As New StreamReader(fs, System.Text.Encoding.ASCII)
ofdOpen.InitialDirectory = ".\My Documents"
' Read to the file using StreamReader class
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin)


' Read each line of the stream and parse until last line is reached
Me.mainWindow.Text = ""
Dim strLine As String = m_streamReader.ReadLine()
While Not (strLine Is Nothing)
Me.mainWindow.Text += strLine + ControlChars.Lf
strLine = m_streamReader.ReadLine()
End While
' Close the stream
m_streamReader.Close()
End If
Catch em As Exception
System.Console.WriteLine(em.Message.ToString())
End Try

End Sub


\\Save File


Private Sub mmMenuFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmMenuFileSave.Click


'SaveFileDialog

Dim sfdSave As New SaveFileDialog
sfdSave.Filter = "Rich Text Format(*.rtf)|*.rtf|Text File(*.txt)|*.txt|Word Document(*.doc)|*.doc|Text Document -MS-DOS Format(*.txt)|*.txt|Unicode Text Document(*utf)|*.utf|All Files|*.*"
sfdSave.FilterIndex = 2 sfdSave.InitialDirectory = ".\My Documents"

sfdSave.OverwritePrompt = True
If sfdSave.ShowDialog() = DialogResult.OK Then
Dim stream As System.IO.Stream
stream = sfdSave.OpenFile()
Console.WriteLine(sfdSave.FileName)
If Not (stream Is Nothing) Then
Dim sw As New System.IO.StreamWriter(stream)
sw.WriteLine(Me.mainWindow.Rtf)
sw.Close()
stream.Close()
End If
End If

End Sub

====================================================
 
Try saving the file in binary format. or, make sure your not using .txt as the extension. I am at work right now and can't get to my IDE but it sounds like your not using the correct extension. I would go with a binary writer myself. Then just serialize and de-serialize the file date into a variable or property. I will try to get some code posted later if this isn't helpful.
 
I am trying to use the code bellow, but still des not work
==============================



Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate)
' create the reader and writer, based on our file stream
Dim w As BinaryWriter = New BinaryWriter(fs)
Dim r As BinaryReader = New BinaryReader(fs)

'make an appropriate receptacle for the information being read in...
' a StringBuilder is a good choice
Dim output As StringBuilder = New StringBuilder
' set the file pointer to the beginning of our file...
r.BaseStream.Seek(0, SeekOrigin.Begin)
' continue to perform this loop while not at the end of our file...
Do While r.PeekChar() > -1
output.Append(r.ReadString())
' use ReadString to read from the file
Loop


' set the file pointer to the end of our file...
w.BaseStream.Seek(0, SeekOrigin.End)
w.Write("Putting a new set of entries into the binary file..." & Chr(13))
Dim i As Integer
For i = 0 To 5 Step 1 ' some arbitrary information to write to the file
w.Write(i.ToString())
Next i
 
This code works fine for me here. Mess with it a little bit to get the results you want. I even used the file extension .txt and StreamWriter instead of Binary to fit your solution. The thing is I am saving as .rtf not .text and reading it back in the same. Maybe during your read back you are useing .text. Examine the code and let me know what exactly you need. I'll help you make this work.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SaveFile()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamWriter
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].SaveFileDialog1
.AddExtension = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] .ShowDialog() = Windows.Forms.DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]f = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamWriter(.FileName)
f.WriteLine(RichTextBox1.Rtf)
f.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].IsSaved = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OpenFile()[/SIZE]
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamReader
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OpenFileDialog1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] .ShowDialog() = Windows.Forms.DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]f = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamReader(.FileName)
RichTextBox1.Rtf = f.ReadToEnd
f.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
A quick review of the methods and properties of the RichTextBox control reveals the two methods LoadFile and SaveFile. These may be of use to you.
 
Hi,

Still does not display correct layout. I have checket every single line for error but still does not work well. The file is saved as "RTF" and read back as "RTF" but display all other haracters generated by the code.
Code error in ".FileName" saying that is not a member of RichTextBox. The mainWindow (Form) is a RichTextBox. Do you think that has to be a TextBox?

Thanks
 
No, the .filename was representing a Property in the SaveFileDialog. Notice it says with me.savefiledialog. anytime you see .Property or Method look up for the With to see what Class or Module it is representing. Hope this really helped and that you weren't being sarcastic, lol. Your two posts were back to back and is hard to tell. :)
 
Back
Top