UrduTextBox Control for C#






3.42/5 (8 votes)
Jun 14, 2007
2 min read

58166

2301
Shows how you can customize a textbox control to accept right-to-left languages like Urdu
Introduction
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.
Description
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:
public UrduTextBox()
{
// This call is required by the Windows.Forms Form Designer.
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)
{
//Move the caret to the end of text
this.SelectionStart = this.Text.Length;
e.Handled=handled;
//We handle only the required keys checked in the key down event
//the rest are passed to the parent
if(!handled)
base.OnKeyPress (e);
}
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)
{
//Set the handled flag only if we are handling a keystroke
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);
//Get the text from our textbox and store it in a string builder
StringBuilder sb = new StringBuilder(this.Text);
//Append appropriate letter to our textbox based on the key pressed
switch(e.KeyCode)
{
case Keys.D0:
sb.Append("\u0660");
break;
case Keys.D1:
sb.Append("\u0661");
break;
case Keys.D2:
sb.Append("\u0662");
break;
...
}
//Set the text to our textbox from the string builder
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...
e.Handled=handled;
...from the OnKeyPress
handler and see what I mean. ;-)
Implementation details
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.
History
- June 9 2007 - Initial release