Solved

Add Method Override

Posted on 1998-06-16
3
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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