已解决:unit VALComboBox;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Menus, Dialogs, StdCtrls;
type
TValComboBox = class(TComboBox)
private
FValue: PString;
FValues: TStrings;
FOnChange: TNotifyEvent;
function GetValue: string;
function GetButtonValue(Index: Integer): string;
procedure SetValue(const Value: string);
procedure SetValues(Value: TStrings);
protected
procedure Change; dynamic;
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Value: string read GetValue write SetValue;
property ItemIndex;
published
property Values: TStrings read FValues write SetValues;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
procedure Register;
implementation
constructor TValComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FValue := NullStr;
FValues := TStringList.Create;
style := csDropDownList;
end;
destructor TValComboBox.Destroy;
begin
DisposeStr (FValue);
FValues.Free;
inherited Destroy;
end;
procedure TValComboBox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
end;
function TValComboBox.GetValue : string;
begin
result:=values[itemindex];
end;
function TValComboBox.GetButtonValue(Index: Integer): string;
begin
if (Index < FValues.Count) and (FValues[Index] <> '') then
Result := FValues[Index]
else if (Index < Items.Count) then
Result := Items[Index]
else
Result := '';
end;
procedure TValComboBox.SetValue (const Value: string);
var
I : Integer;
begin
AssignStr(FValue, Value);
if (ItemIndex < 0) or (GetButtonValue(ItemIndex) <> Value) then
begin
if (ItemIndex >= 0) then ItemIndex := -1;
for I := 0 to Items.Count - 1 do
begin
if GetButtonValue(I) = Value then
begin
ItemIndex := I;
break;
end;
end;
Change;
end;
end;
procedure TValComboBox.SetValues(Value: TStrings);
begin
FValues.Assign(Value);
end;
procedure TValComboBox.Change;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure Register;
begin
RegisterComponents('mycomponent', [TValComboBox]);
end;
end.