有个EDIT控件,假如它的TEXT是‘123456789’
我现在想实现的,让这个控件的部分只读,意思就是前面123是只读的,必须保证前面是开头是123。这样的代码好象不可以的:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  s: string;
begin
  s := '123';
  if (length(edit1.Text) = 3) or (copy(edit1.text, 1, 3) <> s) then
    Key := #0;end;
请大虾帮忙,分不够再加

解决方案 »

  1.   

    用一个取巧的办法,用MaskEdit试试设置 EditMask属性为:!12399999;1;_
      

  2.   

    var
      s: string;
    begin
      s := '123';
      if (copy(edit1.text, 1, 3) = s) then
        if (length(edit1.text) <= 3) and (key = #8)then
          Key := #0;
    end;
      

  3.   

    这段代码显然是错误的,你这里的edit1.text显然是change前的内容。procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    var
      s: string;
      strAftChg:string;
    begin
      s := '123';
      if edit1.SelStart<2 then
      begin
        key:=#0;
        exit;
      end;
      strAftChg:=Copy(edit1.Text,1,edit1.SelStart) + Key;
      if (edit1.SelStart<length(edit1.Text)) and ((edit1.SelLength+ edit1.SelStart)<length(edit1.Text)) then
        strAftChg:=StrAftChg+ copy(edit1.Text,edit1.SelLength+ edit1.SelStart+1, length(edit1.Text)- edit1.SelLength- edit1.SelStart);
      if  (copy(strAftChg, 1, 3) <> s) then
        Key := #0;end;
    这段代码作了模拟修改后处理用strAftChg来判断,而不是edit.text