Click here to Skip to main content
13,042,421 members (136,861 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 14 Jun 2007

UrduTextBox Control for C#

, 14 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Shows how you can customize a textbox control to accept right-to-left languages like Urdu

Screenshot - screenshot.gif


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:

public UrduTextBox() 
   // This call is required by the Windows.Forms Form Designer. 
   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;

    //We handle only the required keys checked in the key down event
    //the rest are passed to the parent
        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
        case Keys.D0:

        case Keys.D1:

        case Keys.D2:

    //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...


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

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


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


About the Author

Singapore Singapore
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionUsing for Persian language Pin
aazam rafiee zade25-Mar-16 8:20
memberaazam rafiee zade25-Mar-16 8:20 
Questionplease help me to make a command Pin
Member 1067294921-Aug-14 20:59
memberMember 1067294921-Aug-14 20:59 
SuggestionType / Write Urdu / Arabic / Farsi in C# and VB.NET Pin
Naseem_Amjad7-Oct-13 6:24
memberNaseem_Amjad7-Oct-13 6:24 
QuestionProblem in User Control Pin
Member 863294216-Jul-12 23:46
memberMember 863294216-Jul-12 23:46 
GeneralDeployment problem Pin
Mohammad Shamim25-Dec-08 19:11
memberMohammad Shamim25-Dec-08 19:11 
GeneralRe: Deployment problem Pin
mobeen21125-Dec-08 20:16
membermobeen21125-Dec-08 20:16 
Questioni need user control Pin
skhurams9-Nov-07 2:36
memberskhurams9-Nov-07 2:36 
AnswerRe: i need user control Pin
mobeen2119-Nov-07 16:52
membermobeen2119-Nov-07 16:52 
Generalcharacters aren't joined properly Pin
effel19796-Aug-07 9:10
membereffel19796-Aug-07 9:10 
QuestionWhy not just use the standard Urdu keyboard? Pin
Mihai Nita14-Jun-07 7:06
memberMihai Nita14-Jun-07 7:06 
AnswerRe: Why not just use the standard Urdu keyboard? Pin
mobeen21114-Jun-07 7:41
membermobeen21114-Jun-07 7:41 
GeneralRe: Why not just use the standard Urdu keyboard? Pin
Mihai Nita14-Jun-07 20:53
memberMihai Nita14-Jun-07 20:53 
GeneralRe: Why not just use the standard Urdu keyboard? [modified] Pin
mobeen21115-Jun-07 3:34
membermobeen21115-Jun-07 3:34 
GeneralRe: Why not just use the standard Urdu keyboard? Pin
Mihai Nita20-Jun-07 21:49
memberMihai Nita20-Jun-07 21:49 
GeneralRe: Why not just use the standard Urdu keyboard? Pin
mobeen21120-Jun-07 23:50
membermobeen21120-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 14 Jun 2007
Article Copyright 2007 by mobeen211
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid