错误异常startindex不能小于零(索引超出范围) [英] Error exception startindex cannot be less than zero (index out of range)

查看:190
本文介绍了错误异常startindex不能小于零(索引超出范围)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有textbox1,我需要允许此textbox1仅接受阿拉伯字符

I have textbox1 and i need to allow this textbox1 to accept Arabic characters only

不接受数字,也不接受任何英文字符

no digit accept also no English characters accept

这样我就可以在key_up事件中编写如下内容

so that i write in key_up event as following

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

 public bool HasArabicCharacters(string text)

        {

            Regex regex = new Regex(

                "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");

            return regex.IsMatch(text);
        }

我收到错误异常startindex不能小于零(索引超出范围)

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

在线

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

当尝试输入阿拉伯字母时

when try to enter Arabic letters

那么如何解决这个问题

推荐答案

我得到错误异常startindex不能小于零(索引超出范围)

在线

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


也许是这样吗?:

Perhaps this?:

if(textBox1.Text.Length != 0)
	textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
	

-韦恩


这篇关于错误异常startindex不能小于零(索引超出范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆