Click here to Skip to main content
14,382,515 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have been using the below code to replace English numbers with Arabic numbers at run-time, in a new form I only need the numeric keypad to replace the numbers and keep everything else as it is.

How to achieve the whole idea?

private void ArabicNumbers_KeyPress(object sender, KeyPressEventArgs e)
    {
        switch (e.KeyChar)
        {
            case '0': e.KeyChar = '٠'; break;

            case '1': e.KeyChar = '١'; break;

            case '2': e.KeyChar = '٢'; break;

            case '3': e.KeyChar = '٣'; break;

            case '4': e.KeyChar = '٤'; break;

            case '5': e.KeyChar = '٥'; break;

            case '6': e.KeyChar = '٦'; break;

            case '7': e.KeyChar = '٧'; break;

            case '8': e.KeyChar = '٨'; break;

            case '9': e.KeyChar = '٩'; break;
        }
    }


What I have tried:

I tried to convert KeyCode/KeyChar and vice versa but couldn't get the required results
Posted
Updated 8-Nov-19 4:59am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Please, read this: Keys Enum (System.Windows.Forms) | Microsoft Docs[^]

If you want to catch key press event (key down) for numeric keypad only, you have to check if e.KeyCode[^] is between NumPad0 and NumPad9 ;)

private void ArabicNumbers_KeyDown(object sender, KeyPressEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.NumPad0: e.KeyChar = '٠'; break;

            case Keys.NumPad1: e.KeyChar = '١'; break;

            case Keys.NumPad2: e.KeyChar = '٢'; break;

            case Keys.NumPad3: e.KeyChar = '٣'; break;

            case Keys.NumPad4: e.KeyChar = '٤'; break;

            case Keys.NumPad5: e.KeyChar = '٥'; break;

            case Keys.NumPad6: e.KeyChar = '٦'; break;

            case Keys.NumPad7: e.KeyChar = '٧'; break;

            case Keys.NumPad8: e.KeyChar = '٨'; break;

            case Keys.NumPad9: e.KeyChar = '٩'; break;
        }
    }
   
v2
Comments
Moosa Arbeed 29-Oct-19 9:53am
   
nothing has changed
0x01AA 29-Oct-19 12:26pm
   
0x01AA 29-Oct-19 12:28pm
   
A 5. Maybe you should also mention Key Preview ;)
0x01AA 30-Oct-19 14:52pm
   
Nevertheless a second trial to send a comment on this :-)
Most probably it is worth to give OP a hint that maybe Form's KeyPreview is relevant ;)
Bruno
Matthew Dennis 30-Oct-19 17:33pm
   
test
Rate this:
Please Sign up or sign in to vote.

Solution 2

"English Numbers" are also know as Western Arabic, and what you are looking for is converting to Eastern Arabic.

One option is to get the value and get the appropriate UTF code, where the numbers begin at U+0660.

Another option would be use manual conversion of the culture info, as seen here:
c# - Converting Numbers from Western Arabic Digits "1,2,3..." to Eastern Arabic Digits "١, ٢, ٣...." - Stack Overflow[^]
   
Comments
Moosa Arbeed 29-Oct-19 11:32am
   
I do not want to go through cultures, I just want to replace the numbers at run-time using the same function "using key events only"
the thing is, I just want to replace those numbers using the numpad "keypad"
while number at the top of the keyboard will remain the same
Maciej Los 30-Oct-19 5:00am
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 3

I figured it out myself

private bool _isKeyNumPadClick;

    private void TxtEdtDocNo_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            _isKeyNumPadClick = true;
        }

        else if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        {
            _isKeyNumPadClick = false;
        }
    }


    private void Numbers_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (_isKeyNumPadClick)
        {
            switch (e.KeyChar)
            {
                case '0': e.KeyChar = '٠'; break;

                case '1': e.KeyChar = '١'; break;

                case '2': e.KeyChar = '٢'; break;

                case '3': e.KeyChar = '٣'; break;

                case '4': e.KeyChar = '٤'; break;

                case '5': e.KeyChar = '٥'; break;

                case '6': e.KeyChar = '٦'; break;

                case '7': e.KeyChar = '٧'; break;

                case '8': e.KeyChar = '٨'; break;

                case '9': e.KeyChar = '٩'; break;
            }
        }
        else
        {
            switch (e.KeyChar)
            {
                case '0': e.KeyChar = '0'; break;

                case '1': e.KeyChar = '1'; break;

                case '2': e.KeyChar = '2'; break;

                case '3': e.KeyChar = '3'; break;

                case '4': e.KeyChar = '4'; break;

                case '5': e.KeyChar = '5'; break;

                case '6': e.KeyChar = '6'; break;

                case '7': e.KeyChar = '7'; break;

                case '8': e.KeyChar = '8'; break;

                case '9': e.KeyChar = '9'; break;
            }
        }

        _isKeyNumPadClick = false;
    }
   
v3

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100