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

Text Editor using VB.NET

  • 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

 
 
  1. Public Class frmEditor
  2. ‘ Variables for setting the font style (Bold, Italics,Underline)
  3. Dim b, i, u As Byte
  4. Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
  5. ‘ enables the textbox for entering new text
  6. txtText.Enabled = True
  7. End Sub
  8. Private Sub mnuClose_Click_1(sender As Object, e As EventArgs) Handles mnuClose.Click
  9. ‘ disables the textbox to inhibit entering text
  10. txtText.Enabled = False
  11. End Sub
  12. Private Sub MnuClear_Click(sender As Object, e As EventArgs) Handles MnuClear.Click
  13. ‘ Clear the textbox for entering new text
  14. txtText.Clear()
  15. End Sub
  16. Private Sub chkBold_CheckedChanged(sender As Object, e As EventArgs) Handles chkBold.CheckedChanged
  17. ‘ sets variable b= 1 if the chkBold checkox is checked
  18. If chkBold.Checked = True Then
  19. b = 1
  20. Else
  21. b = 0
  22. End If
  23. ‘ user defined function fontSet is called by passing values of b.i and u variables
  24. fontSet(b, i, u)
  25. End Sub
  26. Private Sub chkItalics_CheckedChanged(sender As Object, e As EventArgs) Handles chkItalics.CheckedChanged
  27. ‘ sets variable i= 2 if the chkItalics checkox is checked
  28. If chkItalics.Checked = True Then
  29. i = 2
  30. Else
  31. i = 0
  32. End If
  33. ‘ user defined function fontSet is called by passing values of b.i and u variables
  34. fontSet(b, i, u)
  35. End Sub
  36. Private Sub chkUnderline_CheckedChanged(sender As Object, e As EventArgs) Handles chkUnderline.CheckedChanged
  37. ‘ sets variable i= 4 if the chkUnderline checkox is checked
  38. If chkUnderline.Checked = True Then
  39. u = 4
  40. Else
  41. u = 0
  42. End If
  43. ‘ user defined function fontSet is called by passing values of b.i and u variables
  44. fontSet(b, i, u)
  45. End Sub
  46. Private Sub frmEditor_Load(sender As Object, e As EventArgs) Handles Me.Load
  47. ‘ Variables b.i and u initialized with value 0
  48. b = 0
  49. i = 0
  50. u = 0
  51. ‘ Size combo current size value initialized to 10
  52. cboSize.Text = 10
  53. ‘ cboFont is filled with the fonts available in the system
  54. For Each fnt In FontFamily.Families
  55. cboFont.Items.Add(fnt.Name)
  56. Next
  57. End Sub
  58. Private Sub mnuCut_Click(sender As Object, e As EventArgs) Handles mnuCut.Click
  59. ‘ selected text is cut
  60. If txtText.SelectionLength <> 0 Then
  61. txtText.Cut()
  62. End If
  63. End Sub
  64. Private Sub mnuCopy_Click(sender As Object, e As EventArgs) Handles mnuCopy.Click
  65. ‘ selected text is copied
  66. If txtText.SelectionLength <> 0 Then
  67. txtText.Copy()
  68. End If
  69. End Sub
  70. Private Sub mnuPaste_Click(sender As Object, e As EventArgs) Handles mnuPaste.Click
  71. ‘ selected text is pasted at cursor location in txtText
  72. txtText.Paste()
  73. End Sub
  74. Private Sub mnuCenter_Click(sender As Object, e As EventArgs) Handles mnuCenter.Click
  75. ‘ text is aligned center
  76. txtText.TextAlign = HorizontalAlignment.Center
  77. End Sub
  78. Private Sub mnuRight_Click(sender As Object, e As EventArgs) Handles mnuRight.Click
  79. ‘ text is aligned Right
  80. txtText.TextAlign = HorizontalAlignment.Right
  81. End Sub
  82. Private Sub mnuLeft_Click(sender As Object, e As EventArgs) Handles mnuLeft.Click
  83. ‘ text is aligned Left
  84. txtText.TextAlign = HorizontalAlignment.Left
  85. End Sub
  86. Private Sub btnBackClr_Click(sender As Object, e As EventArgs) Handles btnBackClr.Click
  87. ‘ color dialog box appears to prompt user to select desired back color for textText backcolor
  88. clrdSelect.ShowDialog()
  89. ‘ color selected by user is set to the backcolor property of textText
  90. txtText.BackColor = clrdSelect.Color
  91. End Sub
  92. Private Sub cboSize_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboSize.SelectedValueChanged
  93. Dim sz As Single
  94. ‘size selected from cboSize and converted to datatype single
  95. sz = CSng(cboSize.Text)
  96. ‘ user defined function fontSet is called by to set the changes in text
  97. fontSet(b, i, u)
  98. End Sub
  99. Private Sub fontSet(fb As Integer, fi As Integer, fu As Integer)
  100. Dim total As Integer
  101. Dim sz As Single
  102. sz = CSng(cboSize.Text)
  103. ‘ The b,i and u variables passed on by calling fontSet function are added together to set the fontstyle
  104. total = fb + fi + fu
  105. Select Case total
  106. Case 1
  107. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold)
  108. Case 2
  109. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Italic)
  110. Case 3
  111. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold Or FontStyle.Italic)
  112. Case 4
  113. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Underline)
  114. Case 5
  115. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold Or FontStyle.Underline)
  116. Case 6
  117. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Underline Or FontStyle.Italic)
  118. Case 7
  119. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Bold Or FontStyle.Underline Or FontStyle.Italic)
  120. Case Else
  121. txtText.Font = New Font(txtText.Font.FontFamily, sz, FontStyle.Regular)
  122. End Select
  123. End Sub
  124. Private Sub btnForeClr_Click(sender As Object, e As EventArgs) Handles btnForeClr.Click
  125. ‘ color dialog box appears to prompt user to select desired fore color for textText forecolor
  126. clrdSelect.ShowDialog()
  127. ‘ color selected by user is set to the forecolor property of textText
  128. txtText.ForeColor = clrdSelect.Color
  129. End Sub
  130. Private Sub cboFont_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboFont.SelectedValueChanged
  131. ‘ fontfamily of the text in txtText is changed
  132. Dim fnt As FontFamily
  133. Dim sz As Single
  134. sz = CSng(cboSize.Text)
  135. ‘ fnt stores the font family selected from the combobox cboFont
  136. fnt = New FontFamily(cboFont.Text)
  137. txtText.Font = New Font(fnt, sz)
  138. End Sub
  139. Private Sub mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
  140. Me.Close()
  141. End Sub
  142. 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.