Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Urdu NotePad Demo

0.00/5 (No votes)
9 Nov 2014 1  
UNICODE Based Urdu Editor

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:

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:

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:

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