1/04/2015

C# Dictionary

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 KDictionary  
{
  public partial class Dic : Form
{
  private OleDbConnection cn;
  private OleDbDataReader reader;
  private OleDbCommand com;
  private DataSet rs;
  private OleDbDataAdapter ad;
      public Dic()
  {
  InitializeComponent();
  }
    private void Dic_Resize(object sender, EventArgs e)
  {
  //Adjust listbox and textbox heights when the form resizes
  //Dic dictionary = new Dic();
  if (this.WindowState == FormWindowState.Maximized)
  {
  Lstterms.Height = this.Height;  
        Txtresult.Height = this.Height;
  Txtresult.Width = this.Width ;
  }
}   
 private void Dic_Load(object sender, EventArgs e)
  {
  myconnect();
  this.Resize+= new EventHandler(Dic_Resize);
  this.FormClosed += new FormClosedEventHandler(Dic_FormClosed);
   }
 
       private void myconnect()
        {
  try
  {
   //connect to database
  rs = new DataSet();
  cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +   Application.StartupPath + "\\data.mdb;");
  cn.Open();
  ad = new OleDbDataAdapter("SELECT * FROM Tblterms ORDER BY Enterms", cn);
  ad.Fill(rs, "Tblterms");
  com = new OleDbCommand("SELECT Enterms FROM Tblterms ORDER BY Enterms", cn);
  reader = com.ExecuteReader();
  //clear list  
        Lstterms.Items.Clear();
  //clear txtresult
  Txtresult.Text = "";
  while (reader.Read())  {
  Lstterms.Items.Add(reader[0].ToString());
  } 
        } 
 catch (Exception ex)
        {
        }
  Txtbox.Focus();
       }
  private void Txtbox_TextChanged(object sender, EventArgs e)  
        {
  int i;
  DataRow dr;
  try  { 
 for (i = 0; i <= Lstterms.Items.Count - 1; i++)
  { 
  if (Txtbox.Text.Trim().ToUpper() == Lstterms.Items[i].ToString().ToUpper().Substring(0,   Txtbox.TextLength))
  { 
 //Select matched term  
        Lstterms.SelectedIndex = i;
  //Display translation  
        dr = rs.Tables[0].Rows[i];
  Txtresult.Clear();
  Txtresult.Text = dr[1].ToString().Trim();
  highlight(); 
 break;
  } 
 } 
 }  
       catch (Exception ex)
       {
       } 
      }    
       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 ();
  highlight(); 
 }  
       private void highlight()  
       {    
         try  {
   char[] c = Txtresult.Text.ToString().ToCharArray();  //coloring v.,a., and n.
  int i; 
 for (i = 1; i <= Txtresult.Text.ToString().Length - 2; i++)
  { 
 if ((c[i] == '.' && c[i - 1] == 'v') || (c[i] == '.' && c[1 - 1] == 'a') ||   (c[i] == '.' && c[i - 1] == 'n'))  
//If (((c[i] == ".") && (c[i - 1] == "v")) || ((c[i] == ".") && (c[i - 1] ==   "a")) || ((c[i] == ".") && (c[i - 1] =="n"))) 
 {
  Txtresult.SelectionStart = i - 1;
  Txtresult.SelectionLength = 2;
  Txtresult.SelectionColor = Color.Red; 
   } 
   } 
 //coloring adj., adv. 
 for (i = 3; i <= Txtresult.Text.ToString().Length - 1; i++)
  { 
if ((c[i] == '.' && c[i - 1] == 'j' && c[i - 2] == 'd') || (c[i] == '.' &&   c[i - 1] == 'g' && c[i - 2] == 'i') || (c[i] == '.' && c[i - 1] == 'v' && c[i   - 2] == 'd'))
  {  
Txtresult.SelectionStart = i - 3;
  Txtresult.SelectionLength = 4;
  Txtresult.SelectionColor = Color.Red;
  }
    }
  //coloring Khmer character
    for (i = 0; i <= Txtresult.Text.ToString().Length - 1; i++) 
 { 
   if ((int)c[i] > 127)
  {  
       //int ix = (int)c[i];  
       //MessageBox.Show("Hello"+ix);
  Txtresult.SelectionStart = i;
  Txtresult.SelectionLength = 1;
  Txtresult.SelectionColor = Color.Black; 
 } 
 } 
     } 
 catch (Exception ex) { } 
 } 
 private void closeToolStripMenuItem_Click(object sender, EventArgs e)
  { 
  Close();
   }  
  private void menuStrip1_ItemClicked(object sender,ToolStripItemClickedEventArgs e) 
 {
 
 } 
 private void Dic_FormClosed(object sender,EventArgs e) 
 {  
         cn.Close();  
         cn = null;  
         com = null;  
         reader = null;  
         ad = null;  
         rs = null; 
 } 
  }
  }