unit FillComboBox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TFillComboBox =
class(TComboBox)
private
FAutomaticFillin: Boolean;
procedure SetAutomaticFillin(
const Value: Boolean);
protected
procedure ComboWndProc(
var Message: TMessage; ComboWnd: HWnd; ComboProc: Pointer
);
override;
public
published
constructor Create(AOwner: TComponent);
override;
property AutomaticFillin: Boolean
read FAutomaticFillin
write SetAutomaticFillin
default True;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('
Standard', [TFillComboBox]);
end;
{ TFillComboBox }
procedure TFillComboBox.ComboWndProc(
var Message: TMessage; ComboWnd: HWnd; ComboProc: Pointer
);
var
I: Integer;
CurrentText:
String;
begin
inherited ComboWndProc(
Message, ComboWnd, ComboProc);
// skip processing, if turned off
if not FAutomaticFillin
then
Exit;
// first check whether the backspace key was pressed, we do not fill in
// in such case!
if Message.Msg = WM_CHAR
then
begin
// all characters from 32 (Space) through 127 (Upper ANSI) are matched
if TWMChar(
Message).CharCode
in [$20..$7F]
then
begin
// fill in the rest of the text
// save the current text, the user has typed
CurrentText := Text;
// get the first string, matching the text partially
I := SendMessage(
Handle, CB_FINDSTRING, -1, LongInt(PChar(CurrentText)));
if I >= 0
then
begin
// match found!
// load matching text, I is the position of the matching string
Text := Items.Strings[I];
// select the text beyond the text typed
SelStart := Length(CurrentText);
SelLength := Length(Text) - Length(CurrentText);
end;
end;
end;
end;
constructor TFillComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAutomaticFillin := True;
end;
procedure TFillComboBox.SetAutomaticFillin(
const Value: Boolean);
begin
FAutomaticFillin := Value;
end;
end.