Extended or New TurboVision Objects


Q. 405 How do I insert, delete or edit items from a collection assigned to a TListBox?
The concept is very easy. You maintain one collection to which you insert, from which you delete and edit. You should however maintain the relation ship between the number of items the TListBox thinks your collection has and your collection really has.

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
B de Boer Last modified: Tuesday 18 April 1995 - 10:20