With .NET Platform providing built-in support for Unicode, it has now become pretty easy to make controls in languages other than English. This article explains one method of creating a custom textbox control that can be made to accept the Urdu language. With some tweaking, you can customize it for any right-to-left language, such as Hindi or Farsi.
The idea is pretty easy. You have to do the following:
First, enable the Right-to-Left property of the textbox control. This ensures that the text entered into our textbox is right aligned. See the code listing below:
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)
this.SelectionStart = this.Text.Length;
If you look inside the 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)
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);
this.Text = sb.ToString();
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. Try removing this line...
OnKeyPress handler and see what I mean. ;-)
What I have done in my version is simply replaced the English letters with the Urdu letters that were written on my keyboard. For example, the English letter "Q" is replaced with "Qaf." This is fine 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 control. The mapping that I have done is taken from the UZT mapping PDF from Unicode.org.
Using the code
Once you compile the UrduTextBox user control, it appears in the User control panel in the Toolbox. Now simply place the control on your Form and that's it.
- June 9 2007 - Initial release