private void button1_Click(object sender, EventArgs e)
        {
            string input = textBox1.Text.Trim();
           
            if (Regex.IsMatch(input, "^[1-9]\\d{1,11}"))
            {
                txtResult.Text = NumberToChinese2(input);
            }
            else
            {
                MessageBox.Show(this, "输入内容必须是1-12位的数字,请检查!", "错误");
            }
        }
        /// <summary>   
        /// 主要转换方法   
        ///思路:对输入的数字字符串分每4位一段取出添加单位,再对每段内的数字进行转换并添加位数,最后拼接完成.    
        /// </summary>   
        /// <param name="input"> 要转换的数字</param>   
        /// <returns>转换成功的汉字字符串</returns>   
        public string NumberToChinese2(string input)
       {            string returnValue = "";//返回值   
          string[] ChinaNumbers = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玫" };//数字转成中字的字符数组.(汉字数字)   
           string[] digitUnits = { "", "拾", "佰", "仟" };//数字位数的数组(数字单位)   
           string[] teamUnits = { "", ",万,", ",亿,", ",万亿," };//每四位数字一个单元中间应插入的单位.(组单位)   
           int length = input.Length;//获取字符长度   
          int finger = 0;//字符位置指针   
           int m = length % 4;//取模   
           int unitCount = 0;//统计该字符串可分为几段.   
           if (m > 0)   
         {//如果input的长度不是4的倍数.   
               unitCount = length / 4 + 1;   
           }   
           else  
           {//如果input的长度是4的倍数.   .               unitCount = length / 4;   
           }   
           //外层循环,4位一组,给每一组的最后加上组单位:",万亿," ,",亿,",",万,"   
           for (int i = unitCount; i > 0; i--)   
           {   
               int L = 4;//每次要截取的长度,初始为4,但如果input总长度不到4位时,设为input的实际长度   
               if (i == unitCount && m != 0)   
                   L = m;   
               string four = input.Substring(finger, L);//截取到一组数字.   
               int l = four.Length;//获得这组数字的实际长度.   
               //内层循环在该组中的每一位数上循环   
               for (int j = 0; j < l; j++)   
               {   
                   int n = Convert.ToInt32(four.Substring(j, 1));//逐个提取出每一位数字.   
                   if (n == 0)   
                   {//处理位数上为零的情况   
                       if (j < l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !returnValue.EndsWith(ChinaNumbers[n]))   
                           returnValue += ChinaNumbers[n];   
                   }   
                   else  
                   {   
                       if (!(n == 1 && (returnValue.EndsWith(ChinaNumbers[0]) | returnValue.Length == 0) && j == l - 2))   
                       {   
                           returnValue += ChinaNumbers[n];   
                       }                          returnValue += digitUnits[l - j - 1];   
                   }   
             }   
             finger += 1;//指针向前移                    
               if (i < unitCount)//如果不是最高的一组,每组最后加个一个组单位.                 
               {   
                       if (Convert.ToInt32(four) != 0)//如果所有4位数字都不为0,则加上组单位"万","亿"等   
                       returnValue += teamUnits[i - 1];                
               }  
                else           
               {   
                  returnValue += teamUnits[i - 1];//处理最高的一组,最后必须加上单位.            
              }   
           }               return returnValue;         }
    }
}
错误 1 “System.Windows.Forms.Label”不包含“IsMatch”的定义,并且找不到可接受类型为“System.Windows.Forms.Label”的第一个参数的扩展方法“IsMatch”(是否缺少 using 指令或程序集引用?) d:\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs 25 19 WindowsFormsApplication3
提示是

解决方案 »

  1.   

    应该是你的Regex引用的命名空间不对,或者你窗体上有一个Label控件名字也叫Regex
      

  2.   

    错误不在你贴的代码里。有个地方,你直接对 Label 使用 IsMatch 方法了。
      

  3.   

    System.Text.RegularExpressions.Regex.IsMatch(input, patten);
      

  4.   

    复制这个到你随便一个地方public class MyLabelExternMethod
    {
        public static bool IsMatch(this Label label,string pattern)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(label.Text, patten);
        }
    }
      

  5.   

    if (j < l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !returnValue.EndsWith(ChinaNumbers[n]))   
      returnValue += ChinaNumbers[n];   
      }   
      else   
      {   
      if (!(n == 1 && (returnValue.EndsWith(ChinaNumbers[0]) | returnValue.Length == 0) && j == l - 2))   
      {   
      returnValue += ChinaNumbers[n];   
      } returnValue += digitUnits[l - j - 1];   
    [color=#FF0000]
    [/color]
    这一段没有看懂?请高手解释一下、
      

  6.   

    Convert.ToInt32  类型转换。
    EndsWith  最后字符
    if (!(n == 1 && (returnValue.EndsWith(ChinaNumbers[0]) | returnValue.Length == 0) && j == l - 2))   逻辑判断