Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / C#
Tip/Trick

Urdu NotePad Demo

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
9 Nov 2014CPOL2 min read 15K   237   6   4
UNICODE Based Urdu Editor

Image 1

Introduction

In this tip, we will understand how to design and make a non-English editor. As the sample code project, an Urdu Editor Demo has been provided. Initially, it may seems that what, how to start. I will try to explain simple steps to make you understand. There may exist other methods of doing that same task and this is one of them.

Description to Unicode

With .NET Platform providing built-in support for Unicode, it has now become pretty easy to make controls in languages other than English. Here is a sample Webpage where Unicode can be seen easily.

Go to this link: http://unicode-table.com/en/ to get familiar with UNICODES.

How to start?

Let's define some steps:

  1. Go to the above link to get familiar with UNICODES
  2. Surf or go through my code.
  3. If you find any difficulty, then feel free to ask any related question.

Code Description

The idea is pretty easy. As I have, you have to do the following:

Firstly, you should know the format of your language whose editor you are building. Here I am describing URDU Editor. So I enabled the Right-to-Left property of the textbox control (one can use RichTextBox too). This ensures that the text entered into our textbox is right aligned. See the code listing below:

C#
public UrduTextEditorDemo() 
{ 
   // 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)
{
    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 Form1.cs or 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)
{
    textBox2.HideSelection = false;

     // This my builtin Command that places ( ) around the text
     if (e.Alt && e.KeyCode == Keys.D1)
     {
       handled = true;
       if (textBox2.SelectedText.Length > 0)
        textBox2.SelectedText = textBox2.SelectedText.Replace
        (textBox2.SelectedText, " (" + textBox2.SelectedText + ") ");
      }

    //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();
   
    if (e.KeyCode != Keys.Back)
    {
     //decide curser position
     this.textBox2.SelectionStart = this.textBox2.Text.Length;
     textBox2.ScrollToCaret();
    }
}

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

Summary

At the end, let's summarize it. What we have done in here is simply replaced the English keyboard characters with the Urdu letters above. For example, the English letter "A" is replaced with "alif." This is well decent 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.

About Me

I am Hammad Zahid. Student at PUCIT. Doing Research Final Year Project "Technology for Research" in .NET.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralLink issue Pin
Llewelyn Rex10-Nov-14 3:54
Llewelyn Rex10-Nov-14 3:54 
GeneralRe: Link issue Pin
Hammad Zahid Ali Zafar14-Nov-14 6:22
Hammad Zahid Ali Zafar14-Nov-14 6:22 
GeneralRe: Link issue Pin
Hammad Zahid Ali Zafar14-Nov-14 6:25
Hammad Zahid Ali Zafar14-Nov-14 6:25 
GeneralRe: Link issue Pin
Llewelyn Rex21-Nov-14 10:54
Llewelyn Rex21-Nov-14 10:54 

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.