This mini project in VB.NET is meant to understand the flexibility, capability and ease of developing robust and versatile applications in VB.NET. This simple Text Editor using VB.NET allows a user to do following to the text written in the textbox.The interface of the Text Editor is displayed in the image
- Change the font style of the text
- Change forecolor and backcolor,
- Change text size
- Change font family
- Define text alignment
- Cut, copy and paste selected text in the textbox using Edit Menu or shortcut keys.
To let the students understand the concept of short cut keys we have used shortcut keys different than the default keys for these operations as follows
- Cut (Alt+T instead of ctrl+X)
- Copy(Alt+P instead of ctrl+C)
- Paste(Alt+S instead of ctrl+V)
To create Text Editor using VB.NET the following controls are used.
- MenuStrip (prefix mnu)
- CheckBox (prefix chk)
- Label(prefix lbl)
- Combobox(prefix cbo)
- Button(prefix btn)
- Textbox(prefix txt)
- ColorDialog(prefix clrd)
MenuStrip and ColorDialog controls don’t have any graphic presence on a form while running. You can see them in the Component Tray at the bottom in Design View. The combobox for size cboSize is filled with font size values in the items collection in the property window of the combobox. The combobox for font family cboFont is filled with font face names in the form Load event using the FontFamily.Families array.
Controls used and their names in the project
Control |
Names in Project |
| MenuStrip | mnuFile
· mnuNew, mnuClose, mnuClear mnuEdit · mnuCut (shortcut keys Alt+T), mnuCopy(shortcut keys Alt+P), mnuPaste( shortcut keysAlt+S) mnuFormat · mnuLeft,mnuRight,mnuCenter mnuExit |
| CheckBox | chkBold,chkItalics,chkUnderline |
| Labels | lblSize |
| ComboBox | cboSize, cboFont |
| Button | btnBackClr, btnForeClr |
| TextBox | txtText |
| ColorDialog | clrdSelect |
Application Code for Text Editor in VB.NET
Public Class frmEditor
‘ Variables for setting the font style (Bold, Italics,Underline)
Dim b, i, u As Byte
Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
‘ enables the textbox for entering new text
txtText.Enabled = True
End Sub
Private Sub mnuClose_Click_1(sender As Object, e As EventArgs) Handles mnuClose.Click
‘ disables the textbox to inhibit entering text
txtText.Enabled = False
End Sub
Private Sub MnuClear_Click(sender As Object, e As EventArgs) Handles MnuClear.Click
‘ Clear the textbox for entering new text
txtText.Clear()
End Sub
Private Sub chkBold_CheckedChanged(sender As Object, e As EventArgs) Handles chkBold.CheckedChanged
‘ sets variable b= 1 if the chkBold checkox is checked
If chkBold.Checked = True Then
b = 1
Else
b = 0
End If
‘ user defined function fontSet is called by passing values of b.i and u variables
fontSet(b, i, u)
End Sub
Private Sub chkItalics_CheckedChanged(sender As Object, e As EventArgs) Handles chkItalics.CheckedChanged
‘ sets variable i= 2 if the chkItalics checkox is checked
If chkItalics.Checked = True Then
i = 2
Else
i = 0
End If
‘ user defined function fontSet is called by passing values of b.i and u variables
fontSet(b, i, u)
End Sub
Private Sub chkUnderline_CheckedChanged(sender As Object, e As EventArgs) Handles chkUnderline.CheckedChanged
‘ sets variable i= 4 if the chkUnderline checkox is checked
If chkUnderline.Checked = True Then
u = 4
Else
u = 0
End If
‘ user defined function fontSet is called by passing values of b.i and u variables
fontSet(b, i, u)
End Sub
Private Sub frmEditor_Load(sender As Object, e As EventArgs) Handles Me.Load
‘ Variables b.i and u initialized with value 0
b = 0
i = 0
u = 0
‘ Size combo current size value initialized to 10
cboSize.Text = 10
‘ cboFont is filled with the fonts available in the system
For Each fnt In FontFamily.Families
cboFont.Items.Add(fnt.Name)
Next
End Sub
Private Sub mnuCut_Click(sender As Object, e As EventArgs) Handles mnuCut.Click
‘ selected text is cut
If txtText.SelectionLength <> 0 Then
txtText.Cut()
End If
End Sub
Private Sub mnuCopy_Click(sender As Object, e As EventArgs) Handles mnuCopy.Click
‘ selected text is copied
If txtText.SelectionLength <> 0 Then
txtText.Copy()
End If
End Sub
Private Sub mnuPaste_Click(sender As Object, e As EventArgs) Handles mnuPaste.Click
‘ selected text is pasted at cursor location in txtText
txtText.Paste()
End Sub
Private Sub mnuCenter_Click(sender As Object, e As EventArgs) Handles mnuCenter.Click
‘ text is aligned center
txtText.TextAlign = HorizontalAlignment.Center
End Sub
Private Sub mnuRight_Click(sender As Object, e As EventArgs) Handles mnuRight.Click
‘ text is aligned Right
txtText.TextAlign = HorizontalAlignment.Right
End Sub
Private Sub mnuLeft_Click(sender As Object, e As EventArgs) Handles mnuLeft.Click
‘ text is aligned Left
txtText.TextAlign = HorizontalAlignment.Left
End Sub
Private Sub btnBackClr_Click(sender As Object, e As EventArgs) Handles btnBackClr.Click
‘ color dialog box appears to prompt user to select desired back color for textText backcolor
clrdSelect.ShowDialog()
‘ color selected by user is set to the backcolor property of textText
txtText.BackColor = clrdSelect.Color
End Sub
Private Sub cboSize_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboSize.SelectedValueChanged
Dim sz As Single
‘size selected from cboSize and converted to datatype single
sz = CSng(cboSize.Text)
‘ user defined function fontSet is called by to set the changes in text
fontSet(b, i, u)
End Sub
Private Sub fontSet(fb As Integer, fi As Integer, fu As Integer)
Dim total As Integer
Dim sz As Single
sz = CSng(cboSize.Text)
‘ The b,i and u variables passed on by calling fontSet function are added together to set the fontstyle
total = fb + fi + fu
Select Case total
Case 1
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold)
Case 2
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Italic)
Case 3
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold Or FontStyle.Italic)
Case 4
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Underline)
Case 5
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold Or FontStyle.Underline)
Case 6
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Underline Or FontStyle.Italic)
Case 7
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold Or FontStyle.Underline Or FontStyle.Italic)
Case Else
txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Regular)
End Select
End Sub
Private Sub btnForeClr_Click(sender As Object, e As EventArgs) Handles btnForeClr.Click
‘ color dialog box appears to prompt user to select desired fore color for textText forecolor
clrdSelect.ShowDialog()
‘ color selected by user is set to the forecolor property of textText
txtText.ForeColor = clrdSelect.Color
End Sub
Private Sub cboFont_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboFont.SelectedValueChanged
‘ fontfamily of the text in txtText is changed
Dim fnt As FontFamily
Dim sz As Single
sz = CSng(cboSize.Text)
‘ fnt stores the font family selected from the combobox cboFont
fnt = New FontFamily(cboFont.Text)
txtText.Font = New Font(fnt, sz)
End Sub
Private Sub mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
Me.Close()
End Sub
End Class
Final words
This is a simple text editor using VB.NET that you can create by using the code shared here. You can suggest any improvements and ask queries in the comment box below.

