How to Create a Text Editor in Visual Basic
Submitted by Yorkiebar on Tuesday, October 1, 2013 - 04:42.
Language
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.
Step 3:
Now for the save function. We will be writing to our path variable (the path we loaded the file from.)
Step 4:
Finally for the save as function.
Project Complete!
Below is the full source code and download to the project files.
- Imports System.IO
- Dim path As String = Nothing
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim fo As New OpenFileDialog
- fo.Filter = "Text Files|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If (fo.FileName = Nothing) Then
- MsgBox("No file selected.")
- Else
- path = fo.FileName
- Using sr As New StreamReader(fo.FileName)
- RichTextBox1.Text = sr.ReadToEnd()
- End Using
- End If
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- If (Not path = Nothing) Then
- Using sw As New StreamWriter(path)
- sw.Write(RichTextBox1.Text)
- End Using
- End If
- End Sub
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- Dim fs As New SaveFileDialog
- fs.Filter = "Text Files|*.txt"
- fs.FilterIndex = 1
- fs.ShowDialog()
- If (Not fs.FileName = Nothing) Then
- Using sw As New StreamWriter(fs.FileName)
- sw.Write(RichTextBox1.Text)
- End Using
- End If
- End Sub
- Imports System.IO
- Public Class Form1
- Dim path As String = Nothing
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim fo As New OpenFileDialog
- fo.Filter = "Text Files|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If (fo.FileName = Nothing) Then
- MsgBox("No file selected.")
- Else
- path = fo.FileName
- Using sr As New StreamReader(fo.FileName)
- RichTextBox1.Text = sr.ReadToEnd()
- End Using
- End If
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- If (Not path = Nothing) Then
- Using sw As New StreamWriter(path)
- sw.Write(RichTextBox1.Text)
- End Using
- End If
- End Sub
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- Dim fs As New SaveFileDialog
- fs.Filter = "Text Files|*.txt"
- fs.FilterIndex = 1
- fs.ShowDialog()
- If (Not fs.FileName = Nothing) Then
- Using sw As New StreamWriter(fs.FileName)
- sw.Write(RichTextBox1.Text)
- End Using
- End If
- End Sub
- 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
- 1764 views