Introduction
In this tip, we will understand how to design and make a non-English editor. As the sample code project, an Urdu Editor Demo has been provided. Initially, it may seems that what, how to start. I will try to explain simple steps to make you understand. There may exist other methods of doing that same task and this is one of them.
Description to Unicode
With .NET Platform providing built-in support for Unicode, it has now become pretty easy to make controls in languages other than English. Here is a sample Webpage where Unicode can be seen easily.
Go to this link: http://unicode-table.com/en/ to get familiar with UNICODES.
How to start?
Let's define some steps:
- Go to the above link to get familiar with UNICODES
- Surf or go through my code.
- If you find any difficulty, then feel free to ask any related question.
Code Description
The idea is pretty easy. As I have, you have to do the following:
Firstly, you should know the format of your language whose editor you are building. Here I am describing URDU Editor. So I enabled the Right-to-Left property of the textbox control (one can use RichTextBox
too). This ensures that the text entered into our textbox
is right aligned. See the code listing below:
public UrduTextEditorDemo()
{
InitializeComponent();
this.RightToLeft = RightToLeft.Yes;
}
Next, intercept the textbox's keydown
and keypress
events. This is done to prevent the textbox
from accepting English text and replaces the English letter with a corresponding Urdu letter. See the code snippet below:
protected override void OnKeyPress(KeyPressEventArgs e)
{
e.Handled=handled;
if(!handled)
base.OnKeyPress (e);
}
If you look inside the Form1.cs or UrduTextBox.cs file, you will find a boolean variable called handled
. This flag is set when we have handled a key, for instance, any of the alphanumeric keys. This way, we only intercept alphanumeric keys and the rest of the keystrokes are given to the parent class to handle itself. Here is the keydown
handler code for you as it is pretty lengthy, I have omitted it deliberately:
protected override void OnKeyDown(KeyEventArgs e)
{
textBox2.HideSelection = false;
if (e.Alt && e.KeyCode == Keys.D1)
{
handled = true;
if (textBox2.SelectedText.Length > 0)
textBox2.SelectedText = textBox2.SelectedText.Replace
(textBox2.SelectedText, " (" + textBox2.SelectedText + ") ");
}
handled = (e.KeyCode== Keys.Space || e.KeyCode == Keys.Oemcomma ||
e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemQuestion ||
e.KeyCode == Keys.OemPipe || e.KeyCode == Keys.OemBackslash ||
e.KeyCode == Keys.OemSemicolon || e.KeyCode == Keys.OemQuotes ||
e.KeyCode == Keys.OemOpenBrackets ||
e.KeyCode == Keys.OemCloseBrackets ) ||
(e.KeyCode >= Keys.D0 && e.KeyCode<=Keys.D9) ||
(e.KeyCode>= Keys.A && e.KeyCode<= Keys.Z);
StringBuilder sb = new StringBuilder(this.Text);
switch(e.KeyCode)
{
case Keys.D0:
sb.Append("\u0660");
break;
case Keys.D1:
sb.Append("\u0661");
break;
case Keys.D2:
sb.Append("\u0662");
break;
...
}
this.Text = sb.ToString();
if (e.KeyCode != Keys.Back)
{
this.textBox2.SelectionStart = this.textBox2.Text.Length;
textBox2.ScrollToCaret();
}
}
The keydown
handler goes through every keycode
. It checks if we are handling it and then it replaces the text in the Text
property with the Urdu Unicode equivalent for that English keycode. As in the keypress
handler, we have set the handled property to true
. This event is not given to the parent and thus no English characters are written in the TextBox
. :)
Summary
At the end, let's summarize it. What we have done in here is simply replaced the English keyboard characters with the Urdu letters above. For example, the English letter "A
" is replaced with "alif
." This is well decent for explaining the idea. However, it is not a good approach. Instead, the user should provide a Keymapping scheme that can be changed from the designer. This way, a wide range of keyboards can work with this TextBox.
About Me
I am Hammad Zahid. Student at PUCIT. Doing Research Final Year Project "Technology for Research" in .NET.