Public Class Form1
Private Sub OpenFileButton_Click(sender As Object, e As EventArgs) Handles OpenFileButton.Click
Try
' Open file dialog to select a text file
OpenFileDialog1.ShowDialog()
Dim filePath As String = OpenFileDialog1.FileName
If filePath <> "" Then
Dim fileContent As String = IO.File.ReadAllText(filePath)
TextFileRichTextBox.Text = fileContent
End If
Catch ex As Exception
MsgBox("Error opening file: " & ex.Message)
End Try
End Sub
Private Sub WordCountButton_Click(sender As Object, e As EventArgs) Handles WordCountButton.Click
' Count the number of words in the text
Dim words As String() = TextFileRichTextBox.Text.Split(New Char() {" "c, vbTab, vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
WordNumTextBox.Text = words.Length.ToString()
End Sub
Private Sub CharCountButton_Click(sender As Object, e As EventArgs) Handles CharCountButton.Click
' Count the number of characters in the text
CharNumTextBox.Text = TextFileRichTextBox.Text.Length.ToString()
End Sub
Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
' Highlight all occurrences of the searched word
Dim searchWord As String = SearchTextBox.Text
Dim startIndex As Integer = 0
TextFileRichTextBox.SelectAll()
TextFileRichTextBox.SelectionBackColor = TextFileRichTextBox.BackColor
Do
startIndex = TextFileRichTextBox.Find(searchWord, startIndex, RichTextBoxFinds.None)
If startIndex <> -1 Then
TextFileRichTextBox.Select(startIndex, searchWord.Length)
TextFileRichTextBox.SelectionBackColor = Color.Yellow
StringIndexTextBox.Text = startIndex.ToString()
startIndex += searchWord.Length
End If
Loop While startIndex <> -1
End Sub
Private Sub FindNextButton_Click(sender As Object, e As EventArgs) Handles FindNextButton.Click
' Find the next occurrence of the searched word
Dim searchWord As String = SearchTextBox.Text
Dim startIndex As Integer = TextFileRichTextBox.SelectionStart + 1
Dim index As Integer = TextFileRichTextBox.Find(searchWord, startIndex, RichTextBoxFinds.None)
If index <> -1 Then
TextFileRichTextBox.Select(index, searchWord.Length)
TextFileRichTextBox.SelectionBackColor = Color.Yellow
StringIndexTextBox.Text = index.ToString()
Else
MsgBox("No more occurrences found.")
End If
End Sub
Private Sub AnsiTextBox_TextChanged(sender As Object, e As EventArgs) Handles AnsiTextBox.TextChanged
' Display the ANSI value of the entered character
If AnsiTextBox.Text.Length > 0 Then
Dim ch As Char = AnsiTextBox.Text(0)
AnsiTextBox.Text = Asc(ch).ToString()
End If
End Sub
Private Sub ShiftStringButton_Click(sender As Object, e As EventArgs) Handles ShiftButton.Click
' Shift the string using a randomly generated key
Dim key As Integer = New Random().Next(1, 26)
KeyTextBox.Text = key.ToString()
Dim shiftedText As String = New String(TextFileRichTextBox.Text.Select(Function(c) Chr(Asc(c) + key)).ToArray())
CharacterShiftRichTextBox.Text = shiftedText
End Sub
Private Sub UnshiftStringButton_Click(sender As Object, e As EventArgs) Handles ShiftBackButton.Click
' Revert the shifted string back to the original using the key
Dim key As Integer
If Integer.TryParse(KeyTextBox.Text, key) Then
Dim originalText As String = New String(CharacterShiftRichTextBox.Text.Select(Function(c) Chr(Asc(c) - key)).ToArray())
CharacterShiftRichTextBox.Text = originalText
Else
MsgBox("Invalid shift key.")
End If
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
' Exit the application
Me.Close()
End Sub
End Class