Solved

Add Method Override

Posted on 1998-06-16
3
290 Views
Last Modified: 2010-04-04
I would like to override Add Method for ListBox.

Or to be more specific:

How could I make a new ListBox component, that will
act like this:

   NewListBox.Items.Add('12345');
   s := NewListBox.Items[0];
   // after this s contains '54321'

Also I need override for Insert method in same manner.
0
Comment
Question by:bor061297
3 Comments
 
LVL 1

Expert Comment

by:michaue
ID: 1353319
May I ask why you want to do this, then I would probably be able to give you a good answer ...
0
 
LVL 10

Accepted Solution

by:
Jacco earned 100 total points
ID: 1353320
Here is a sample.

In the implementation section I implemented a new TStrings class TNewListBoxStrings which reversed the strings when added or inserted. (The rest is just copied from the source in StdCtrls)

In the interface section I implemented a TNewListBox which uses the new class. It declares a new FItems variable and a published property for it. The SetItems method for this property is also provided. In the constructor the Old Items are freed (just to be sure and the new Items are created.

Hope it helps you.

Regards Jacco

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TNewListBox = class(TListBox)
  private
    FItems : TStrings;
    procedure SetItems(Value : TStrings);
  public
    constructor Create(AOwner : TComponent); override;
  published
    property Items : TStrings read FItems write SetItems;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FNewListBox : TNewListBox;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

// utility function

function ReverseString(const S : String) : String;
var
  iChar : Integer;
begin
  Result:='';
  for iChar:=Length(S) downto 1 do
    Result:=Result+S[iChar];
end;

// TNewListBoxStrings

type
  TNewListBoxStrings = class(TStrings)
  private
    ListBox: TCustomListBox;
  protected
    function Get(Index: Integer): string; override;
    function GetCount: Integer; override;
    function GetObject(Index: Integer): TObject; override;
    procedure PutObject(Index: Integer; AObject: TObject); override;
    procedure SetUpdateState(Updating: Boolean); override;
  public
    function Add(const S: string): Integer; override;
    procedure Clear; override;
    procedure Delete(Index: Integer); override;
    procedure Insert(Index: Integer; const S: string); override;
  end;

function TNewListBoxStrings.GetCount: Integer;
begin
  Result := SendMessage(ListBox.Handle, LB_GETCOUNT, 0, 0);
end;

function TNewListBoxStrings.Get(Index: Integer): string;
var
  Len: Integer;
  Text: array[0..4095] of Char;
begin
  Len := SendMessage(ListBox.Handle, LB_GETTEXT, Index, Longint(@Text));
  if Len < 0 then raise EStringListError.Create('List index error');
  SetString(Result, Text, Len);
end;

function TNewListBoxStrings.GetObject(Index: Integer): TObject;
begin
  Result := TObject(SendMessage(ListBox.Handle, LB_GETITEMDATA, Index, 0));
  if Longint(Result) = LB_ERR then
    raise EStringListError.Create('List index error');
end;

procedure TNewListBoxStrings.PutObject(Index: Integer; AObject: TObject);
begin
  SendMessage(ListBox.Handle, LB_SETITEMDATA, Index, Longint(AObject));
end;

// this one reverses the string
function TNewListBoxStrings.Add(const S: string): Integer;
begin
  Result := SendMessage(ListBox.Handle, LB_ADDSTRING, 0, Longint(PChar(ReverseString(S))));
  if Result < 0 then raise EOutOfResources.Create('Insert line error');
end;

procedure TNewListBoxStrings.Insert(Index: Integer; const S: string);
begin
  if SendMessage(ListBox.Handle, LB_INSERTSTRING, Index,
    Longint(PChar(ReverseString(S)))) < 0 then
    raise EOutOfResources.Create('Insert line error');
end;

procedure TNewListBoxStrings.Delete(Index: Integer);
begin
  SendMessage(ListBox.Handle, LB_DELETESTRING, Index, 0);
end;

procedure TNewListBoxStrings.Clear;
begin
  SendMessage(ListBox.Handle, LB_RESETCONTENT, 0, 0);
end;

procedure TNewListBoxStrings.SetUpdateState(Updating: Boolean);
begin
  SendMessage(ListBox.Handle, WM_SETREDRAW, Ord(not Updating), 0);
  if not Updating then ListBox.Refresh;
end;

// TNewListBox

constructor TNewListBox.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  inherited Items.Free;
  FItems:=TNewListBoxStrings.Create;
  TNewListBoxStrings(FItems).ListBox:=Self;
end;

procedure TNewListBox.SetItems(Value : TStrings);
begin
  Items.Assign(Value);
end;

// TForm1

procedure TForm1.FormCreate(Sender: TObject);
begin
  FNewListBox:=TNewListBox.Create(Self);
  with FNewListBox do begin
    Parent:=Self;
    Items.Add('12345');
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FNewListBox.Items.Add('abcde');
end;

end.

0
 

Author Comment

by:bor061297
ID: 1353321
Adjusted points to 100
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question