2
Answers

Error exception startindex cannot be less than zero

ahmed salah

ahmed salah

7y
316
1

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

  1. private void textBox1_KeyUp(object sender, KeyEventArgs e)  
  2.         {  
  3.             if (textBox1.Text.Any(char.IsDigit))  
  4.             {  
  5.                   
  6.                 textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);  
  7.                 textBox1.Focus();  
  8.             }  
  9.   
  10.             QrClasses qr = new MatrixBarcode.QrClasses();  
  11.             bool b = qr.HasArabicCharacters(textBox1.Text);  
  12.   
  13.             if (b == false)  
  14.             {  
  15.                   
  16.                     textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);  
  17.                   
  18.               
  19.                 
  20.                 textBox1.Focus();  
  21.   
  22.             }  
  23.             else  
  24.             {  
  25.                 textBox1.Enabled = true;  
  26.             }  
  27.         }  
  1. public bool HasArabicCharacters(string text)  
  2.   
  3.         {  
  4.   
  5.             Regex regex = new Regex(  
  6.   
  7.                 "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");  
  8.   
  9.             return regex.IsMatch(text);  
  10.         }  

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

in line

  1. textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);  

when try to enter Arabic letters

so that how to solve this problem

 
 
Answers (2)