Creating a simple text editor in VB.NET is an ideal beginner project for learning how to use Windows Forms controls, manage user actions through event handlers, and read or write files using the System.IO library. By building this application, you will create a functional tool similar to a basic version of Windows Notepad. Step 1: Set Up Your Project Open Visual Studio. Click Create a new project.
Select Windows Forms App (.NET Framework) or Windows Forms App, choosing Visual Basic as the language.
Name your project (e.g., SimpleTextEditor) and click Create. Step 2: Design the User Interface (UI)
You will need to drag a few essential components from the Toolbox onto your main canvas (Form1):
MenuStrip: Drag this to the top of your form. Type &File for the first main menu item, then add drop-down sub-items for &New, &Open, &Save, and E&xit. TextBox: Drag a standard text box control onto the form.
In the Properties Window, find the MultiLine property and set it to True.
Set the ScrollBars property to Both to allow text scrolling.
Set the Dock property to Fill so the text box stretches to fit the entire window automatically. Change its (Name) property to txtEditor.
OpenFileDialog: Drag this component anywhere onto your form. It will sit in the component tray below your design window. Change its name to ofdOpen.
SaveFileDialog: Drag this component onto your form as well. Change its name to sfdSave. Step 3: Write the Code
Double-click each menu item you created to automatically generate its click event framework in the code-behind view. Replace or fill your form’s code file (Form1.vb) with the following logic:
Imports System.IO ‘ Required to read and write files on your computer Public Class Form1 ’ Action for File > New Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click ‘ Clear the text box to start a fresh document txtEditor.Clear() End Sub ’ Action for File > Open Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click ‘ Set up the file filter so the user looks for text files ofdOpen.Filter = “Text Files (.txt)|.txt|All Files (.)|.” ’ Show the dialog window; if the user picks a file and clicks OK, load it If ofdOpen.ShowDialog() = DialogResult.OK Then Try ‘ Read all the text from the selected file path and place it in our text box txtEditor.Text = File.ReadAllText(ofdOpen.FileName) System.Exception MessageBox.Show(“Error opening file: ” & ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub ’ Action for File > Save Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click ‘ Set up the file filter for saving sfdSave.Filter = “Text Files (.txt)|.txt|All Files (.)|.” sfdSave.DefaultExt = “txt” ’ Show the save dialog window; if the user clicks OK, save the file If sfdSave.ShowDialog() = DialogResult.OK Then Try ‘ Write all text currently inside the text box out to the specified file path File.WriteAllText(sfdSave.FileName, txtEditor.Text) MessageBox.Show(“File saved successfully!”, “Success”, MessageBoxButtons.OK, MessageBoxIcon.Information) System.Exception MessageBox.Show(“Error saving file: ” & ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub ’ Action for File > Exit Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click ‘ Close the entire application window Me.Close() End Sub End Class Use code with caution. Step 4: Test Your Application
Press the Start button (or F5) inside Visual Studio to build and run your software. Type some text inside your application window.
Go to File > Save to save it to your desktop as a .txt file.
Clear the screen using File > New, then try loading that exact file back into your app using File > Open.
For a step-by-step visual guide on building a similar workspace and working with text windows in VB.NET, check out this tutorial video: How to Make a Simple HTML Editor in VB.NET ProgrammingKnowledge YouTube · Feb 9, 2014 Text Editor Using VB.net
Leave a Reply