Solved

Add Method Override

Posted on 1998-06-16
3
284 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now