You are on page 1of 10

Dictionary - Use Text File as Database VB 2008

This tutorial will show you how to make a dictionary:


The dictionary will allow the user to enter an English word, and get the definition
in English.
Open Visual Basic 2008 Express Edition. Click on Project next to Create:

Click on Windows Form Application, and name the project "Your name's
Dictionary" and then click Ok.

Change the text property of the form to "your name's dictionary".


Add a label, textbox and a button to the form. Change the text of the label to Word,
and change the text of the button to Search.

Add another text box to the form and change it's property to Multiline:

Resize the textbox you just added and add a label above it. Change the label text to
Definition:

Add a listbox to the form and place it like the picture below:

The dictionary will show the English words once it launches.


We will add a code to fill up the list box with English words.
Double click on the form and add the following highlighted code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Lo
ad
ListBox1.Items.Add("aardvark")
ListBox1.Items.Add("aardwolf")
ListBox1.Items.Add("aaron")
ListBox1.Items.Add("aback")
ListBox1.Items.Add("abacus")
ListBox1.Items.Add("abaft")
ListBox1.Items.Add("abalone")

ListBox1.Items.Add("abandon")
ListBox1.Items.Add("abandoned")
ListBox1.Items.Add("abandonment")
ListBox1.Items.Add("abandons")
ListBox1.Items.Add("abase")
ListBox1.Items.Add("abased")
ListBox1.Items.Add("abasement")
ListBox1.Items.Add("abash")
ListBox1.Items.Add("abashed")
ListBox1.Items.Add("abate")
ListBox1.Items.Add("abated")
ListBox1.Items.Add("abatement")
End Sub

Notice how the English words are inserted into the code. You can add all English
words to the dictionary.
Adding the list of English words to the dictionary will take a lot of time, specially
that you have to insert every single word in the codes. It would be easier if we use
a database to store the words and retrieve them. But think about it this way: if you
are programming a dictionary, you know exactly what words to add and what
definitions, so the user will not update words and add definitions and that's why we
do not need a database.
We want to make sure the words are listed in alphabetical order. To do that, add
the following code between the last English word inside the code and End sub:
ListBox1.Items.Add("abatement")
ListBox1.Sorted = True
End Sub

We will add a code that when you start typing in the search box, the listbox will
highlight or select a matching word.
Double click on the textbox and add the following highlighted code:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles


TextBox1.TextChanged
Dim Item As String = TextBox1.Text.ToString()
Dim index As Integer = ListBox1.FindString(Item)
If index = -1 Then
ListBox1.SelectedIndex = ListBox1.SelectedIndex
Else
ListBox1.SetSelected(index, True)
End If
End Sub

When you type a word in the search box, the word will highlight in the listbox
Let's program the Search button, so when the user types a word an clicks the
button, they get the definition in the big text box.
Let's say the user wants the definition of the word ABACUS, so they types the
word abacus in the search box and they hit the search button. They suppose to get
the following definition:
A manual computing device consisting of a frame holding parallel rods strung with
movable counters.

Double click on the search button and add the following highlighted code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton1.C
lick
If TextBox1.Text = "abacus" Then
TextBox2.Text = "A manual computing device consisting of a frame holding parallel rods strung with movable
counters."
ElseIf TextBox1.Text = "abash" Then
TextBox2.Text = "To make ashamed or uneasy; disconcert."
Else
'A message box will show up if the entered word is not found
MsgBox("No mathcing results where found")

End If
End Sub

You can add as many definitions as you can to the dictionary.


We will make the search button also accepts the Enter key as a trigger to the search
button:
Right click on the form and click on properties:
Change AcceptButton property to Button1 as follows:

You might also add a feature to the dictionary that when the user double clicks on
a word in the listbox, the definition will show for the selected word. To do that: on
the designer page, single click on listbox1, change the properties window to events
like follows:

Look for DoubleClick event, and double click on the empty bar next to it:

Add the following highlighted code:


Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles L
istBox1.DoubleClick
TextBox1.Text = ListBox1.SelectedItem
Button1.PerformClick()
End Sub

Test the dictionary project and it should work fine.

You might also like