首页  编辑  

不完全输入

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

(*//

标题:不完全输入

说明:Edit、ComboBox自动匹配输入字符;正好弥补Delphi6.0不支持中文不完全输入

设计:Zswang

日期:2002-01-26

支持:wjhu111@21cn.com

//*)

///////Begin Source

function EditComplete(mEdit: TEdit; mItems: TStrings;

 mCase: Boolean = True): Boolean;

var

 I, T: Integer;

 vSubStr, S: string;

begin

 Result := False;

 if (not Assigned(mEdit)) or (not Assigned(mItems)) then Exit;

 vSubStr := mEdit.Text;

 if not mCase then vSubStr := UpperCase(vSubStr);

 for I := 0 to Pred(mItems.Count) do begin

   S := mItems[I];

   if not mCase then S := UpperCase(S);

   if (Pos(vSubStr, S) = 1) and (vSubStr <> S) then begin

     T := Length(vSubStr);

     mEdit.Text := mItems[I];

     mEdit.SelStart := T;

     mEdit.SelLength := Length(mItems[I]) - T;

     Result := True;

     Break;

   end;

 end;

end; { EditComplete }

function ComboBoxComplete(mComboBox: TComboBox; mCase: Boolean = True): Boolean;

var

 I, T: Integer;

 vSubStr, S: string;

begin

 Result := False;

 if not Assigned(mComboBox) then Exit;

 vSubStr := mComboBox.Text;

 if not mCase then vSubStr := UpperCase(vSubStr);

 for I := 0 to Pred(mComboBox.Items.Count) do begin

   S := mComboBox.Items[I];

   if not mCase then S := UpperCase(S);

   if (Pos(vSubStr, S) = 1) and (vSubStr <> S) then begin

     T := Length(vSubStr);

     mComboBox.Text := mComboBox.Items[I];

     mComboBox.SelStart := T;

     mComboBox.SelLength := Length(mComboBox.Items[I]) - T;

     Result := True;

     Break;

   end;

 end;

end; { EditComplete }

///////End Source

///////Begin Demo

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;

 Shift: TShiftState);

begin

 case Key of

   VK_BACK, VK_DELETE, VK_LEFT, VK_RIGHT, VK_DOWN, VK_UP: ;

 else EditComplete(TEdit(Sender), Memo1.Lines, False);

 end;

end;

procedure TForm1.ComboBox1KeyUp(Sender: TObject; var Key: Word;

 Shift: TShiftState);

begin

 case Key of

   VK_BACK, VK_DELETE, VK_LEFT, VK_RIGHT, VK_DOWN, VK_UP: ;

 else ComboBoxComplete(TComboBox(Sender), False);

 end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

 ComboBox1.AutoComplete := False; //for Delphi6.0

end;

procedure TForm1.Memo1Change(Sender: TObject);

begin

 ComboBox1.Items.Text := TMemo(Sender).Text;

end;

///////End Demo