'I am working on a dictionary project for word meaning search which is written in VB10.net . it contain around 8,000 pairs of word and meaning.

'When i run, it loads perfectly but when i click on the search button it start "Not responding" for around 30 seconds. Then it works. But when tested in my friends PC it never returns from "Not responding" state.
'I WILL VERY VERY THANKFUL IF ANYBODY COULD HELP ME OUT TO OVER COME THIS "NOT RESPONDING" PROBLEM.
'Dictionary code is as follows:

'###########################################################################################

        Public Class Form1

            Private Sub Form1_Load(byVal sender As System.Object, byVal e As System.EventArgs) Handles MyBase.Load


                'a
                ListBox1.Items.Add("a")
                ListBox1.Items.Add("aba")
                ListBox1.Items.Add("abah")
                ListBox1.Items.Add("abey")
                ListBox1.Items.Add("abi")
                ListBox1.Items.Add("abie")
                ListBox1.Items.Add("abii")
                ListBox1.Items.Add("abo")





                ListBox1.Sorted = True
            End Sub

            Private Sub AboutUsToolStripMenuItem_Click(byVal sender As System.Object, byVal e As System.EventArgs) Handles AboutUsToolStripMenuItem.Click
                AboutBox1.Show()

            End Sub

            Private Sub EndToolStripMenuItem_Click(byVal sender As System.Object, byVal e As System.EventArgs) Handles EndToolStripMenuItem.Click
                End

            End Sub

            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

            Private Sub button1_Click(byVal sender As System.Object, byVal e As System.EventArgs) Handles button1.Click




                    If TextBox1.Text ="a"Then
                        TextBox2.Text = TextBox1.Text
                        TextBox3.Text ="Verb"
                        TextBox4.Text ="to come; to enter."
                ElseIf TextBox1.Text ="aba"Then
                    TextBox2.Text = TextBox1.Text
                    TextBox3.Text ="Adverb"
                    TextBox4.Text ="To come together/at the same time."
                ElseIf TextBox1.Text ="abah"Then
                    TextBox2.Text = TextBox1.Text
                    TextBox3.Text ="Adverb"
                    TextBox4.Text ="to come in addition to sb."
                ElseIf TextBox1.Text ="abey"Then
                    TextBox2.Text = TextBox1.Text
                    TextBox3.Text ="Adverb"
                    TextBox4.Text ="bored after coming regularly for a long time."
                ElseIf TextBox1.Text ="abi"Then
                    TextBox2.Text = TextBox1.Text
                    TextBox3.Text =""
                    TextBox4.Text ="to come on behalf of other."
                ElseIf TextBox1.Text ="abie"Then
                    TextBox2.Text = TextBox1.Text
                    TextBox3.Text =""
                    TextBox4.Text ="to come with pleasure."
                ElseIf TextBox1.Text ="abii"Then
                    TextBox2.Text = TextBox1.Text
                    TextBox3.Text =""
                    TextBox4.Text ="to have come."
                ElseIf TextBox1.Text ="abo"Then
                    TextBox2.Text = TextBox1.Text
                    TextBox3.Text ="Adverb"
                    TextBox4.Text ="to come along with sb. Annii ageen ho paze atu si abo mondo.(the duckling is coming with its mother)."



                Else
                    MsgBox("No Match Found!!")
                End If
            End Sub


            Private Sub ListBox1_DoubleClick(byVal sender As System.Object, byVal e As System.EventArgs)
                TextBox1.Text = ListBox1.SelectedItem
                button1.PerformClick()
            End Sub

            Private Sub TextBox2_TextChanged(byVal sender As System.Object, byVal e As System.EventArgs) Handles TextBox2.TextChanged

            End Sub

            Private Sub MenuStrip1_ItemClicked(byVal sender As System.Object, byVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

            End Sub

            Private Sub Form1_DoubleClick(byVal seNounder As System.Object, byVal e As System.EventArgs) Handles MyBase.DoubleClick

            End Sub

            Private Sub ListBox1_DoubleClick_1(byVal seNounder As System.Object, byVal e As System.EventArgs) Handles ListBox1.DoubleClick
                TextBox1.Text = ListBox1.SelectedItem
                button1.PerformClick()
            End Sub

            Private Sub SplashScreenToolStripMenuItem_Click(byVal sender As System.Object, byVal e As System.EventArgs) Handles SplashScreenToolStripMenuItem.Click
                SplashScreen1.Show()

            End Sub

            Private Sub ListBox1_SelectedIndexChanged(byVal sender As System.Object, byVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

            End Sub

            Private Sub CreditsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreditsToolStripMenuItem.Click
                Form2.Show()

            End Sub
        End Class

=======================================================================================================================

Any time you have values being loaded in to memory, you run the risk of slowing down a process.

I suggest a small database backend to store the words/meanings/sample sentences. Then perform a lookup when needed.

For example:

----------------------------------------------------------------
|__________________________tblWords____________________________|
|_____ID_____|______Word ____|______Type______|____Sentence____|
|  Identity  |  VarChar(50)  |  VarChar(100)  |  VarChar(500)  |
----------------------------------------------------------------

This will give you a lookup table, where you can store values similar to this:

0 , abo , Adverb , to come along with sb. Annii ageen ho paze atu si abo mondo.(the duckling is coming with its mother).
1 , abi ,  , to come on behalf of other.

This table will store ALL of your words/definitions.

Then to get the definition, just query the database:

Dim myCon As New OleDBConnection("MyConnectionStringHere") 'See www.ConnectionStrings.com
myCon.Open()
'Assumes words are unique in spelling in database.
Dim da As New OleDBDataAdapter("SELECT * FROM tblWords WHERE Word=@Word",myCon)
da.SelectCommand.Parameters.AddWithValue("@Word",TextBox1.Text)
Dim ds As New DataSet

Try
    da.Fill(ds,"Definitions")
    If ds.Tables("Definitions") IsNot Nothing And ds.Tables("Definitions").Rows.Count > 0 Then
        With ds.Tables("Definitions")
            TextBox2.Text = TextBox1.Text
            TextBox3.Text = .Rows(0)("Type")
            TextBox4.Text = .Rows(0)("Sentence")
        End With
    End If
Catch ex As Exception
    MsgBox(ex.ToString)
End Try

This will force the database to do most of the work, and allow your code to run smoother by removing the HUGE nested if that is there.

Edited 3 Years Ago by Begginnerdev

Comments
Thanks a lot for such a beautiful tip. Thanks again

Thanks a lot for such a beautiful tip. Thanks again. I was almost lost.
I will do as you suggested. and if i encounter any problem i am sure to get back to you.

Your first problem (and this is a biggie) is that you have included no comments with your code. It is hard to say if your code is doing what it is supposed to do when there are no comments saying what it is supposed to do.

Your second problem is that you have hard coded your entries. For the sake of developing the app, hard coding in a few entries is not a problem, but the way you have done it ensures that you have to add special code for every new entry. I suggest using a dictionary. The key would be the word, and the value could be a string array where item 0 is the part of speech and item 1 is the definition. For testing you can initialize the dictionary with a few words by

Private mydic As New SortedDictionary(Of String, String()) From {
    {"quick", {"adjective", "rapid"}},
    {"brown", {"adjective", "an earthtone colour"}},
    {"fox", {"noun", "a small carnivore"}}
}

but you would read the actual values from a database or file eventually. You can then load the listbox by

For Each word As String In mydic.Keys
    ListBox1.Items.Add(word)
Next

You wouldn't need a button. You could just select the word from the listbox by single clicking or by typing a word into textbox1. The code for the textbox1 event would be

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

    'As the user enters text into the textbox, check to see if it matches a dictionary entry.
    'If it does then display the part of speech and the definition, otherwise clear the display.

    Dim tbx As TextBox = sender

    If mydic.ContainsKey(tbx.Text) Then
        TextBox2.Text = mydic(tbx.Text).GetValue(0)
        TextBox3.Text = mydic(tbx.Text).GetValue(1)
    Else
        TextBox2.Text = ""
        TextBox3.Text = ""
    End If

End Sub

Please note the use of a comment to indicate what the Sub is supposed to do.

Comments
Thank you.
Yep!

Is it possible to store Data(Word;Type;Sentence) in textfile and to display in the Listbox and textbox.

Is it possible to store Data(Word;Type;Sentence) in textfile and to display in the Listbox and textbox.

Yes, It's possible.

Imports System.IO

    ' This will add Data as new line in txt file.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim writer As StreamWriter
        writer = File.AppendText("D:\Dictionary.txt")
        writer.WriteLine(txtWord.Text & ";" & txtType.Text & ";" & txtSentence.Text)
        writer.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If File.Exists("D:\Dictionary.txt") Then
            Dim openFileLines() As String = File.ReadAllLines("D:\Dictionary.txt")
            ListBox1.Items.AddRange(openFileLines)
        End If
    End Sub

Edited 3 Years Ago by Jx_Man

*Dear Reverend Jim,
I have re-corrected the code accordingly.
But it show the error message as follows:

Error:1. Option Strict On disallows implicit conversion from 'Object' to 'String'.
 : Replace 'sender' with 'CType(sender, TextBox)'.

Error2. Option Strict On disallows implicit conversions from 'object' to 'string'.
    : Replace 'mydic(tbx.Text).GetValue(0)' with CStr(mydic(tbx.Text).GetValue(0))

Error3. Option Strict On disallows implicit conversions from 'object' to 'string'.
    : Replace 'mydic(tbx.Text).GetValue(1)' with CStr(mydic(tbx.Text).GetValue(1))


Option Strict On
    Option Explicit On
    Imports System.Collections.Generic
    Imports System.Windows.Forms
    Public Class Form1
    Private mydic As New SortedDictionary(Of String, String()) From {
            {"quick", {"adjective", "rapid"}},
            {"brown", {"adjective", "an earthtone colour"}},
            {"fox", {"noun", "a small carnivore"}}
    }

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each word As String In mydic.Keys
            ListBox1.Items.Add(word)
        Next
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim tbx As TextBox = sender                      'Error 1
        If mydic.ContainsKey(tbx.Text) Then
                TextBox2.Text = mydic(tbx.Text).GetValue(0)  'Error2
                TextBox3.Text = mydic(tbx.Text).GetValue(1)  'Error3
        Else
                TextBox2.Text = ""
                TextBox3.Text = ""
        End If
End Sub

End Class

Dear Jx_Man

Which will be faster to load, Using Database or Textfile.
Actually, i have not yet elimanated my problem of "Not Responding.

Can i store data as following into textfile:
word | Type | Meaning
quick | Adjective | Rapid.
brown | Adjective | Earthtoned Coloured.

And data could be Searched by:
1. Typing in Textbox1.
2. clicking on listbox1.

Then displaying data as follows:
1. textbox2 will display :Word
2. textbox3 will display :Type
3. textbox4 will display :Meaning

Is it possible this way. Or you have other way out. Please help.

But it show the error message as follows:

I'm pretty careful about casting so I tend to keep Option Strict disabled so I doon't get the error messages that you were getting so just do the explicit cast like you are doing and things should be fine.

And data could be Searched by:
1. Typing in Textbox1.
2. clicking on listbox1.

Easily. My code sample above shows you how to update based on typing in the textbox. And you can trigger on SelectedIndexChanged on the listbox so the user can pick a word by clicking on it in the listbox.

List of problem I am still stuck with:

1.I have come out with a code which dont automatically display word and its meaning. But a dailog box opens where user have to manually select the targeted TextFile containing Pairs of word/meaning.
2. I want this application to display 'Type' and 'Sentence' in 2 different textbox.(Textbox2 and Textbox3 respectively)

'It will triger LoadDictionary() and LBadd()

Private Dict As New Dictionary(Of String, String)
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' for example only - would be in file normally
        'Dict.Add("word", "Type. Sentence.")
        'Dict.Add("Abacus", "Noun. Early type of computer")
        ' Dict.Add("Baby", "Noun. Early type of wrinkly")
        ' Dict.Add("Cave Painting", "Noun. Early type of TV")
        ' Dict.Add("Ama", "Noun. Mother")
        ' Dict.Add("Aba", "Noun. Father")
        ' Dict.Add("Apa", "Noun. Uncle")
        'Dict.Add("Ata", "Noun. Sister")
        ' if in text file '|' separated pairs
        LoadDictionary()
        LBadd()
    End Sub

'It will load text file data from textfile.

Private Sub LoadDictionary()
        Dim fo As New OpenFileDialog
        fo.InitialDirectory = Application.StartupPath & "\DATA\words.txt"
        Dim result As DialogResult = fo.ShowDialog()
        Dim fn As String = fo.FileName
        If Not fn = Nothing Then
            Dim line As String
            Using fr As New IO.StreamReader(fn)
                Do
                    line = fr.ReadLine()
                    If Not line = Nothing Then
                        Dim a() As String = Split(line, "|")
                        Dict.Add(a(0), a(1))
                    End If
                Loop Until line = Nothing
            End Using
        End If
    End Sub

'It will display wordlist in the listbox from textfile.

 Private Sub LBadd()
        For Each s As String In Dict.Keys
            If Not Me.ListBox1.Items.Contains(s) Then
                Me.ListBox1.Items.Add(s)
            End If
        Next
    End Sub

'It will take user input in the textbox1.

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

'it will display meaning in textbox2 if word selected in the list box.

Dim lb As ListBox = DirectCast(sender, ListBox)
        Dim s As String = lb.SelectedItem.ToString
        Me.TextBox2.Text = Dict(s)

I want the above code to automatically access the textfile file and display the desired output without promting me a dialog box to locate the targeted textfile.

Also,I want this application to display 'Type' and 'Sentence' in 2 different textbox.(Textbox2 and Textbox3 respectively)

Your time and help will be very much appriciated. Thanks.

Something like this should load a file directly:

Private Sub LoadDictionary()
    Dim fn As String = Application.StartupPath & "\DATA\words.txt"
    Dim line As String
    Dim fr As IO.StreamReader = New IO.StreamReader(fn)
    While fr.Peek >= 0
        line = fr.ReadLine()
        Dim a() As String = Split(line, "|")
        Dict.Add(a(0), a(1))
    End While
End Sub

This might work for splitting the type and sentence:

Dim lb As ListBox = DirectCast(sender, ListBox)
Dim s As String = lb.SelectedItem.ToString
dim Temp() As String = Dict(s).Split(".")
Me.TextBox2.Text = Temp(0)
Me.TextBox3.Text = Temp(1)

On a side note, you might want to consider handling the keypress event and check for the Enter key or use a button, to signal when to search. Using the textchanged event will trigger a search with every letter typed.

Also since you are using 3 data fields you might find it easier to make your dictionary accept a string array

Private Dict As New Dictionary(Of String, String())

Then adding to it and using it would be like this

Private Sub LoadDictionary()
    Dim fn As String = Application.StartupPath & "\DATA\words.txt"
    Dim line As String
    Dim fr As IO.StreamReader = New IO.StreamReader(fn)
    While fr.Peek >= 0
        line = fr.ReadLine()
        Dim a() As String = Split(line, "|")
        dim b() As String = Split(a(1),".")
        Dict.Add(a(0), b)
    End While
End Sub

Dim lb As ListBox = DirectCast(sender, ListBox)
Dim s As String = lb.SelectedItem.ToString
Me.TextBox2.Text = Dict(s)(0)
Me.TextBox3.Text = Dict(s)(1)

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.