错误异常startindex如何不能小于zerodo我...... [英] How error exception startindex cannot be less than zerodo I...

查看:82
本文介绍了错误异常startindex如何不能小于zerodo我......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



没有数字接受也没有英文字符接受



所以我在key_up事件中写如下



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

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不能小于零(索引超出范围)



in line



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

当试图输入阿拉伯语字母时



以便如何解决这个问题



我的尝试:



错误异常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);
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

推荐答案

想想你的代码,当文本框为空时代码中发生了什么(长度= 0)

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





当你不明白你的代码在做什么或为什么它做它做的事情,答案是调试器

使用调试器查看代码正在做什么。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。



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.


这篇关于错误异常startindex如何不能小于zerodo我......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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