'Sams Teach Yourself Visual Basic .NET in 21 Days
'By Lowell Mauer
'Published 2001
'Sams Publishing
'ISBN 0672322714
Imports System.Windows.Forms
Imports System.IO
public class Note
public Shared Sub Main
Application.Run(New frmNote)
End Sub
End class
Public Class frmNote
Inherits System.Windows.Forms.Form
'***Added these two fields
Private m_sFileName As String
Private m_bDirty As Boolean
Public Property FileName() As String
Get
Return m_sFileName
End Get
Set(ByVal Value As String)
m_sFileName = Value
Me.Text = "Note - " & m_sFileName
End Set
End Property
Public Property Dirty() As Boolean
Get
Return m_bDirty
End Get
Set(ByVal Value As Boolean)
m_bDirty = Value
If Value = True Then
Me.Text = "Note - " & m_sFileName & "*"
End If
End Set
End Property
Public Sub NewFile()
Me.txtText.Text = ""
Me.FileName = "Untitled"
Me.Dirty = False
End Sub
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
NewFile()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private WithEvents mnuHelp As System.Windows.Forms.MenuItem
Private WithEvents mnuFileSep As System.Windows.Forms.MenuItem
Private WithEvents mnuEdit As System.Windows.Forms.MenuItem
Private WithEvents mnuMain As System.Windows.Forms.MainMenu
Private WithEvents mnuFileExit As System.Windows.Forms.MenuItem
Private WithEvents mnuHelpAbout As System.Windows.Forms.MenuItem
Private WithEvents mnuFileSave As System.Windows.Forms.MenuItem
Private WithEvents mnuFileSaveAs As System.Windows.Forms.MenuItem
Private WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
Private WithEvents mnuFile As System.Windows.Forms.MenuItem
Private WithEvents mnuFileNew As System.Windows.Forms.MenuItem
Private WithEvents txtText As System.Windows.Forms.TextBox
Private WithEvents mnuEditCopy As System.Windows.Forms.MenuItem
Private WithEvents mnuEditCut As System.Windows.Forms.MenuItem
Private WithEvents mnuEditPaste As System.Windows.Forms.MenuItem
Private WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
Private WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.dlgSave = New System.Windows.Forms.SaveFileDialog()
Me.mnuEdit = New System.Windows.Forms.MenuItem()
Me.mnuEditCopy = New System.Windows.Forms.MenuItem()
Me.mnuFile = New System.Windows.Forms.MenuItem()
Me.mnuMain = New System.Windows.Forms.MainMenu()
Me.txtText = New System.Windows.Forms.TextBox()
Me.mnuFileSep = New System.Windows.Forms.MenuItem()
Me.mnuEditPaste = New System.Windows.Forms.MenuItem()
Me.mnuFileSaveAs = New System.Windows.Forms.MenuItem()
Me.mnuFileSave = New System.Windows.Forms.MenuItem()
Me.mnuFileExit = New System.Windows.Forms.MenuItem()
Me.mnuEditCut = New System.Windows.Forms.MenuItem()
Me.mnuHelp = New System.Windows.Forms.MenuItem()
Me.dlgOpen = New System.Windows.Forms.OpenFileDialog()
Me.mnuHelpAbout = New System.Windows.Forms.MenuItem()
Me.mnuFileNew = New System.Windows.Forms.MenuItem()
Me.mnuFileOpen = New System.Windows.Forms.MenuItem()
Me.dlgSave.FileName = "note1"
Me.dlgSave.Filter = "Text Files|*.txt|All Files|*.*"
Me.mnuEdit.Index = 1
Me.mnuEdit.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEditCut, Me.mnuEditCopy, Me.mnuEditPaste})
Me.mnuEdit.Text = "&Edit"
Me.mnuEditCopy.Index = 1
Me.mnuEditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC
Me.mnuEditCopy.Text = "&Copy"
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNew, Me.mnuFileOpen, Me.mnuFileSave, Me.mnuFileSaveAs, Me.mnuFileSep, Me.mnuFileExit})
Me.mnuFile.Text = "&File"
Me.mnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuEdit, Me.mnuHelp})
Me.txtText.Dock = System.Windows.Forms.DockStyle.Fill
Me.txtText.Multiline = True
Me.txtText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtText.Size = New System.Drawing.Size(292, 253)
Me.txtText.TabIndex = 0
Me.mnuFileSep.Index = 4
Me.mnuFileSep.Text = "-"
Me.mnuEditPaste.Index = 2
Me.mnuEditPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV
Me.mnuEditPaste.Text = "&Paste"
Me.mnuFileSaveAs.Index = 3
Me.mnuFileSaveAs.Text = "Save &As..."
Me.mnuFileSave.Index = 2
Me.mnuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
Me.mnuFileSave.Text = "&Save"
Me.mnuFileExit.Index = 5
Me.mnuFileExit.Shortcut = System.Windows.Forms.Shortcut.CtrlQ
Me.mnuFileExit.Text = "E&xit"
Me.mnuEditCut.Index = 0
Me.mnuEditCut.Shortcut = System.Windows.Forms.Shortcut.CtrlX
Me.mnuEditCut.Text = "Cu&t"
Me.mnuHelp.Index = 2
Me.mnuHelp.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuHelpAbout})
Me.mnuHelp.Text = "&Help"
Me.dlgOpen.Filter = "Text Files|*.txt|All Files|*.*"
Me.mnuHelpAbout.Index = 0
Me.mnuHelpAbout.Text = "&About"
Me.mnuFileNew.Index = 0
Me.mnuFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
Me.mnuFileNew.Text = "&New"
Me.mnuFileOpen.Index = 1
Me.mnuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
Me.mnuFileOpen.Text = "&Open..."
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 253)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtText})
Me.Menu = Me.mnuMain
Me.Text = "Note"
End Sub
Private Sub mnuFileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click
If Me.Dirty = True Then
If MessageBox.Show("You have made changes to the file that will be lost. Continue?", "New File", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult().Yes Then
NewFile()
End If
Else
NewFile()
End If
End Sub
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
Dim oFile As FileStream
Dim oReader As StreamReader
If Me.dlgOpen.ShowDialog = DialogResult().OK Then
'OK, we can try to open and read the file
Try
Me.FileName = Me.dlgOpen.FileName
oFile = File.OpenRead(Me.FileName)
oReader = New StreamReader(oFile)
Me.txtText.Text = oReader.ReadToEnd
Catch ex As Exception
'just display the error for now
MessageBox.Show(ex.Message, "Error Opening File", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'remember to always close your readers and files
oReader.Close()
oFile.Close()
End Try
End If
End Sub
Private Sub mnuFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click
'we should only try to save this file if it has a name
If Me.FileName <> "Untitled" Then
'OK, let's try saving the file
Dim oFile As FileStream
Dim oWriter As StreamWriter
Try
oFile = File.OpenWrite(Me.FileName)
'convert the contents of the TextBox to an array of Bytes
oWriter = New StreamWriter(oFile)
'and write to the file
oWriter.Write(Me.txtText.Text)
'and now we're not dirty
Me.Dirty = False
Catch ex As Exception
'for now, just display an error on an Exception
MessageBox.Show(ex.Message, "Error Saving File", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'always remember to close all writers and streams
oWriter.Close()
oFile.Close()
End Try
Else
'if not, go get a name
mnuFileSaveAs_Click(sender, e)
End If
End Sub
Private Sub mnuFileSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSaveAs.Click
If Me.dlgSave.ShowDialog = DialogResult().OK Then
'they've clicked OK, we can set the filename and save
FileName() = Me.dlgSave.FileName
'use the code already in the File, Save item to save the file
mnuFileSave_Click(sender, e)
End If
End Sub
Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
Me.Close()
End Sub
Private Sub mnuEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEdit.Click
'is there any text selected?
If Me.txtText.SelectionLength > 0 Then
mnuEditCut.Enabled = True
mnuEditCopy.Enabled = True
Else
mnuEditCut.Enabled = False
mnuEditCopy.Enabled = False
End If
'is there anything on the Clipboard that we can paste here?
If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
mnuEditPaste.Enabled = True
Else
mnuEditPaste.Enabled = False
End If
End Sub
Private Sub mnuEditCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCut.Click
Clipboard.SetDataObject(Me.txtText.SelectedText)
Me.txtText.SelectedText = ""
End Sub
Private Sub mnuEditCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditCopy.Click
Clipboard.SetDataObject(Me.txtText.SelectedText)
End Sub
Private Sub mnuEditPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditPaste.Click
Me.txtText.SelectedText = CType(Clipboard.GetDataObject.GetData(DataFormats.Text), String)
End Sub
Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click
Dim sMessage As String
sMessage = "Note -- a simple text editor" & ControlChars.CrLf & _
"Original in Teach Yourself Visual Basic.NET in 21 Days." & ControlChars.CrLf & _
"copyright 2001. Full rights to extend provided by the author."
MessageBox.Show(sMessage, "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub txtText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtText.TextChanged
Me.Dirty = True
End Sub
Private Sub frmNote_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Me.Dirty = True Then
If MessageBox.Show("You have made changes to the file that will be lost. Continue?", "New File", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult().No Then
e.Cancel = True
End If
End If
End Sub
#End Region
End Class
|