Solved

Add Method Override

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

18 Experts available now in Live!

Get 1:1 Help Now