Click here to Skip to main content
15,883,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
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 3:59am
v2

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

C#
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;
        }
    }
 
Share this answer
 
v2
Comments
Moosa Arbeed 29-Oct-19 9:53am    
nothing has changed
[no name] 29-Oct-19 12:26pm    
[no name] 29-Oct-19 12:28pm    
A 5. Maybe you should also mention Key Preview ;)
[no name] 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
"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[^]
 
Share this answer
 
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!
I figured it out myself

C#
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;
    }
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900