Program No 3:
RICH TEXT BOX
Aim:
To write a program using rich text box control using vb.net.
Procedure:
Step 1: Open Visual Studio.
Step 2: Create a new Windows Forms App (.NET Framework) project.
Step 3: Drag and drop a RichTextBox, MenuStrip, and a ToolStrip onto the form.
Step 4: Add the following code to handle basic functionalities.
Step 5:
Form Design
1. Add a RichTextBox to the form and name it RichTextBox1.
2. Add a MenuStrip to the form and configure the following menu items:
o File
▪ Open
▪ Save
▪ Exit
o Edit
▪ Clear
o Format
▪ Font
▪ Color
3. Link the menu items to the respective event handlers in the code above.
Features
Open Files: Opens text (.txt) and rich-text files (.rtf).
Save Files: Saves text or rich-text files.
Text Formatting: Change font and color of the selected text.
Clear Text: Clears all text in the editor.
Exit: Closes the application.
Program:
Imports System.IO
Public Class Form1
' Load Form
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Rich Text Editor"
Me.WindowState = FormWindowState.Maximized
End Sub
' Open File
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
OpenToolStripMenuItem.Click
Dim openFileDialog As New OpenFileDialog()
openFileDialog.Filter = "Text Files (*.txt)|*.txt|Rich Text Files (*.rtf)|*.rtf|All Files (*.*)|*.*"
If openFileDialog.ShowDialog() = DialogResult.OK Then
Dim filePath As String = openFileDialog.FileName
If Path.GetExtension(filePath).ToLower() = ".rtf" Then
RichTextBox1.LoadFile(filePath, RichTextBoxStreamType.RichText)
Else
RichTextBox1.Text = File.ReadAllText(filePath)
End If
End If
End Sub
' Save File
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
SaveToolStripMenuItem.Click
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "Text Files (*.txt)|*.txt|Rich Text Files (*.rtf)|*.rtf"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
Dim filePath As String = saveFileDialog.FileName
If Path.GetExtension(filePath).ToLower() = ".rtf" Then
RichTextBox1.SaveFile(filePath, RichTextBoxStreamType.RichText)
Else
File.WriteAllText(filePath, RichTextBox1.Text)
End If
End If
End Sub
' Change Font
Private Sub FontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
FontToolStripMenuItem.Click
Dim fontDialog As New FontDialog()
If fontDialog.ShowDialog() = DialogResult.OK Then
RichTextBox1.SelectionFont = fontDialog.Font
End If
End Sub
' Change Color
Private Sub ColorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
ColorToolStripMenuItem.Click
Dim colorDialog As New ColorDialog()
If colorDialog.ShowDialog() = DialogResult.OK Then
RichTextBox1.SelectionColor = colorDialog.Color
End If
End Sub
' Clear All
Private Sub ClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
ClearToolStripMenuItem.Click
RichTextBox1.Clear()
End Sub
' Exit Application
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
ExitToolStripMenuItem.Click
Me.Close()
End Sub
End Class
Result:
The program using Rich text box was successfully done using vb.net.