Friday, August 12, 2011

C Sharp II

Lets learn some more C-Sharp:


Adding few more lines to the previous program 


http://apiriyasakan.blogspot.com/2011/08/c-sharp.html


we can turn it to a mini speller:



using System;
using System.Windows.Forms;
using System.Drawing;


public class dataGrid:Form {


   public static void Main() {
      Application.Run(new dataGrid());
      Application.Exit();
   } 


   DataGridView letterGrid = new DataGridView();
   DataGridView spellGrid = new DataGridView();
   TextBox textBox = new TextBox();
   TextBox spellBox = new TextBox();
   Button buttonSpell = new Button();
   String[] uCodeValues = {
      " ","அ","ஆ","இ","ஈ","உ","ஊ","எ","ஏ","ஐ","ஒ","ஓ","ஔ",
      "க்","க","கா","கி","கீ","கு","கூ","கெ","கே","கை","கொ","கோ","கௌ",
      "ங்","ங","ஙா","ஙி","ஙீ","ஙு","ஙூ","ஙெ","ஙே","ஙை","ஙொ","ஙோ","ஙௌ",
      "ச்","ச","சா","சி","சீ","சு","சூ","செ","சே","சை","சொ","சோ","சௌ",
      "ஞ்","ஞ","ஞா","ஞி","ஞீ","ஞு","ஞூ","ஞெ","ஞே","ஞை","ஞொ","ஞோ","ஞௌ",
      "ட்","ட","டா","டி","டீ","டு","டூ","டெ","டே","டை","டொ","டோ","டௌ",
      "ண்","ண","ணா","ணி","ணீ","ணு","ணூ","ணெ","ணே","ணை","ணொ","ணோ","ணௌ",
      "த்","த","தா","தி","தீ","து","தூ","தெ","தே","தை","தொ","தோ","தௌ",
      "ந்","ந","நா","நி","நீ","நு","நூ","நெ","நே","நை","நொ","நோ","நௌ",
      "ப்","ப","பா","பி","பீ","பு","பூ","பெ","பே","பை","பொ","போ","பௌ",
      "ம்","ம","மா","மி","மீ","மு","மூ","மெ","மே","மை","மொ","மோ","மௌ",
      "ய்","ய","யா","யி","யீ","யு","யூ","யெ","யே","யை","யொ","யோ","யௌ",
      "ர்","ர","ரா","ரி","ரீ","ரு","ரூ","ரெ","ரே","ரை","ரொ","ரோ","ரௌ",
      "ல்","ல","லா","லி","லீ","லு","லூ","லெ","லே","லை","லொ","லோ","லௌ",
      "வ்","வ","வா","வி","வீ","வு","வூ","வெ","வே","வை","வொ","வோ","வௌ",
      "ழ்","ழ","ழா","ழி","ழீ","ழு","ழூ","ழெ","ழே","ழை","ழொ","ழோ","ழௌ",
      "ள்","ள","ளா","ளி","ளீ","ளு","ளூ","ளெ","ளே","ளை","ளொ","ளோ","ளௌ",
      "ற்","ற","றா","றி","றீ","று","றூ","றெ","றே","றை","றொ","றோ","றௌ",
      "ன்","ன","னா","னி","னீ","னு","னூ","னெ","னே","னை","னொ","னோ","னௌ",
      "ஜ்","ஜ","ஜா","ஜி","ஜீ","ஜு","ஜூ","ஜெ","ஜே","ஜை","ஜொ","ஜோ","ஜௌ",
      "ஷ்","ஷ","ஷா","ஷி","ஷீ","ஷு","ஷூ","ஷெ","ஷே","ஷை","ஷொ","ஷோ","ஷௌ",
      "ஹ்","ஹ","ஹா","ஹி","ஹீ","ஹு","ஹூ","ஹெ","ஹே","ஹை","ஹொ","ஹோ","ஹௌ",
      "ஸ்","ஸ","ஸா","ஸி","ஸீ","ஸு","ஸூ","ஸெ","ஸே","ஸை","ஸோ","ஸோ","ஸௌ",
      "ஃ",""+(char)3021,""+(char)3006,""+(char)3007,""+(char)3008,
     ""+(char)3009,""+(char)3010,""+(char)3014,""+(char)3015,
     ""+(char)3016,""+(char)3018,""+(char)3019,""+(char)3020,"ஸ்ரீ",
     ",",".",";",":","?","!","(",")","-"," "," "," ",
     "0","1","2","3","4","5","6","7","8","9"," "," ","\r\n" 
   };


   String[] spellValues = {
      "","a","aa","i","ee","u","oo","e","ae","ai","o","oa","ow",
      "k","ka","kaa","ki","kee","ku","koo","ke","kae","kai","ko","koa","kow",
      "nk","nka","nkaa","nki","nkee","nku","nkoo","nke","nkae","nkai","nko","nkoa","nkow",
      "ch","cha","chaa","chi","chee","chu","choo","che","chae","chai","cho","choa","chow",
      "nj","nja","njaa","nji","njee","nju","njoo","nje","njae","njai","njo","njoa","njow",
      "d","da","daa","di","dee","du","doo","de","dae","dai","do","doa","dow",
      "nn","nna","nnaa","nni","nnee","nnu","nnoo","nne","nnae","nnai","nno","nnoa","nnow",
      "dh","dha","dhaa","dhi","dhee","dhu","dhoo","dhe","dhae","dhai","dho","dhoa","dhow",
      "ndh","Na","Naa","Ni","Nee","Nu","Noo","Ne","Nae","Nai","No","Noa","Now",
      "b","ba","baa","bi","bee","bu","boo","be","bae","bai","bo","boa","bow",
      "m","ma","maa","mi","mee","mu","moo","me","mae","mai","mo","moa","mow",
      "y","ya","yaa","yi","yee","yu","yoo","ye","yae","yai","yo","yoa","yow",
      "r","ra","raa","ri","ree","ru","roo","re","rae","rai","ro","roa","row",
      "l","la","laa","li","lee","lu","loo","le","lae","lai","lo","loa","low",
      "v","va","vaa","vi","vee","vu","voo","ve","vae","vai","vo","voa","vow",
      "zh","zha","zhaa","zhi","zhee","zhu","zhoo","zhae","zhaae","zhai","zho","zhoa","zhow",
      "L","La","Laa","Li","Lee","Lu","Loo","Le","Lae","Lai","Lo","Loa","Low",
      "dr","dra","draa","dri","dree","dru","droo","dre","drae","drai","dro","droa","drow",
      "n","na","naa","ni","nee","nu","noo","ne","nae","nai","no","noa","now",
      "j","ja","jaa","ji","jee","ju","joo","je","jae","jai","jo","joa","jow",
      "Sh","Sha","Shaa","Shi","Shee","Shu","Shoo","She","Shae","Shai","Sho","Shoa","Show",
      "h","ha","haa","hi","hee","hu","hoo","ha","hae","hai","ho","hoa","how",
      "S","Sa","Saa","Si","See","Su","Soo","Se","Sae","Sai","So","Soa","Sow",
      "ak",""+(char)3021,""+(char)3006,""+(char)3007,""+(char)3008,
      ""+(char)3009,""+(char)3010,""+(char)3014,""+(char)3015,
      ""+(char)3016,""+(char)3018,""+(char)3019,""+(char)3020,"Sri",
      ",",".",";",":","?","!","(",")","-","","t = d","p = b",
      "0","1","2","3","4","5","6","7","8","9"," " ,"g = k ","s = ch"
   };
      
   public dataGrid() {           


      int nCol = 26, nRow = 13;
      int cellHt = 20, cellWdth = 35;


      this.Text = "தமிழ் எழுத்துக்கள்";
      this.Size = new Size(nCol*cellWdth+30,(nRow+7)*cellHt+40+25);
      this.Location = new Point(100,50);
      this.StartPosition = FormStartPosition.Manual;
      this.FormBorderStyle = FormBorderStyle.Sizable;
      //this.CancelButton = buttonExit;
      this.MaximizeBox = false;
      this.MinimizeBox = false;


      textBox.Size = new Size(15*cellWdth,5*cellHt);
      textBox.Location = new Point((int)(1.75*cellWdth),cellHt);
      textBox.AcceptsReturn = false;
      textBox.WordWrap = true;
      textBox.Multiline = true; 
      textBox.ScrollBars = ScrollBars.Vertical;
      textBox.KeyPress += new KeyPressEventHandler(spellBox_keyPress);


      spellBox.Size = new Size(15*cellWdth,cellHt);
      spellBox.Location = new Point((int)(1.75*cellWdth),(int)(6.0*cellHt)+5);
      spellBox.KeyPress += new KeyPressEventHandler(spellBox_keyPress);
      
      buttonSpell.Text = "Spell Lookup";
      buttonSpell.Size = new Size(3*cellWdth,(int)(1.5*cellHt));
      buttonSpell.Location = new Point(17*cellWdth,6*cellHt);
      buttonSpell.Click += new EventHandler(buttonSpell_click);


      createGrid(letterGrid,nCol,nRow,cellHt,cellWdth,uCodeValues);
      letterGrid.Click += new EventHandler(letterGrid_click);


      createGrid(spellGrid,nCol,nRow,cellHt,cellWdth,spellValues);
      spellGrid.Visible = false;


      this.Controls.Add(textBox);
      this.Controls.Add(spellBox);
      this.Controls.Add(buttonSpell);
      this.Controls.Add(letterGrid);
      this.Controls.Add(spellGrid);
      this.Show();
      spellBox.Focus();


   }


   private void createGrid(DataGridView gridControl, int nCol, int nRow, 
                           int cellHt, int cellWdth, String[] valuesControl) {
      
      int i,j;
      gridControl.ColumnCount = nCol;
      gridControl.RowCount = nRow;
      gridControl.ColumnHeadersVisible = false;
      gridControl.RowHeadersVisible = false;
      for (j=0; j<gridControl.ColumnCount;j++) {
         gridControl.Columns[j].Width = cellWdth;
         gridControl.Columns[j].ReadOnly = true;
         gridControl.Columns[j].Resizable = DataGridViewTriState.False;
         for (i=0; i<gridControl.RowCount;i++) {
            if (j==0) gridControl.Rows[i].Height = cellHt;
            if (j==0) gridControl.Rows[i].ReadOnly = true;
            if (j==0) gridControl.Rows[i].Resizable = DataGridViewTriState.False;
            if (j*nRow+i < valuesControl.Length) 
                    gridControl[j,i].Value = valuesControl[j*nRow+i];
         }
      }
      gridControl.Size = new Size(nCol*cellWdth+3,nRow*cellHt+3);
      gridControl.Location = new Point(5,this.Height-gridControl.Height-43);
      
   }   


   private void spellBox_keyPress(object sender, KeyPressEventArgs e) {
      
      int i;      
      if (spellBox.Text.Length == 0) { 
         if (e.KeyChar == (char)Keys.Enter) {
            textBox.AppendText("\r\n");
            e.Handled = true;
         }
         if (e.KeyChar == (char)Keys.Back) {
            if (textBox.Text.Length > 0) {
               textBox.Select(textBox.Text.Length-1,1);
               textBox.Cut();
               e.Handled = true;
            }
         }
      }   


      if (e.KeyChar == 't') e.KeyChar = 'd'; 
      if (e.KeyChar == 'p') e.KeyChar = 'b'; 
      if (e.KeyChar == 'g') e.KeyChar = 'k'; 
      if (e.KeyChar == 's') {SendKeys.Send("ch");e.Handled = true;}


      if (spellBox.Text.Length == 0) return;
      
      if (e.KeyChar == (char)Keys.Space) { 
         for (i=0;i<spellValues.Length;i++) 
            if (spellBox.Text == spellValues[i]) break;
         if (i<spellValues.Length) {
            textBox.AppendText(uCodeValues[i]); 
            spellBox.Text = "";
         } 
         else 
            Console.Beep();
         e.Handled = true;
      }      
   }


   private void buttonSpell_click(object sender, EventArgs e) {
      if (buttonSpell.Text == "தமிழ் எழுத்து") {
         spellGrid.Visible = false;
         letterGrid.Visible = true;
         buttonSpell.Text = "Spell Lookup";
      }
      else {
         letterGrid.Visible = false;
         spellGrid.Visible = true;
         buttonSpell.Text = "தமிழ் எழுத்து";
      }
   }


   private void letterGrid_click(object sender, EventArgs e) {
      textBox.AppendText((String)letterGrid.CurrentCell.Value);
   }


}