Click here to Skip to main content
15,913,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have textbox1 and i need to allow this textbox1 to accept Arabic characters only

no digit accept also no English characters accept

so that i write in key_up event as following

C#
private void textBox1_KeyUp(object sender, KeyEventArgs e)  
        {  
            if (textBox1.Text.Any(char.IsDigit))  
            {  
                  
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);  
                textBox1.Focus();  
            }  
  
            QrClasses qr = new MatrixBarcode.QrClasses();  
            bool b = qr.HasArabicCharacters(textBox1.Text);  
  
            if (b == false)  
            {  
                  
                    textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);  
                  
              
                
                textBox1.Focus();  
  
            }  
            else  
            {  
                textBox1.Enabled = true;  
            }  
        }

C#
public bool HasArabicCharacters(string text)  
  
        {  
  
            Regex regex = new Regex(  
  
                "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");  
  
            return regex.IsMatch(text);  
        }

I get Error exception startindex cannot be less than zero (index out of range)

in line

textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
when try to enter Arabic letters

so that how to solve this problem

What I have tried:

Error exception startindex cannot be less than zero
Posted
Updated 27-Jan-17 13:40pm
Comments
CHill60 27-Jan-17 19:47pm    
So when you debug this code, and you examine the contents of textBox1 is the code behaving as you would expect given the data?
Have a look at Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

1 solution

Think about your code, what happen in your code when the textbox is empty (length=0)
C#
bool b = qr.HasArabicCharacters(textBox1.Text);
if (b == false)
{
    textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
    textBox1.Focus();
}


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
CHill60 27-Jan-17 19:48pm    
5'd ... went for a cuppa and missed your solution ... feel good that we went for the same CP article though :-)
Patrice T 27-Jan-17 20:07pm    
Thank you.

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