Menu

[r17]: / cpac-TextEditor / MainForm.vb  Maximize  Restore  History

Download this file

285 lines (243 with data), 10.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
' Copyright (C) 2006 Christopher Josph Dean Schaefer
'
' This program is free software; you can redistribute it and/or
' modify it under the terms of the GNU General Public License
' as published by the Free Software Foundation; either version 2
' of the License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Imports System.Drawing.Printing
Imports System.Text.RegularExpressions
Public Partial Class MainForm
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
'
' TODO : Add constructor code after InitializeComponents
'
End Sub
Sub ExitToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'exit
end
End Sub
Sub OpenToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'open
OpenFileDialog1.ShowDialog(Me)
me.Text = "CPAC-TextEditor " & "(" & OpenFileDialog1.FileName & ")"
End Sub
Sub SaveToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'save
'check to see if the last file opened exists if so write over it.
If System.IO.File.Exists(OpenFileDialog1.FileName) = True Then
richtextbox1.SaveFile(OpenFileDialog1.FileName,RichTextBoxStreamType.PlainText)
me.Text = "CPAC-TextEditor " & "(" & OpenFileDialog1.FileName & ")"
End If
End Sub
Sub SaveAsToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'save as
SaveFileDialog1.ShowDialog(Me)
me.Text = "CPAC-TextEditor " & "(" & SaveFileDialog1.FileName & ")"
End Sub
Private WithEvents pdoc As New PrintDocument()
Sub PrintToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'print
PrintDialog1.Document = pdoc
If PrintDialog1.ShowDialog(me) = DialogResult.OK Then
pdoc.Print()
End If
End Sub
Sub UndoToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'undo
richtextbox1.Undo
End Sub
Sub RedoToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'redo
richtextbox1.Redo
End Sub
Sub CutToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'cut
richtextbox1.Cut
End Sub
Sub CopyToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'copy
richtextbox1.Copy
End Sub
Sub PasteToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Paste
richtextbox1.Paste
End Sub
Sub DeleteToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Delete
richtextbox1.SelectedText = ""
End Sub
Sub SelectAllToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Select All
richtextbox1.SelectAll
End Sub
Sub WordCountToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Word Count
msgbox(("There are " & (richtextbox1.Text.Split(" ").GetUpperBound(0)+1) & " words in this document."),MsgBoxStyle.Information,"Word Count")
End Sub
Sub LineCountToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Line Count
msgbox(("There are " & (richtextbox1.Lines.GetUpperBound(0)+1) & " lines in this document."),MsgBoxStyle.Information,"Word Count")
End Sub
Sub OpenFileDialog1FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
If system.IO.File.Exists(OpenFileDialog1.FileName) = True Then
Richtextbox1.LoadFile(OpenFileDialog1.FileName,RichTextBoxStreamType.PlainText)
End If
End Sub
Sub SaveFileDialog1FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
Richtextbox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.PlainText)
End Sub
Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Static intCurrentChar As Int32
Dim font As New font("Microsoft Sans Serif", 24)
Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
With pdoc.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
marginLeft = .Margins.Left
marginTop = .Margins.Top
End With
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(richtextbox1.Text, intCurrentChar + 1), font,New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt,intCharsFitted, intLinesFilled)
e.Graphics.DrawString(Mid(richtextbox1.Text, intCurrentChar + 1), font,Brushes.Black, rectPrintingArea, fmt)
intCurrentChar += intCharsFitted
If intCurrentChar < richtextbox1.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Sub RichTextBox1TextChanged(sender As Object, e As System.EventArgs)
pageSetupDialog1.Document = pdoc
ToolStripStatusLabel1.Text = "Ready"
End Sub
Sub PrintSetupToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'print setup
PageSetupDialog1.ShowDialog(me)
End Sub
Sub NewToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'new
richtextbox1.Clear
OpenFileDialog1.FileName = ""
SaveFileDialog1.FileName = ""
End Sub
Sub MainFormLoad(sender As Object, e As System.EventArgs)
pageSetupDialog1.Document = pdoc
End Sub
Sub TimeDateToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'insert time and date
Dim cache As String = ""
If system.Windows.Forms.Clipboard.ContainsText = True Then
cache = system.Windows.Forms.Clipboard.GetText()
End If
system.Windows.Forms.Clipboard.SetText(Date.Now.ToString)
richtextbox1.Paste
system.Windows.Forms.Clipboard.SetText(cache)
End Sub
Protected FindDialogInput As CPAC_Lib_CommonTypes.FindData
Protected LastFind As Integer
Sub FindToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Find
LastFind = 0
Dim fd As New CPAC_Lib_Dialogs.FindDialog()
fd.ShowDialog(Me)
FindDialogInput = fd.data
If FindDialogInput.MatchCase = True Then
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase or RichTextBoxFinds.Reverse)
End If
Else
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.None)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.Reverse)
End If
End If
if LastFind <= 0 then ToolStripStatusLabel1.Text = "Not Found" else ToolStripStatusLabel1.Text = "Ready"
End Sub
Sub FindNextToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Find Next
If FindDialogInput.MatchCase = True Then
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase or RichTextBoxFinds.Reverse)
End If
Else
If FindDialogInput.SearchBackward = False Then
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.None)
Else
LastFind = 1 + richtextbox1.Find(FindDialogInput.Pattern,LastFind,RichTextBoxFinds.Reverse)
End If
End If
End Sub
Protected ReplaceDialogInput As CPAC_Lib_CommonTypes.ReplaceData
Sub ReplaceToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Replace
Dim rd As New CPAC_Lib_Dialogs.ReplaceDialog
rd.ShowDialog(Me)
ReplaceDialogInput = rd.data
If ReplaceDialogInput.Pattern <> "" Then
If ReplaceDialogInput.FindOnly = True Then
If ReplaceDialogInput.MatchCase = False Then
LastFind = 1 + richtextbox1.Find(ReplaceDialogInput.Pattern,LastFind,RichTextBoxFinds.None)
Else
LastFind = 1 + richtextbox1.Find(ReplaceDialogInput.Pattern,LastFind,RichTextBoxFinds.MatchCase)
End If
Else
dim reg as New Regex(ReplaceDialogInput.Pattern)
If ReplaceDialogInput.ReplaceOnce = False Then
richtextbox1.Text = reg.Replace(richtextbox1.Text,ReplaceDialogInput.Replacement)
Else
richtextbox1.Text = reg.Replace(richtextbox1.Text,ReplaceDialogInput.Replacement,1)
End If
End If
end if
End Sub
Sub WordWrapToolStripMenuItemClick(sender As Object, e As System.EventArgs)
If WordWrapToolStripMenuItem.Checked = True Then
WordWrapToolStripMenuItem.Checked = false
richtextbox1.WordWrap = WordWrapToolStripMenuItem.Checked
Else
WordWrapToolStripMenuItem.Checked = True
richtextbox1.WordWrap = WordWrapToolStripMenuItem.Checked
End If
End Sub
Sub FontToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'Select Font
If FontDialog1.ShowDialog(Me) = DialogResult.OK Then
If richtextbox1.SelectedText.Length > 0 Then
richtextbox1.SelectionFont = FontDialog1.Font
Else
richtextbox1.Font = FontDialog1.Font
End If
End If
End Sub
Sub AboutToolStripMenuItemClick(sender As Object, e As System.EventArgs)
'About Box
Dim ab As New About
ab.ShowDialog(me)
End Sub
End Class