Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add Method Override

Posted on 1998-06-16
3
Medium Priority
?
309 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 400 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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

688 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