View File

advertisement
NotePad Project Solution
•
What was required
•
What we did
frmNotePad
MenuStrip
txtNotePad
NewToolStripMenuItem
OpenToolStripMenuItem
SaveToolStripMenuItem
SaveAsToolStripMenuItem
PageSetupToolStripMenuItem
PrinterToolStripMenuItem
ExitToolStripMenuItem
UndoToolStripMenuItem
CutToolStripMenuItem
CopyToolStripMenuItem
PasteToolStripMenuItem
DeleteToolStripMenuItem
FindToolStripMenuItem
FindNextToolStripMenuItem
ReplaceToolStripMenuItem
GotoToolStripMenuItem
SelectAllToolStripMenuItem
TimeDateToolStripMenuItem
WordWrapToolStripMenuItem
FontToolStripMenuItem
StatusBarToolStripMenuItem
HelpTopicsToolStripMenuItem
AboutToolStripMenuItem
• Components Detail:
frmNotepad:
Modified Properties are:
 Name: frmNotepad
 (Icon) : C:\Document and Settings\Adeel\Desktop\Notepad.ico
 Text: Untitled – Notepad
Code:
Global variables are defined on top of the code under the Public Class frmNotepad:
Dim WordWrap = 1
Dim Status = 0
Dim Ans As Integer
Private Sub frmNotepad_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Resize
txtNotePad.Width = Me.Width - 8
txtNotePad.Height = Me.Height - 58
End Sub
txtNotepad:
Modified Properties are:
 Name: txtNotepad
 Multiline: True
 ScrollBars: Vertical
Code:
Private Sub txtNotePad_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtNotePad.TextChanged
Me.ToolStripStatusLabel.Text = "Ln " +
CStr(CInt(txtNotePad.GetLineFromCharIndex(txtNotePad.SelectionStart) +
1)) + ",Col " + CStr((CInt(txtNotePad.SelectionStart) CInt(txtNotePad.GetFirstCharIndexOfCurrentLine) + 1))
End Sub
Private Sub txtNotePad_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtNotePad.KeyDown
Me.ToolStripStatusLabel.Text = "Ln " +
CStr(CInt(txtNotePad.GetLineFromCharIndex(txtNotePad.SelectionStart) +
1)) + ",Col " + CStr((CInt(txtNotePad.SelectionStart) CInt(txtNotePad.GetFirstCharIndexOfCurrentLine) + 1))
End Sub
Modified Properties are:
 Name: MenuStrip
Code:
Private Sub NewToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
NewToolStripMenuItem.Click
If txtNotePad.Modified = True Then
Ans = MsgBox("The text in the " + Me.Text.Substring(0, 9) +
" file has changed." + Chr(13) + Chr(13) + "Do you want to save the
changes?", MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Exclamation)
If (Ans = MsgBoxResult.Yes) Then
Dim Writer As System.IO.StreamWriter
Me.SaveFileDialog.FileName = "*.txt"
Me.SaveFileDialog.Filter = "Text files
(*.txt)|*.txt|All files (*.*)|*.*"
If Me.Text = "Untitled - Notepad" Then
If (Me.SaveFileDialog.ShowDialog =
Windows.Forms.DialogResult.OK) Then
Writer =
IO.File.CreateText(Me.SaveFileDialog.FileName)
Writer.Write(txtNotePad.Text)
Writer.Close()
Me.Text = Me.SaveFileDialog.FileName + " Notepad"
txtNotePad.Modified = False
txtNotePad.Clear()
UndoToolStripMenuItem.Enabled = False
End If
Else
Writer = IO.File.CreateText(Mid(Me.Text, 1,
InStr(Me.Text, "- Notepad") - 1))
Writer.Write(txtNotePad.Text)
Writer.Close()
txtNotePad.Modified = False
txtNotePad.Clear()
UndoToolStripMenuItem.Enabled = False
End If
ElseIf (Ans = MsgBoxResult.No) Then
txtNotePad.Clear()
Me.Text = "Untitled - Notepad"
UndoToolStripMenuItem.Enabled = False
txtNotePad.Modified = False
End If
Else
txtNotePad.Clear()
Me.Text = "Untitled - Notepad"
txtNotePad.Modified = False
UndoToolStripMenuItem.Enabled = False
End If
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ExitToolStripMenuItem.Click
Me.Close()
End Sub
Modified Properties are:
 Name: StatusStrip
Modified Properties are:
 Name: OpenFileDialog
Code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
OpenToolStripMenuItem.Click
If txtNotePad.Modified = True Then
Ans = MsgBox("The text in the " + Me.Text.Substring(0, 9) +
" file has changed." + Chr(13) + Chr(13) + "Do you want to save the
changes?", MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Exclamation)
If (Ans = MsgBoxResult.Yes) Then
Dim Writer As System.IO.StreamWriter
Me.SaveFileDialog.FileName = "*.txt"
Me.SaveFileDialog.Filter = "Text files
(*.txt)|*.txt|All files (*.*)|*.*"
Writer = IO.File.CreateText(Mid(Me.Text, 1,
InStr(Me.Text, "- Notepad") - 1))
Writer.Write(txtNotePad.Text)
Writer.Close()
txtNotePad.Modified = False
Me.OpenFileDialog.FileName = "*.txt"
Me.OpenFileDialog.Filter = "Text files
(*.txt)|*.txt|All files (*.*)|*.*"
If (Me.OpenFileDialog.ShowDialog =
Windows.Forms.DialogResult.OK) Then
Dim Reader As System.IO.StreamReader
Dim EntireFile As String
Reader =
IO.File.OpenText(Me.OpenFileDialog.FileName)
EntireFile = Reader.ReadToEnd()
txtNotePad.Text = EntireFile
Me.Text = Me.OpenFileDialog.FileName + " - Notepad"
End If
ElseIf (Ans = MsgBoxResult.No) Then
Me.OpenFileDialog.FileName = "*.txt"
Me.OpenFileDialog.Filter = "Text files
(*.txt)|*.txt|All files (*.*)|*.*"
If (Me.OpenFileDialog.ShowDialog =
Windows.Forms.DialogResult.OK) Then
Dim Reader As System.IO.StreamReader
Dim EntireFile As String
Reader =
IO.File.OpenText(Me.OpenFileDialog.FileName)
EntireFile = Reader.ReadToEnd()
txtNotePad.Text = EntireFile
Me.Text = Me.OpenFileDialog.FileName + " - Notepad"
End If
End If
Else
Me.OpenFileDialog.FileName = "*.txt"
Me.OpenFileDialog.Filter = "Text files (*.txt)|*.txt|All
files (*.*)|*.*"
If (Me.OpenFileDialog.ShowDialog =
Windows.Forms.DialogResult.OK) Then
Dim Reader As System.IO.StreamReader
Dim EntireFile As String
Reader = IO.File.OpenText(Me.OpenFileDialog.FileName)
EntireFile = Reader.ReadToEnd()
txtNotePad.Text = EntireFile
Me.Text = Me.OpenFileDialog.FileName + " - Notepad"
End If
End If
End Sub
Modified Properties are:
 Name: SaveFileDialog
Code:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SaveToolStripMenuItem.Click
Dim Writer As System.IO.StreamWriter
Me.SaveFileDialog.FileName = "*.txt"
Me.SaveFileDialog.Filter = "Text files (*.txt)|*.txt|All files
(*.*)|*.*"
If Me.Text = "Untitled - Notepad" Then
If (Me.SaveFileDialog.ShowDialog =
Windows.Forms.DialogResult.OK) Then
Writer = IO.File.CreateText(Me.SaveFileDialog.FileName)
Writer.Write(txtNotePad.Text)
Writer.Close()
Me.Text = Me.SaveFileDialog.FileName + " - Notepad"
txtNotePad.Modified = False
End If
Else
Writer = IO.File.CreateText(Mid(Me.Text, 1, InStr(Me.Text,
"- Notepad") - 1))
Writer.Write(txtNotePad.Text)
Writer.Close()
txtNotePad.Modified = False
End If
End Sub
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SaveAsToolStripMenuItem.Click
Me.SaveFileDialog.FileName = "*.txt"
Me.SaveFileDialog.Filter = "Text files (*.txt)|*.txt|All files
(*.*)|*.*"
If (Me.SaveFileDialog.ShowDialog =
Windows.Forms.DialogResult.OK) Then
Dim Writer As System.IO.StreamWriter
Writer = IO.File.CreateText(Me.SaveFileDialog.FileName)
Writer.Write(txtNotePad.Text)
Writer.Close()
Me.Text = Me.SaveFileDialog.FileName + " - Notepad"
txtNotePad.Modified = False
End If
End Sub
Modified Properties are:
 Name: PageSetupDialog
Code:
Private Sub PageSetupToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PageSetupToolStripMenuItem.Click
With PageSetupDialog
.PageSettings = PrintDocument.DefaultPageSettings
End With
Try
If PageSetupDialog.ShowDialog =
Windows.Forms.DialogResult.OK Then
PrintDocument.DefaultPageSettings =
PageSetupDialog.PageSettings
End If
Catch es As Exception
If PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK
Then
PrintDocument.Print()
End If
End Try
End Sub
Modified Properties are:
 Name: PrintDialog
Code:
Private Sub PrintToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
PrintToolStripMenuItem.Click
If PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDocument.DocumentName = (Mid(Me.Text, 1,
InStr(Me.Text, "- Notepad") - 1))
PrintDocument.Print()
End If
End Sub
Modified Properties are:
 Name: PrintDocument
Code:
Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument.PrintPage
'PrintPage is the foundational printing event. This event gets
fired for every
' page that will be printed
Static intCurrentChar As Int32
' declaring a static variable to hold the position of the last
printed char
Dim font As New Font("Verdana", 14)
' initializing the font to be used for printing
Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As
Int32
With PrintDocument.DefaultPageSettings
' initializing local variables that contain the bounds of
the printing area rectangle
PrintAreaHeight = .PaperSize.Height - .Margins.Top .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left .Margins.Right
' initializing local variables to hold margin values that
will serve
' as the X and Y coordinates for the upper left corner of
the printing
' area rectangle.
marginLeft = .Margins.Left
marginTop = .Margins.Top
' X and Y coordinate
End With
If PrintDocument.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = intTemp
' if the user selects landscape mode, swap the printing
area height and width
End If
Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
' calculating the total number of lines in the document based
on the height of
' the printing area and the height of the font
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop,
PrintAreaWidth, PrintAreaHeight)
' initializing the rectangle structure that defines the
printing area
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
'instantiating the StringFormat class, which encapsulates text
layout information
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(txtNotePad.Text, intCurrentChar +
1), font, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt,
intCharsFitted, intLinesFilled)
' calling MeasureString to determine the number of characters
that will fit in
' the printing area rectangle
e.Graphics.DrawString(Mid(txtNotePad.Text, intCurrentChar + 1),
font, Brushes.Black, rectPrintingArea, fmt)
' print the text to the page
intCurrentChar += intCharsFitted
'advancing the current char to the last char printed on this
page
If intCurrentChar < txtNotePad.Text.Length Then
e.HasMorePages = True
'HasMorePages tells the printing module whether another
PrintPage event should be fired
Else
e.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Modified Properties are:
 Name: FontDialog
Code:
Private Sub FontToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
FontToolStripMenuItem.Click
If Me.FontDialog.ShowDialog = Windows.Forms.DialogResult.OK
Then
txtNotePad.Font = Me.FontDialog.Font
End If
End Sub
Private Sub FontDialog_Apply(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles FontDialog.Apply
Me.txtNotePad.Font = FontDialog.Font
End Sub
Modified Properties are:
 Name: Timer
Code:
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer.Tick
If txtNotePad.SelectedText.Length = 0 Then
CutToolStripMenuItem.Enabled = False
CopyToolStripMenuItem.Enabled = False
DeleteToolStripMenuItem.Enabled = False
Else
CutToolStripMenuItem.Enabled = True
CopyToolStripMenuItem.Enabled = True
DeleteToolStripMenuItem.Enabled = True
End If
If Clipboard.ContainsText = True Then
PasteToolStripMenuItem.Enabled = True
Else
PasteToolStripMenuItem.Enabled = False
End If
If (txtNotePad.Modified = True) Then
UndoToolStripMenuItem.Enabled = True
End If
If Len(txtNotePad.Text) > 0 Then
FindToolStripMenuItem.Enabled = True
FindNextToolStripMenuItem.Enabled = True
Else
FindToolStripMenuItem.Enabled = False
FindNextToolStripMenuItem.Enabled = False
End If
End Sub
Modified Properties are:
 Name: ctrlFindReplce
Code:
Private Sub FindToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
FindToolStripMenuItem.Click
Me.ctrlFindReplace.Type =
FindReplace.FindReplaceDialogType.Find
'Me.ctrlFindReplace.FindString(txtNotePad)
Me.ctrlFindReplace.Show(Me)
End Sub
Private Sub FindNextToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
FindNextToolStripMenuItem.Click
Me.ctrlFindReplace.Type =
FindReplace.FindReplaceDialogType.Find
'Me.ctrlFindReplace.FindString(txtNotePad)
End Sub
Private Sub ReplaceToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ReplaceToolStripMenuItem.Click
Me.ctrlFindReplace.Type =
FindReplace.FindReplaceDialogType.Replace
Me.ctrlFindReplace.Show(Me)
End Sub
Private Sub ctrlFindReplace_FindNextClick() Handles
ctrlFindReplace.FindNextClick
' Handles FindNext event raised by the FindReplaceDialog. You
can write custom code to
' find the string here.
Dim activeControl As System.Windows.Forms.Control
Dim txtBox As System.Windows.Forms.TextBox
activeControl = Me.ActiveControl
If TypeOf activeControl Is System.Windows.Forms.TextBox Then
txtBox = CType(activeControl, System.Windows.Forms.TextBox)
ctrlFindReplace.FindString(txtBox)
End If
End Sub
Private Sub ctrlFindReplace_Help() Handles ctrlFindReplace.Help
' Handles Help event raised by the FindReplaceDialog. You can
write custom code to
' display a help topic here.
' MsgBox("Help button was clicked.")
End Sub
Private Sub ctrlFindReplace_DialogTerminate() Handles
ctrlFindReplace.DialogTerminate
' Handles DialogTerminate event raised by the
FindReplaceDialog. You can write custom code to
' run when the dialog closes here.
'MsgBox("Dialog Terminate")
End Sub
Private Sub ctrlFindReplace_ReplaceClick() Handles
ctrlFindReplace.ReplaceClick
' Handles Replace event raised by the FindReplaceDialog. You
can write custom code to
' replace found text here.
Dim activeControl As System.Windows.Forms.Control
Dim txtBox As System.Windows.Forms.TextBox
activeControl = Me.ActiveControl
If TypeOf activeControl Is System.Windows.Forms.TextBox Then
txtBox = CType(activeControl, System.Windows.Forms.TextBox)
ctrlFindReplace.ReplaceString(txtBox, replaceAll:=False)
End If
End Sub
Private Sub ctrlFindReplace_ReplaceAllClick() Handles
ctrlFindReplace.ReplaceAllClick
' Handles ReplaceAll event raised by the FindReplaceDialog. You
can write custom code to
' replace all occurrences of found text here.
Dim activeControl As System.Windows.Forms.Control
Dim txtBox As System.Windows.Forms.TextBox
activeControl = Me.ActiveControl
If TypeOf activeControl Is System.Windows.Forms.TextBox Then
txtBox = CType(activeControl, System.Windows.Forms.TextBox)
ctrlFindReplace.ReplaceString(txtBox, ReplaceAll:=True)
End If
End Sub
Private Sub ctrlFindReplace_TextFound(ByVal eventArguments As
FindReplace.FindReplaceEventArgs) Handles ctrlFindReplace.TextFound
' MsgBox("Found """ & eventArguments.FindWhat & """ at position
" & eventArguments.Position)
End Sub
Private Sub ctrlFindReplace_SearchFailed(ByVal eventArguments As
FindReplace.FindReplaceEventArgs) Handles ctrlFindReplace.SearchFailed
MsgBox("Can not find """ & eventArguments.FindWhat & """",
MsgBoxStyle.Information)
End Sub
Other Code in Menus:
Private Sub GotoToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
GotoToolStripMenuItem.Click
Ans = InputBox("Line Number:", "Goto line", "1")
Go_to(Int(Ans), 1)
Me.ToolStripStatusLabel.Text = "Ln " +
CStr(CInt(txtNotePad.GetLineFromCharIndex(txtNotePad.SelectionStart) +
1)) + ",Col " + CStr((CInt(txtNotePad.SelectionStart) CInt(txtNotePad.GetFirstCharIndexOfCurrentLine) + 1))
' If Ans < 1 Or (Ans > 1 And Ans > txtNotePad.Lines.Length)
Then
' MsgBox("Line number out of range", MsgBoxStyle.OkOnly,
"Notepad - Goto Line")
' Else
' SendKeys.Send("^+{HOME}") 'Goto Top Left
' If Ans > 1 Then
' Dim i As Integer
' For i = 1 To Ans - 1
' SendKeys.Send("{DOWN}")
' Next i
' End If
' End If
End Sub
Private Sub Go_to(ByVal line As Integer, ByVal column As Integer)
If line < 1 Or column < 1 Or txtNotePad.TextLength < line Then
Return
Else
txtNotePad.SelectionStart =
txtNotePad.GetFirstCharIndexFromLine(line - 1) + column - 1
txtNotePad.SelectionLength = 0
SendKeys.Send("{HOME}")
End If
End Sub
Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SelectAllToolStripMenuItem.Click
txtNotePad.SelectAll()
End Sub
Private Sub TimeDateToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
TimeDateToolStripMenuItem.Click
Dim Timestamp As String
Timestamp = Now.Date + Now.TimeOfDay
SendKeys.Send(Timestamp)
End Sub
Private Sub WordWrapToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
WordWrapToolStripMenuItem.Click
WordWrap = -WordWrap + 1
WordWrapToolStripMenuItem.Checked = WordWrap
txtNotePad.WordWrap = WordWrap
txtNotePad.ScrollBars = ScrollBars.Both - WordWrap
If WordWrap = 1 Then
StatusBarToolStripMenuItem.Enabled = False
Me.StatusStrip.Hide()
txtNotePad.Height = Me.Height - 58
Else
StatusBarToolStripMenuItem.Enabled = True
If StatusBarToolStripMenuItem.Checked = True Then
Me.StatusStrip.Show()
txtNotePad.Height = Me.Height - 78
End If
End If
End Sub
•
Possible Alternatives
•
Evaluate
•
What we learnt
•
Re-usable components
•
Code Snippets
•
Optimize
Download