Public Class Form1 'The class
Structure Abbr 'A structure, used to hold one abbreviation
Dim Abbreviation As String
Dim Extended As String
End Structure
Dim abbrs As List(Of Abbr) = New List(Of Abbr) 'A list of Abbr structures that holds all abbreviations
Private Function GetAbbr(ByVal abbrS As String) As Abbr 'Retrieves an Abbr object from a string by splitting it
Dim rt As Abbr 'The ReTurn value (an abbreviation object)
Dim cchr As Char 'Current CHaRacter
Dim i As Integer = 0 'Current Index
rt.Abbreviation = "" 'Set the abbreviation to "", just in case something goes wrong
While i < abbrS.Length 'While loop, loops through all chars inside passed string
cchr = abbrS.Chars(i) 'set current char
If cchr = " "c Then 'If it's a space...
rt.Extended = abbrS.Substring(i + 1, abbrS.Length) 'Set the extended version to the rest of the string
If rt.Abbreviation = "" Then Return Nothing 'If the space is the first char, return nothing
Return rt 'otherwise, return the completed abbreviation.
End If
rt.Abbreviation &= cchr 'add the cchr to the current abbreviation
i += 1 'increment i
End While
Return Nothing 'If there was no space, return Nothing (it's the error value)
End Function
Private Sub txtMain_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMain.TextChanged
'Text changed
If Me.txtMain.Text.Chars(Me.txtMain.Text.Length - 1) <> " "c Then Exit Sub 'If they didn't just type a space, exit.
Dim abbr As Abbr 'Create another abbr object
For Each abbr In abbrs 'For Each loop; get all abbreviations in the list
ReplaceAbbr(abbr) 'Call ReplaceAbbr sub to replace the abbreviation with the extended version.
Next
End Sub
Private Sub ReplaceAbbr(ByVal abbr As Abbr)
Dim original As String = Me.txtMain.Text 'The original
Me.txtMain.Text = Me.txtMain.Text.Replace(abbr.Abbreviation, abbr.Extended) 'Replace all abbreviations
If Not (Me.txtMain.Text = original) Then 'If they're different then select the end of it
Me.txtMain.Select(original.LastIndexOf(abbr.Abbreviation) + (abbr.Extended.Length - abbr.Abbreviation.Length), 0)
End If
End Sub
Private Sub LoadData()
Try
Dim fi As New IO.FileInfo("abbr_info.dat")
If Not fi.Exists Then Throw New IO.FileNotFoundException() 'If it doesn't exist, tell the user.
Dim fs As New IO.FileStream("abbr_info.dat", IO.FileMode.Open, IO.FileAccess.Read) 'Create a file stream for reading
Dim rf As New IO.StreamReader(fs) 'set up an object to read from that stream
While rf.Peek() > -1 'While there's still text in the file
Dim tmp As Abbr = GetAbbr(rf.ReadLine()) 'Get the abbreviation
If Not Object.Equals(tmp, Nothing) Then 'If there wasn't an error...
abbrs.Add(tmp) 'add the abbreviation to the list.
End If
End While
rf.Close() 'Close the streams
fs.Close()
Catch ex As Exception 'If there was an error.
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData() 'Load the data
End Sub
End Class