Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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. Wink | ;-)

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

Share

About the Author

mobeen211

Singapore Singapore
No Biography provided

Comments and Discussions

 
Questionplease help me to make a command PinmemberMember 1067294921-Aug-14 20:59 
SuggestionType / Write Urdu / Arabic / Farsi in C# and VB.NET PinmemberNaseem_Amjad7-Oct-13 6:24 
QuestionProblem in User Control PinmemberMember 863294216-Jul-12 23:46 
GeneralDeployment problem PinmemberMohammad Shamim25-Dec-08 19:11 
GeneralRe: Deployment problem Pinmembermobeen21125-Dec-08 20:16 
Questioni need user control Pinmemberskhurams9-Nov-07 2:36 
AnswerRe: i need user control Pinmembermobeen2119-Nov-07 16:52 
Generalcharacters aren't joined properly Pinmembereffel19796-Aug-07 9:10 
QuestionWhy not just use the standard Urdu keyboard? PinmemberMihai Nita14-Jun-07 7:06 
AnswerRe: Why not just use the standard Urdu keyboard? Pinmembermobeen21114-Jun-07 7:41 
GeneralRe: Why not just use the standard Urdu keyboard? PinmemberMihai Nita14-Jun-07 20:53 
GeneralRe: Why not just use the standard Urdu keyboard? [modified] Pinmembermobeen21115-Jun-07 3:34 
GeneralRe: Why not just use the standard Urdu keyboard? PinmemberMihai Nita20-Jun-07 21:49 
GeneralRe: Why not just use the standard Urdu keyboard? Pinmembermobeen21120-Jun-07 23:50 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 14 Jun 2007
Article Copyright 2007 by mobeen211
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid