How to Create a Text Editor in Visual Basic

Submitted by: 
Language: 
Visitors have accessed this post 10160 times.

Introduction:
This tutorial is on how to create a text editor in Visual Basic.

Steps of Creation:
Step 1:

First we need a form. Mine will consist of;
- Richtextbox1 - Contain text contents
- Button1 - Load File
- Button2 - Save File
- Button3 - Save File As New Name

Step 2:
Now lets import System.IO so we can read and write files. We also want to create a path String variable to contain our opened file path. Let's also make a load file function. It will simply open a browser dialog and read it to the textbox.

  1. Imports System.IO
  1. Dim path As String = Nothing
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. Dim fo As New OpenFileDialog
  3. fo.Filter = "Text Files|*.txt"
  4. fo.FilterIndex = 1
  5. fo.ShowDialog()
  6. If (fo.FileName = Nothing) Then
  7. MsgBox("No file selected.")
  8. Else
  9. path = fo.FileName
  10. Using sr As New StreamReader(fo.FileName)
  11. RichTextBox1.Text = sr.ReadToEnd()
  12. End Using
  13. End If
  14. End Sub

Step 3:
Now for the save function. We will be writing to our path variable (the path we loaded the file from.)

  1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  2. If (Not path = Nothing) Then
  3. Using sw As New StreamWriter(path)
  4. sw.Write(RichTextBox1.Text)
  5. End Using
  6. End If
  7. End Sub

Step 4:
Finally for the save as function.

  1. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  2. Dim fs As New SaveFileDialog
  3. fs.Filter = "Text Files|*.txt"
  4. fs.FilterIndex = 1
  5. fs.ShowDialog()
  6. If (Not fs.FileName = Nothing) Then
  7. Using sw As New StreamWriter(fs.FileName)
  8. sw.Write(RichTextBox1.Text)
  9. End Using
  10. End If
  11. End Sub

Project Complete!
Below is the full source code and download to the project files.

  1. Imports System.IO
  2. Public Class Form1
  3. Dim path As String = Nothing
  4. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5. Dim fo As New OpenFileDialog
  6. fo.Filter = "Text Files|*.txt"
  7. fo.FilterIndex = 1
  8. fo.ShowDialog()
  9. If (fo.FileName = Nothing) Then
  10. MsgBox("No file selected.")
  11. Else
  12. path = fo.FileName
  13. Using sr As New StreamReader(fo.FileName)
  14. RichTextBox1.Text = sr.ReadToEnd()
  15. End Using
  16. End If
  17. End Sub
  18.  
  19. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  20. If (Not path = Nothing) Then
  21. Using sw As New StreamWriter(path)
  22. sw.Write(RichTextBox1.Text)
  23. End Using
  24. End If
  25. End Sub
  26.  
  27. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  28. Dim fs As New SaveFileDialog
  29. fs.Filter = "Text Files|*.txt"
  30. fs.FilterIndex = 1
  31. fs.ShowDialog()
  32. If (Not fs.FileName = Nothing) Then
  33. Using sw As New StreamWriter(fs.FileName)
  34. sw.Write(RichTextBox1.Text)
  35. End Using
  36. End If
  37. End Sub
  38. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.