var s:string; i,e,c:integer; begin s:=Edit1.text; e:=0;c:=0; for i:=1 to length(s) do begin if (ord(s[i])>=33)and(ord(s[i])<=126) then begin inc(e); label1.caption:='英文字数:'+inttostr(e); end else if (ord(s[i])>=127) then begin inc(c); label2.caption:='中文字数:'+inttostr(c div 2); end; end; end;
那就在EDIT控件的ONChange()是上面的代码.
把1楼的代码放到一个函数里..然后在Edit的OnChange事件里调用函数就可以了.
哦,我发现你不会用函数,呵呵,那就告诉你一个更简单的方法吧 在 interfaceuses 加入StrUtils单元procedure TForm1.Edit1Change(Sender: TObject); var i,Count: Integer; begin for i := 1 to Length(Edit1.Text) do begin if ByteType(Edit1.Text,i) <> mbSingleByte then Count := Count + 1; end; showMessage('中文字有: '+IntToStr(Count div 2)+' 个'); end;Email:[email protected]
var s:String; ws:WideString; begin ws:=s; Len:=Length(ws); end;
i,e,c:integer;
begin
s:=Edit1.text;
e:=0;c:=0;
for i:=1 to length(s) do
begin
if (ord(s[i])>=33)and(ord(s[i])<=126) then
begin
inc(e);
label1.caption:='英文字数:'+inttostr(e);
end
else
if (ord(s[i])>=127) then
begin
inc(c);
label2.caption:='中文字数:'+inttostr(c div 2);
end;
end;
end;
在
interfaceuses
加入StrUtils单元procedure TForm1.Edit1Change(Sender: TObject);
var
i,Count: Integer;
begin
for i := 1 to Length(Edit1.Text) do
begin
if ByteType(Edit1.Text,i) <> mbSingleByte then
Count := Count + 1;
end;
showMessage('中文字有: '+IntToStr(Count div 2)+' 个');
end;Email:[email protected]
ws:WideString;
begin
ws:=s;
Len:=Length(ws);
end;