Advertisement
asril99

SRC-Notepad

Aug 15th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. Public Class Form1
  2.  
  3. Private Sub MsubExit_Click(sender As Object, e As EventArgs) Handles MsubExit.Click
  4. If MessageBox.Show("Are you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
  5. 'you can use the more method
  6. '1- End
  7. '2- Close
  8. '3- Dispose
  9. '4- Application.Exit
  10. 'but please remeber the above method is difference but is a normally we can use close method
  11. Me.Close()
  12. 'Me.Dispose()
  13. 'End
  14. 'Application.Exit()
  15. End If
  16. End Sub
  17.  
  18. Private Sub MSubNew_Click(sender As Object, e As EventArgs) Handles MSubNew.Click
  19. 'check if there text added to textbox
  20. If txtData.Modified Then
  21. 'if text in Textbox is been changed , the My Notepad App Ask
  22. Dim a As MsgBoxResult
  23. a = MsgBox("Do you want to save change to " & Me.Text, MsgBoxStyle.YesNoCancel, "New Document")
  24. If a = MsgBoxResult.No Then
  25. 'you can set clear textbox by default method or you can set string space the default method set null value but is not better. like this you can set string space is better
  26. 'txtData.Clear()
  27. txtData.Text = ""
  28. ElseIf a = MsgBoxResult.Cancel Then
  29. ElseIf a = MsgBoxResult.Yes Then
  30. SfdNotepad.ShowDialog()
  31. 'write text to Text File...
  32. My.Computer.FileSystem.WriteAllText(SfdNotepad.FileName, txtData.Text, False)
  33. txtData.Text = ""
  34. TsLbNotepad.Text = "Ready Saved ...."
  35. Else
  36. txtData.Text = ""
  37. End If
  38. End If
  39. End Sub
  40.  
  41. Private Sub txtData_TextChanged(sender As Object, e As EventArgs) Handles txtData.TextChanged
  42. TsLbNotepad.Text = ""
  43. End Sub
  44.  
  45. Private Sub MSubFont_Click(sender As Object, e As EventArgs) Handles MSubFont.Click
  46. 'get default font of FontLDialog
  47. FdNotepad.ShowDialog()
  48. txtData.Font = FdNotepad.Font
  49. End Sub
  50.  
  51. Private Sub MsubOpen_Click(sender As Object, e As EventArgs) Handles MsubOpen.Click
  52. 'check if text is added
  53. If txtData.Modified Then
  54. 'if text of Textbox is changed, so ask
  55. Dim a As MsgBoxResult
  56. a = MsgBox("Do you want to save change to" & OfdNotepad.FileName, MsgBoxStyle.YesNoCancel, "Open Document")
  57. If a = MsgBoxResult.No Then
  58. OfdNotepad.ShowDialog()
  59. txtData.Text = My.Computer.FileSystem.ReadAllText(OfdNotepad.FileName)
  60. Me.Text = OfdNotepad.FileName
  61. ElseIf a = MsgBoxResult.Cancel Then
  62. 'nothing
  63. ElseIf a = MsgBoxResult.Yes Then
  64. SfdNotepad.ShowDialog()
  65. My.Computer.FileSystem.WriteAllText(SfdNotepad.FileName, txtData.Text, False)
  66. txtData.Text = ""
  67. End If
  68. Else
  69. 'if textbox still the same
  70. OfdNotepad.ShowDialog()
  71. Try
  72. txtData.Text = My.Computer.FileSystem.ReadAllText(OfdNotepad.FileName)
  73. Me.Text = OfdNotepad.FileName
  74. Catch ex As Exception
  75. MsgBox(ex.Message)
  76. End Try
  77. End If
  78. End Sub
  79.  
  80. Private Sub MsubSave_Click(sender As Object, e As EventArgs) Handles MsubSave.Click
  81. If SfdNotepad.ShowDialog = Windows.Forms.DialogResult.OK Then
  82. 'check exist file
  83. If My.Computer.FileSystem.FileExists(SfdNotepad.FileName) Then
  84. If MessageBox.Show(SfdNotepad.FileName & " Already exist file do you want to replace its?", "File Exist", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
  85. SfdNotepad.ShowDialog()
  86. Else
  87. My.Computer.FileSystem.WriteAllText(SfdNotepad.FileName, txtData.Text, False)
  88. End If
  89. End If
  90. Else
  91. 'use try catch
  92. Try
  93. My.Computer.FileSystem.WriteAllText(SfdNotepad.FileName, txtData.Text, False)
  94. Catch ex As Exception
  95. MsgBox(ex.Message)
  96. End Try
  97. End If
  98. End Sub
  99.  
  100. Private Sub MsubSaveAs_Click(sender As Object, e As EventArgs) Handles MsubSaveAs.Click
  101. If SfdNotepad.ShowDialog = Windows.Forms.DialogResult.OK Then
  102. My.Computer.FileSystem.WriteAllText(SfdNotepad.FileName, txtData.Text, False)
  103. txtData.Text = ""
  104. TsLbNotepad.Text = "Saved file ..."
  105. End If
  106. End Sub
  107.  
  108. Private Sub MsubPageSetup_Click(sender As Object, e As EventArgs) Handles MsubPageSetup.Click
  109. 'initialize page setting
  110. PsdNotepad.PageSettings = New Printing.PageSettings
  111.  
  112. 'hide the network button if you want
  113. PsdNotepad.ShowNetwork = False
  114.  
  115. If PsdNotepad.ShowDialog = Windows.Forms.DialogResult.OK Then
  116. Dim s() As Object = New Object() {PsdNotepad.PageSettings.Margins, PsdNotepad.PageSettings.PaperSize, PsdNotepad.PageSettings.Landscape, PsdNotepad.PrinterSettings.PrinterName, PsdNotepad.PrinterSettings.PrintRange}
  117. End If
  118. End Sub
  119.  
  120. Private Sub MsubPrintPreview_Click(sender As Object, e As EventArgs) Handles MsubPrintPreview.Click
  121. PpdNotepad.Document = PDoc
  122. PpdNotepad.ShowDialog()
  123. End Sub
  124.  
  125. Private Sub PDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PDoc.PrintPage
  126. Static c_char, c_line As Integer
  127. Dim tfont As Font = txtData.Font
  128. Dim h, w, left, top As Integer
  129. With PDoc.DefaultPageSettings
  130. h = .PaperSize.Height - .Margins.Top - .Margins.Bottom
  131. w = .PaperSize.Width - .Margins.Left - .Margins.Right
  132. left = PDoc.DefaultPageSettings.Margins.Left
  133. top = PDoc.DefaultPageSettings.Margins.Top
  134. End With
  135. e.Graphics.DrawRectangle(Pens.Black, New Rectangle(left, top, w, h))
  136. If PDoc.DefaultPageSettings.Landscape Then
  137. Dim i As Integer
  138. i = h
  139. h = w
  140. w = i
  141. End If
  142. Dim lines As Integer = CInt(Math.Round(h / txtData.Height))
  143. Dim j As New Rectangle(left, top, w, h)
  144. Dim frt As StringFormat
  145. If Not txtData.WordWrap Then
  146. frt = New StringFormat(StringFormatFlags.NoWrap)
  147. frt.Trimming = StringTrimming.EllipsisWord
  148. Dim k As Integer
  149. For k = c_line To Math.Min(c_line + lines, txtData.Lines.Length - 1)
  150. e.Graphics.DrawString(txtData.Lines(k), tfont, Brushes.Black, New Rectangle(left, top + tfont.Height * (k - c_line), w, tfont.Height), frt)
  151. Next
  152. c_line += lines
  153. If c_line >= txtData.Lines.Length Then
  154. e.HasMorePages = False
  155. c_line = 0
  156. Else
  157. e.HasMorePages = True
  158. End If
  159. Exit Sub
  160. End If
  161. frt = New StringFormat(StringFormatFlags.LineLimit)
  162. Dim l, c As Integer
  163. e.Graphics.MeasureString(Mid(txtData.Text, c_char + 1), tfont, New SizeF(w, h), frt, c, l)
  164. If c_char + c < txtData.Text.Length Then If txtData.Text.Substring(c_char + c, 1) <> " " And txtData.Text.Substring(c_char + c, 1) <> vbLf Then
  165. While c > 0
  166. txtData.Text.Substring(c_char + c, 1)
  167. c -= 1
  168. End While
  169. c += 1
  170. End If
  171. End If
  172. e.Graphics.DrawString(txtData.Text.Substring(c_char, c), tfont, Brushes.Black, j, frt)
  173. c_char = c_char + c
  174. If c_char < txtData.Text.Length Then e.HasMorePages = True Else e.HasMorePages = False c_char = 0 End If End Sub Private Sub MsubPrint_Click(sender As Object, e As EventArgs) Handles MsubPrint.Click With PdNotepad If .ShowDialog = Windows.Forms.DialogResult.OK Then PDoc.Print() End If End With End Sub Private Sub MSubUndo_Click(sender As Object, e As EventArgs) Handles MSubUndo.Click 'check user click undo If txtData.CanUndo Then txtData.Undo() End If End Sub Private Sub MSubCut_Click(sender As Object, e As EventArgs) Handles MSubCut.Click My.Computer.Clipboard.Clear() If txtData.SelectionLength > 0 Then
  175. My.Computer.Clipboard.SetText(txtData.SelectedText)
  176. End If
  177. txtData.SelectedText = ""
  178. End Sub
  179.  
  180. Private Sub MSubCopy_Click(sender As Object, e As EventArgs) Handles MSubCopy.Click
  181. My.Computer.Clipboard.Clear()
  182. If txtData.SelectionLength > 0 Then
  183. My.Computer.Clipboard.SetText(txtData.SelectedText)
  184. End If
  185. End Sub
  186.  
  187. Private Sub MSubPast_Click(sender As Object, e As EventArgs) Handles MSubPast.Click
  188. If My.Computer.Clipboard.ContainsText Then
  189. txtData.Paste()
  190. End If
  191.  
  192. End Sub
  193.  
  194. Private Sub MSubSelectall_Click(sender As Object, e As EventArgs) Handles MSubSelectall.Click
  195. txtData.SelectAll()
  196. End Sub
  197.  
  198. Private Sub MSubFind_Click(sender As Object, e As EventArgs) Handles MSubFind.Click
  199. Dim j, k As String
  200. j = InputBox("Input Text here: ")
  201. k = InStr(txtData.Text, j)
  202. If k Then
  203. txtData.Focus()
  204. txtData.SelectionStart = k - 1
  205. txtData.SelectionLength = Len(j)
  206. Else
  207. MsgBox("text is not found.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Not found")
  208. End If
  209. End Sub
  210.  
  211. Private Sub MSubDelete_Click(sender As Object, e As EventArgs) Handles MSubDelete.Click
  212. txtData.SelectedText = ""
  213. End Sub
  214.  
  215. Private Sub MSubReplace_Click(sender As Object, e As EventArgs) Handles MSubReplace.Click
  216. With Findata
  217. .txtReplace.Focus()
  218. If .ShowDialog = Windows.Forms.DialogResult.OK Then
  219. 'check text on Textbox and make replace it
  220. If txtData.Text.Contains(.txtReplace.Text) Then
  221. txtData.Text = txtData.Text.Replace(.txtReplace.Text, .txtTo.Text)
  222. End If
  223. End If
  224. End With
  225. End Sub
  226.  
  227. Private Sub MSubDateTime_Click(sender As Object, e As EventArgs) Handles MSubDateTime.Click
  228. 'check textbox focus or not
  229. If txtData.Focus = True Then
  230. txtData.Text += Now
  231. End If
  232. End Sub
  233.  
  234. Private Sub MSubStatusBar_Click(sender As Object, e As EventArgs) Handles MSubStatusBar.Click
  235. If MSubStatusBar.CheckState = CheckState.Checked Then
  236. SsNotepad.Visible = False
  237. MSubStatusBar.CheckState = CheckState.Unchecked
  238. Else
  239. SsNotepad.Visible = True
  240. MSubStatusBar.CheckState = CheckState.Checked
  241. End If
  242. End Sub
  243.  
  244. Private Sub MSubFindNext_Click(sender As Object, e As EventArgs) Handles MSubFindNext.Click
  245. MSubFind_Click(sender, e)
  246. End Sub
  247.  
  248. Private Sub AboutNotepadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutNotepadToolStripMenuItem.Click
  249. AboutBox1.ShowDialog()
  250. End Sub
  251.  
  252. Private Sub MSubFontColor_Click(sender As Object, e As EventArgs) Handles MSubFontColor.Click
  253. CDNotepad.ShowDialog()
  254. txtData.ForeColor = CDNotepad.Color
  255. End Sub
  256.  
  257. Private Sub MSubBackgroundColor_Click(sender As Object, e As EventArgs) Handles MSubBackgroundColor.Click
  258. CDNotepad.ShowDialog()
  259. txtData.BackColor = CDNotepad.Color
  260. End Sub
  261.  
  262. Private Sub MSubLeft_Click(sender As Object, e As EventArgs) Handles MSubLeft.Click
  263. 'txtData.TextAlign()
  264. 'we can't use it if you want to use it you replaace textbox to richtextbox
  265. End Sub
  266. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement