Click here to Skip to main content
15,884,917 members
Articles / Programming Languages / C#
Article

UrduTextBox Control for C#

Rate me:
Please Sign up or sign in to vote.
3.42/5 (8 votes)
14 Jun 20072 min read 57K   2.3K   10   15
Shows how you can customize a textbox control to accept right-to-left languages like Urdu

Screenshot - screenshot.gif

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:

C#
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:

C#
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:

C#
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...

C#
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUsing for Persian language Pin
aazam rafiee zade25-Mar-16 8:20
aazam rafiee zade25-Mar-16 8:20 
Questionplease help me to make a command Pin
Hammad Zahid Ali Zafar21-Aug-14 20:59
Hammad Zahid Ali Zafar21-Aug-14 20:59 
SuggestionType / Write Urdu / Arabic / Farsi in C# and VB.NET Pin
Naseem_Amjad7-Oct-13 6:24
Naseem_Amjad7-Oct-13 6:24 
QuestionProblem in User Control Pin
Member 863294216-Jul-12 23:46
Member 863294216-Jul-12 23:46 
GeneralDeployment problem Pin
Mohammad Shamim25-Dec-08 19:11
Mohammad Shamim25-Dec-08 19:11 
GeneralRe: Deployment problem Pin
mobeen21125-Dec-08 20:16
mobeen21125-Dec-08 20:16 
Questioni need user control Pin
skhurams9-Nov-07 2:36
skhurams9-Nov-07 2:36 
AnswerRe: i need user control Pin
mobeen2119-Nov-07 16:52
mobeen2119-Nov-07 16:52 
Generalcharacters aren't joined properly Pin
effel19796-Aug-07 9:10
effel19796-Aug-07 9:10 
QuestionWhy not just use the standard Urdu keyboard? Pin
Mihai Nita14-Jun-07 7:06
Mihai Nita14-Jun-07 7:06 
AnswerRe: Why not just use the standard Urdu keyboard? Pin
mobeen21114-Jun-07 7:41
mobeen21114-Jun-07 7:41 
Hi Mihai Nita
Thanks for your comments. I will try to answer your quries one by one.
[quote]
Basically this is not an Urdu control. All standard controls supports Urdu out of the box.
You are just messing up with the keyboard, in a non-standard way.
[/quote]
Actually the standard Urdu keyboard that Microsoft has provided is not mapped as expected for Urdu writing. There are so many letter for instance the letter 'm' should be mapped to the urdu letter 'Meem' but it isn't and is mapped to I think the urdu letter 'Yeh'.

[quote]What if I want to type an English name in that control?[/quote]
I created it for one of my projects where I did not have this requirement. If anyone else has this requirement, he/she can provide a key toggle for instance Alt+E to allow English letters in between but I think it would mess the carat movement.

[quote] - if you are not happy with it, design your own using "The Microsoft Keyboard Layout Creator"
(http://www.microsoft.com/globaldev/tools/msklc.mspx)[/quote]
I did not know about the Microsoft's Keyboard Layout Creator otherwise I would have used it thanks for spotting that out.

Once again, I am glad that you have spotted some really good things and I am sure that it will help others as well.

Regards,
Mobeen
GeneralRe: Why not just use the standard Urdu keyboard? Pin
Mihai Nita14-Jun-07 20:53
Mihai Nita14-Jun-07 20:53 
GeneralRe: Why not just use the standard Urdu keyboard? [modified] Pin
mobeen21115-Jun-07 3:34
mobeen21115-Jun-07 3:34 
GeneralRe: Why not just use the standard Urdu keyboard? Pin
Mihai Nita20-Jun-07 21:49
Mihai Nita20-Jun-07 21:49 
GeneralRe: Why not just use the standard Urdu keyboard? Pin
mobeen21120-Jun-07 23:50
mobeen21120-Jun-07 23:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.