Monday, March 23, 2009

Shanti C# -> English - Spanish Translator

In the class definition, you might want to declare and initialise the two global string variables

string[] sEng = {"arm","body","ear","eye","face","foot/feet","finger","hair","hand","head","leg","mouth","neck",
"nose","stomach","tooth/teeth"};
string[] sSpan = {"brazo","cuerpo","oreja","ojo","cara","pie/s","dedo","pelo","mano","cabeza","pierna","boca",
"cuello","nariz","est\u00F3mago","diente/s"};



Note the character in orange, which is the unicode that represents the spanish character.



Now, we bind the arrays to the controls (I use a combo box and a list box).

for (int i = 0; i < sEng.Length; i++)
{
cmbEng.Items.Add(sEng[i]);
lisSpan.Items.Add(sSpan[i]);
}

for (int i = 0; i < sMorse.Length; i++)
{
cmbText.Items.Add(sText[i]);
lisMorse.Items.Add(sMorse[i]);
}


Finally, we put the coding behind the index selection of the combobox and listbox respectively, so that when you click on an english term, the spanish translation is offered, and vice-versa:

private void cmbEng_SelectedIndexChanged(object sender, EventArgs e)
{
int iIndex = cmbEng.SelectedIndex;
lisSpan.Text =
sSpan[iIndex];
}

private void lisSpan_SelectedIndexChanged(object sender, EventArgs e)
{
int iIndex = lisSpan.SelectedIndex;
cmbEng.Text = sEng[iIndex];
}



That's it, compile, run and learn Spanish :)

No comments:

Post a Comment