Sunday, October 14, 2007

File Handling with StreamReader and StreamWriter using Dialogs!


This is the code:
----------------
'
'
'Sequence 1 Task 11 open file special isntruction
Private Sub Buttons1t11importsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttons1t11importsi.Click
Try
OpenFileDialogs1t11.Filter = "Rich Text Files(*.rtf)|*.rtf|MS Documents(*.doc)|*.doc|" & _
"All files(*.*)|*.*"

If OpenFileDialogs1t11.ShowDialog = Windows.Forms.DialogResult.OK Then
OpenFileDialogs1t11.ShowHelp = True

Dim fs As New FileStream(OpenFileDialogs1t11.FileName, FileMode.Open)

Dim sr As New StreamReader(fs)
RichTextBoxs1t11si.Rtf = sr.ReadToEnd()
sr.Close()
fs.Close()

End If

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub


'
'
'Sequence 1 Task 11 save file special instruction
Private Sub Buttons1t11editsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttons1t11editsi.Click
Try
SaveFileDialogs1t11.Filter = "Rich Text Files(*.rtf)|*.rtf|MS Documents(*.doc)|*.doc|" & _ "All files(*.*)|*.*"

If SaveFileDialogs1t11.ShowDialog = Windows.Forms.DialogResult.OK Then
SaveFileDialogs1t11.ShowHelp = True

Dim fs As New FileStream(SaveFileDialogs1t11.FileName, FileMode.OpenOrCreate)

Dim sw As New StreamWriter(fs)
sw.Write(RichTextBoxs1t11si.Rtf)
sw.Close()
fs.Close()

End If
Catch ex As Exception
MsgBox(ex.ToString)

End Try
End Sub


'
'
'Sequence 1 Task 11 format
Private Sub Buttons1t11format_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttons1t11format.Click
Try
If FontDialogs1t1.ShowDialog = Windows.Forms.DialogResult.OK Then
FontDialogs1t1.ShowHelp = True

'for work instruction
RichTextBoxs1t11wi.SelectionFont = FontDialogs1t1.Font
RichTextBoxs1t11wi.SelectionColor = FontDialogs1t1.Color

'for special instruction
RichTextBoxs1t11si.SelectionFont = FontDialogs1t1.Font
RichTextBoxs1t11si.SelectionColor = FontDialogs1t1.Color

End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub