Sample Dictionary

Build a sample Dictionary application



In this C SHARP example program, you will figure out how to assemble a basic Balochi Dictonary. The key terms are English words and each of English word is made an interpretation of into Balochi dialect when the word is chosen from the rundown or when it is written into the inquiry box. The word that is written in the hunt textbox is looked through the rundown. On the off chance that it is found, the word will be chosen in the rundown and its interpretation will show in a right-hand rich content box. We utilize Microsoft Access 2007 database to store the English words and their interpretations. At the point when the lexicon shape first loads, the English words will be recover from the database document and populated into a rundown box.

dictionary main form

Presently, you understand general thought of project. Gives move to the interface a chance to design stage. To finish this stage you need to follow the steps below:

-Open Visual Studio 2008 and create a new project by going to

From Menu select File --> then select New and click on Project...
-The New Project dialogue opens.

In the New Project dialogue Box, under Project Types select Visual C#, and select Windows Form Application under Templates. In the Name box type EnglishBalochiDictionary.

new project name

-You may click Browser and choose the location to store your project files.

Click Ok. Then you will see a default form named Form1.

new form

-Right-Click on the form and select properties to display the properties window of the form.

change properties value

-On the Property Manager tab. You will need to set the

 Text property to EngBalochi and the Name property to Dic.	
-Set the form Backcolor property to Inactivecaption.
-Set the form FormBorderStyle property to FixedDialog to make the form borders unresizable.

-Expand the Toolbox on the left side to display a list of Visual Basic controls and components.

design dictionary form
-Drag and drop one ListBox on to the form.
-Rename the list box by setting its Name property to Lstterms.
-Drag and drop one Textbox on the form above the list box.

after that drag and drop one RichTextbox to the right of the list box. set the Name property of the textbox to Txtbox, and set the Name property of the richtextbox to Txtresult. The forecolor of the richtextbox is set to MenuHighlight by using the its Forecolor property. the ReadOnly property is also set to True. This will not allow users to edit the text in the richtextbox.

-To add a menu to the form, you need to drag and drop MenuScript from the Toolbox.

insert menu
-Type &File in The Type Here box and in the next Type Here type &Help.
-Type &Close in to the Type Here box under the File menu and type &About... in to the Type Here box under the Help menu.

Now we finish the interface design of our simple dictionary. Then we will move to Create Database phase.

Create a Microsoft Access 2007 database

As you definitely knew, to store the English key terms and their EnglishBalochi interpretations, we require a Microsoft Access 2007 database. Presently lets make a database called "data.accdb" in Microsoft Access 2007 and spare it in the "canister/investigate" index of your undertaking.
This database has one table called Tblterms that has just two fields- - Enterm and Balochi.

The sample data is entered to each field as shown below:

database table

After you make a database and enter some example information, you can move to Create association and populate information in the rundown stage.

The code beneath will help you to connect with the interface database document and recover information to put in the Lstterm listbox:

- Double-click on the structure form to open the code window

- You will see the code as demonstrated as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EnglishBalochiDictionary
{
    public partial class Dic : Form
    {
        

        public Dic()
        {
            InitializeComponent();
        }
private void Dic_Load(object sender, EventArgs e)
        {
}
}
}

You have to include the statement Using System.Data.OleDb; to the Using bunch explanations statements in the code above. So it will be come:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace EnglishBalochiDictionary
{
    public partial class Dic : Form
    {
        

        public Dic()
        {
            InitializeComponent();
        }
private void Dic_Load(object sender, EventArgs e)
        {
}
}
}

-Under the "public partial class Dic: Form" line sort the accompanying code:

        private OleDbConnection cn;
        private OleDbDataReader reader;
        private OleDbCommand com;
        private DataSet rs;
        private OleDbDataAdapter ad;

This code will declare

cn variable as the OleDbConnection class, 
reader variable as OleDbReader class, 
com variable as OleDbReader Class, 
rs variable as DatSet class, and 
ad as OleDbDataAdapter classs. 

These variables will be used to make their objects of their sorts.

- Create a strategy to associate with DataBase by writing the code as demonstrated as follows:

private void Dic_Load(object sender, EventArgs e)
        {
          
            rs = new data3DataSet();
            string constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data3.mdb";
            cn = new OleDbConnection(constring);
            cn.Open();
            ad = new OleDbDataAdapter("SELECT * FROM Tblterms ORDER By Enterm", cn);
            ad.Fill(rs, "Tblterms");
            com = new OleDbCommand("SELECT Enterm From Tblterms ORDER BY Enterm", cn);
            reader = com.ExecuteReader();
//clear list
            Lstterms.Items.Clear();
            //Lstterms.DataSource = reader;

            //clear txtresult
            Txtresult.Text = "";

            while (reader.Read())
            {
	
                Lstterms.Items.Add(reader[0].ToString());
            }
// list.Clear();
            TxtBox.Focus();
        }

The code begins by make a DataSet item to store the table got from the database and an association article to interface with the database record: to get database location to associate database take after the underneath below steps

add new data source
//connect to database
From Menu Click Data then from submenu select Add New Data Source
Menu-->Data --> Add New Data Source --> 
data source configuration wizard

Data Source Configuration Wizard appeared

Select Database then click Next
Database --> Next

Choose New Connection appearing Add Connection

add connection

From the DataSource select Microsoft Access Database File (OLE DB) and for Database record name zone click Browse Button and after that from Directory select Microsoft Access Database File that you are made some time recently.

Click Test Connection Button.

test connection

If you get information Test Connection succeeded then press Ok Button.

connection string

click on (+) connection string, appearing connection string.

Copy and Past connection string

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bin\Debug\data.accdb

rs = new DataSet();
            string constring = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=data.accdb";
            cn = new OleDbConnection(constring);
            cn.Open();

The Adapter item is made to get information from the Tblterms table and
the information is filled to the DataSet object rs by utilizing the Fill() strategy for the connector:

ad = new OleDbDataAdapter("SELECT * FROM Tblterms ORDER By Enterm", cn);
            ad.Fill(rs, "Tblterms");

The Command object com is likewise made to get information from the table and populate the Listbox:

com = new OleDbCommand("SELECT Enterm From Tblterms ORDER BY Enterm", cn);
          reader = com.ExecuteReader();
//clear list
            Lstterms.Items.Clear();

            //clear txtresult
            Txtresult.Text = "";

            while (reader.Read())
            {
              Lstterms.Items.Add(reader[0].ToString());
            }

The search TextBox is set the center focus to ensure that the cursor is dynamic in this Box for input Text.

Txtbox.Focus();

By putting the technique association connection in the structure form load event, the Listbox Lstterms will be populated by the English words:

Presently you have information filled in the Listbox Lstterms when the structure form loads. The following stage is to keep in touch with some C# code to make a workable inquiry search content box.

To make the SearchBox workable you require the accompanying code that is connected to the TextChanged occasion of the content Textbox:

private void TxtBox_TextChanged(object sender, EventArgs e)
        {
            
            int index = Lstterms.FindString(this.TxtBox.Text);
            
                if(0 <= index)
                {
                    Lstterms.SelectedIndex = index;
                  
                    
            }
	 }
	 

At the point when a client sorts a word in the content Textbox TxtBox, This word is utilized to compare with word in the rundown Listbox. On the off chance that the words are coordinated the word in the List will be chosen and its interpretation will show in the richTextbox Txtresult.
In the event that the utilization chooses from the List as opposed to writing into the pursuit Serachbox, you require another bit of code connected to the SelectedIndexChanged occasion event of the rundown Listbox:

private void Lstterms_SelectedIndexChanged(object sender, EventArgs e)
        {
           DataRow dr;
            dr = rs.Tables[0].Rows[Lstterms.SelectedIndex];
            Txtresult.Clear();   //clear result
           Txtresult.Text = dr[1].ToString().Trim();
           
            
        }
		

//On the off chance that the client clicks the list instead of writing into the search box, you require another bit of code connected to the Lstterms_MouseDown occasion of the rundown Listbox:

private void Lstterms_MouseDown(object sender, MouseEventArgs e)
        {
            DataRow dr;
            dr = rs.Tables[0].Rows[Lstterms.SelectedIndex];
            Txtresult.Clear();   //clear result
            Txtresult.Text = dr[1].ToString().Trim();
            
            TxtBox.Text = dr[0].ToString().Trim();
        }

Note: The DataRow dr is used to get data of a selected row of the table.

Make the form smarter

Presently on the off chance that you maximize the form, the things are not sufficiently keen to fit the structure form. What we need to talk in this page is to help you make a structure form that its things still stay fit with the structure form when it is resized for most maximized state. To fulfill this task you require the accompanying code that is connected to the Resize occasion event of the structure:

private void Dic_Resize(object sender, EventArgs e) 
         {
             if (this.WindowState == FormWindowState.Maximized)
             {
                 Lstterms.Height = this.Height;
                 Txtresult.Height = this.Height;
                 Txtresult.Width = this.Width;
             }
        }
		

Note: If you attempt to resize the structure form and the code doesn't carry out its job you have to include a different line of code(as demonstrated as follows) to the structure form load event:

private void Dic_Load(object sender, EventArgs e)
        {
.
.
.
.
            

            this.Resize += new EventHandler(Dic_Resize);
          

 // list.Clear();
            TxtBox.Focus();
        }
		

At the point when the structure form loads numerous objects, for example, cn, rs, advertisement, com,and reader, are likewise made in the system for utilizations and these items possess spaces in PC memory. At the point when the structure form is closed these items ought to be discharged from the memory.

private void Dic_FormClosed(object sender, EventArgs e)
        {
            cn.Close();
            cn = null;
            com = null;
            reader = null;
            ad = null;
            rs = null;
            
        }
		

You also need to add code:

   this.FormClosed += new FormClosedEventHandler(Dic_FormClosed);
   

to the form load event to make Dic_FormClosed() method callable.

private void Dic_Load(object sender, EventArgs e)
        {
.
.
.
.
            

            this.Resize += new EventHandler(Dic_Resize);
            this.FormClosed += new FormClosedEventHandler(Dic_FormClosed);
          

 // list.Clear();
            TxtBox.Focus();
        }

Something else we need to talk here is to give the user a chance to close the project when his/her clicks on the Close sub menu item structure form the File menu. that task may be done simply by attaching the code to the clickEvent of the Close sub menu item:

private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
       }

Now, let Run your program..................