Here follows the code for a simple TStringCollection and the associated TListBox. I called its descendent TStringInputBox. TListBox2 is the general object for editing lists, TStringInputBox is tailored to TStringCollections.
type
PListBox2 = ^TListBox2;
TListBox2 = object(TListBox)
constructor Init(var Bounds: TRect; ANumCols: Integer;
AVScrollBar: PScrollBar);
procedure HandleEvent(var Event : TEvent); virtual;
procedure InsertItem; virtual;
procedure DeleteItem; virtual;
procedure EditItem; virtual;
end;
PStringInputBox = ^TStringInputBox;
TStringInputBox = object(TListBox2)
procedure InsertItem; virtual;
procedure DeleteItem; virtual;
procedure EditItem; virtual;
end;
{* TListBox2 *}
constructor TListBox2.Init(var Bounds: TRect; ANumCols: Integer;
AVScrollBar: PScrollBar);
begin
inherited Init(Bounds, ANumCols, AVScrollBar);
Options := Options or ofPostProcess;
end;
procedure TListbox2.HandleEvent(var Event : TEvent);
var
b : Boolean;
begin
inherited HandleEvent(Event);
case Event.What of
evCommand : begin
case Event.Command of
cmBInsertItem, cmInsertItem :
InsertItem;
cmBDeleteItem, cmDeleteItem :
if Focused < Range then DeleteItem;
cmEditItem :
if Focused < Range then EditItem;
else Exit;
end; { of case }
end;
evBroadCast : begin
case Event.Command of
cmReceivedFocus :
if Event.InfoPtr = @Self then
EnableCommands([cmInsertItem, cmDeleteItem]);
cmReleasedFocus :
if Event.InfoPtr = @Self then
DisableCommands([cmInsertItem, cmDeleteItem]);
else Exit;
end; { of case }
end;
else Exit;
end; { of case }
ClearEvent(Event);
end;
procedure TListbox2.InsertItem;
begin
Abstract;
end;
procedure TListbox2.DeleteItem;
begin
Abstract;
end;
procedure TListbox2.EditItem;
begin
Abstract;
end;
{* TStringInputBox *}
procedure TStringInputBox.InsertItem;
var
s : string;
begin
s := '';
if InputString('Parameter', s, 255, '', hcNoContext) = cmOK
then begin
List^.Insert(NewStr(s));
SetRange(Range+1);
Inc(Focused);
DrawView;
end;
end;
procedure TStringInputBox.DeleteItem;
var
s : string;
begin
if UserAnswer('Delete current item?', hcNoContext) = Yes
then begin
List^.AtFree(Focused);
SetRange(Range-1);
DrawView;
end;
end;
procedure TStringInputBox.EditItem;
var
s : string;
begin
s := GetStr(PString(List^.At(Focused)));
if InputString('Parameter', s, 255, '', hcNoContext) = cmOK
then begin
List^.FreeItem(List^.At(Focused));
List^.Insert(NewStr(s));
DrawView;
end;
end;
Next question