Text Editor in Visual Basic

Introduction: This tutorial is on how to create a basic text editor in Visual Basic. Design: The design for this program is; Button, button1, Save the document. Button, button2, Open a document. Textbox, textbox1, Contain the document text. Imports: The only thing we need to import is System.IO which allows us to access files from the computers FileSystem...
  1. Imports System.IO
Button1 Click: To save a document, we first want to ensure that the textbox1 text is not empty (so there is something to actually save)...
  1. If (Not TextBox1.Text = Nothing) Then
  2.  
  3. Else
  4.         MsgBox("Nothing to save!")
  5. End If
Then if there is something to save, we are going to create a msgbox prompt asking if they want to specify a new save location. If the answer is no, the document will be saved to the path of a variable named 'path' which we are yet to create.
  1. If (Not path = Nothing) Then
  2.         Dim result = MsgBox("Would you like to save to a new location? (No = Save overwrite)", MsgBoxStyle.YesNoCancel, "Save Location")
  3.         If (result = MsgBoxResult.Yes) Then
  4.                 Using fs As New SaveFileDialog
  5.                         fs.MultiSelect = False
  6.                         fs.RestoreDirectory = False
  7.                         fs.ShowDialog()
  8.                         If (Not fs.FileName = Nothing) Then
  9.                                 Using sw As New StreamWriter(fs.FileName)
  10.                                         sw.Write(TextBox1.Text)
  11.                                 End Using
  12.                         End If
  13.                 End Using
  14.         Else If (result = MsgBoxResult.No) Then
  15.                 Using sw As New StreamWriter(path)
  16.                         sw.Write(TextBox1.Text)
  17.                 End Using
  18.         End If
  19. Else
  20.         Using fs As New SaveFileDialog
  21.                 fs.MultiSelect = False
  22.                 fs.RestoreDirectory = False
  23.                 fs.ShowDialog()
  24.                 If (Not fs.FileName = Nothing) Then
  25.                         Using sw As New StreamWriter(fs.FileName)
  26.                                 sw.Write(TextBox1.Text)
  27.                         End Using
  28.                 End If
  29.         End Using
  30. End If
Variables: The path variable I talked about earlier will simply hold the latest opened documents path so that we can offer the overwrite functionality later on. We create this in the global scope (outside of any other subs, functions or classes)...
  1. Dim path As String = ""
Button2 Click: To open a document, we simply give the user a new OpenFileDialog, allow them to select a file, and then read it to the textbox (textbox1)...
  1. Using fo As New OpenFileDialog
  2.         fo.MultiSelect = False
  3.         fo.RestoreDirectory = False
  4.         fo.ShowDialog()
  5.         If (Not fo.FileName = Nothing) Then
  6.                 Using sr As New StreamReader(fo.FileName)
  7.                         While (sr.Peek <> -1)
  8.                                 Textbox1.Text += sr.ReadLine()
  9.                         End While
  10.                 End Using
  11.         End If
  12. End Using

Add new comment