悟空-简单就好
.net企业级应用研究

考虑最复杂的情况
开发出最简单的实现


多种方式,帮你快速找到文档(知识点)!
今天在做IBM AS400 DB2和Excle加载项一个数据交换碰到的一个问题:
  限制每行输入的字符数。以下为源代码(textBox1命名保留为缺省为方便您测试使用)
有用的两个函数:
1、计算字符串的出现次数
2、计算英汉混合字符串的字节数
 1private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
 2        {
 3            const int MaxLength=10;
 4            char chrKeyIn;
 5            string strLastCurrentFocus,strCurrentLine;
 6            int intCurrentLine;
 7            //计算当前行号
 8            strLastCurrentFocus=this.textBox1.Text.Substring(0,this.textBox1.SelectionStart);
 9            intCurrentLine=RepeatNumber (strLastCurrentFocus,"\r\n");
10            //计算当前行的字节数
11            chrKeyIn=e.KeyChar;
12            strCurrentLine=this.textBox1.Lines[intCurrentLine-1]+chrKeyIn;
13            if(LenB(strCurrentLine)>MaxLength)
14            {
15                if(false==System.Char.IsControl(chrKeyIn)  )
16                    e.Handled =true;
17            }

18        }

19        //计算字符串的出现次数
20        private int RepeatNumber(string strSource,string strFind)
21        {
22            int intLastFind;
23            int intNumber;
24                intNumber=1;
25            intLastFind=strSource.IndexOf(strFind);
26            while(intLastFind>0)
27            {
28                intNumber++;
29                intLastFind=strSource.IndexOf(strFind,intLastFind+1);
30            }

31            return intNumber;
32        }

33        //计算英汉混合字符串的字节数
34        private int LenB(string strSource)
35        {
36            int intLength;
37            char[] aryLenB=strSource.ToCharArray() ;
38            intLength=0;
39            foreach(char chrLenB in aryLenB)
40            {
41                if((int)chrLenB>255)
42                    intLength+=2;
43                else
44                    intLength++;
45            }

46            return intLength;
47        }
posted on 2005-09-13 14:18 针式个人知识库管理 阅读(2408) 评论(8)  编辑 收藏
Comments
  • gifted [未注册用户]
    Posted @ 2005-09-13 16:52
    我是个初学者,不过我想问一下是不是第十二行写错了。


      回复  引用    
  • #2楼 
    gifted [未注册用户]
    Posted @ 2005-09-13 17:08
    还有一个问题,如果selectionStart的位置人为的改变了呢,那样长度会不会变化

    我还作了个小试验我放了两个textbox,在第一个textbox的keypress事件中加入
    string str;
    str=this.textBox1.Text.Substring(0,this.textBox1.SelectionStart);
    this.textBox2.Text=str;
    为什么当按了第二个字符时,textbox2中才显示textbox1中的第一个字符   回复  引用    
  • #3楼 [楼主]
          
    Posted @ 2005-09-13 17:57
    因为按第二个字符时this.textBox1.Text为第一字符,即
    按了第二个字符时的keypress后(没有取消的话)才为两次输入的字符
    另:第十二行没有写错,这是C#,不是VB的代码   回复  引用  查看    
  • #4楼 
    g555 [未注册用户]
    Posted @ 2005-09-14 09:43
    粘贴的进来的数据不能限制   回复  引用    
  • #5楼 [楼主]
          
    Posted @ 2005-09-14 09:49
    g555,谢谢发现该Bug。目前,还只能在数据上载前再做一次数据Check(治标方法)   回复  引用  查看    
  • #6楼 
    laserman [未注册用户]
    Posted @ 2005-09-14 09:54
    //计算英汉混合字符串的字节数
    用Encoding好像更合理   回复  引用    
  • #7楼 [楼主]
          
    Posted @ 2005-09-14 11:00
    1、使用Encoding来计算字符串的字节数确实更佳。
    2、实际使用中因为要符合As400字符串长度计算要求,目前只找到此方法;即要计算汉字的出现次数   回复  引用  查看    
  • #8楼 
    分发 [未注册用户]
    Posted @ 2006-02-09 12:03
    计算英汉混合字符串的字节数 中用foreach来计算是错的,如果全部是中文则不正确   回复  引用    

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  博客园首页

  新闻频道

  社区

  小组

  博问

  网摘

  闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
成果网帮您增加网站收入


相关链接: