• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

Project Approach advice needed

Hi all,

I need some advice here on how to best approach this.

I am working on a project where I need to add people's details for example:

Name, Surname and address.

So, I have the fields -  eg: two textboxes and  a memo (for example) for each person.

So, everytime I click on add record I get a new two textboxes and  a memo ... if I click 3 times then I get 3 etc.

But everytime the new fields are added they are added after each other at runtime so scrolling might be necessary?

Each record would have a name or id so:

id = 1

has 4 people

id = 2

has 7 people

etc etc...

What's the best way to approach this project please?

I'll need to later search and read different records so I would need to save to database?

Thanks
0
error77
Asked:
error77
  • 6
  • 6
  • 6
2 Solutions
 
BdLmCommented:
you should work with lists (TObjectList) and classes eg.  define
a class

   Person= class
       Name, Surname and address : String
      end;

Create a new Class / person and add this one to your object List .....
     
0
 
BdLmCommented:
code example below :  
unit Unit1;

interface

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

type
  // The customer class definition
  TCustomer = class
    private
      // The data fields of this new class
      CustomerName   : String;
      CustomerNumber : Integer;

    public
      // Properties to read these data values
      property Name : String
          read CustomerName;
      property Number : Integer
          read CustomerNumber;

      // Constructor
      constructor Create(const CustomerName   : String;
                         const CustomerNumber : Integer);
  end;

  // The form class definition
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);

  private
    // The TList object we use in this code
    myList : TList;

    // Method to show the contents of our list object
    procedure ShowListContents;

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


// Customer constructor
// --------------------------------------------------------------------------
constructor TCustomer.Create(const CustomerName   : String;
                             const CustomerNumber : Integer);
begin
  // Save the passed parameters
  self.CustomerName   := CustomerName;
  self.CustomerNumber := CustomerNumber;
end;


// TList sort routine : compare customers by name
// --------------------------------------------------------------------------
// The returned integer has the following value :
//
//   > 0 : (positive) Item1 is less than Item2
//     0 : Item1 is equal to Item2
//   < 0 : (negative) Item1 is greater than Item2
function compareByName(Item1 : Pointer; Item2 : Pointer) : Integer;
var
  customer1, customer2 : TCustomer;
begin
  // We start by viewing the object pointers as TCustomer objects
  customer1 := TCustomer(Item1);
  customer2 := TCustomer(Item2);

  // Now compare by string
  if      customer1.Name > customer2.Name
  then Result := 1
  else if customer1.Name = customer2.Name
  then Result := 0
  else Result := -1;
end;


// A routine to display the contents of our list
// --------------------------------------------------------------------------
procedure TForm1.ShowListContents;
var
  i : Integer;
begin
  // And redisplay the list
  for i := 0 to myList.Count-1 do
  begin
    ShowMessage(TCustomer(myList[i]).Name+' is customer number '+
                IntToStr(TCustomer(myList[i]).Number));
  end;
end;

// Form constructor
// --------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
  customer : TCustomer;

begin
  // Create the TList object to hold a set of customer objects
  myList := TList.Create;

  // Create some customer objects and add to our object list
  customer := TCustomer.Create('Neil Moffatt', 123);
  myList.Add(customer);
  customer := TCustomer.Create('Bill Gates', 64);
  myList.Add(customer);

  // We can add the object without assigning to an intermediate variable
  myList.Add(TCustomer.Create('Henry Cooper', 999));
  myList.Add(TCustomer.Create('Alan Sugar', 2));

  // Now display the list
  ShowListContents;

  // We will now sort the list into name sequence and redisplay
  myList.Sort(compareByName);

  // And redisplay the list
  ShowListContents;

  // Now do some object inserts and deletes
  // Note that indices start at 0
  myList.Insert(2, TCustomer.Create('Added as item 3', 33));
  myList.Delete(4);

  // And redisplay the list
  ShowListContents;

  // Free up the list
  myList.free;
end;

end.
Show full unit code
   Neil Moffatt is customer number 123
   Bill Gates is customer number 64
   Henry Cooper is customer number 999
   Alan Sugar is customer number 2
  
   Alan Sugar is customer number 2
   Bill Gates is customer number 64
   Henry Cooper is customer number 999
   Neil Moffatt is customer number 123
  
   Alan Sugar is customer number 2
   Bill Gates is customer number 64
   Added as item 3 is customer number 33
   Henry Cooper is customer number 999

Open in new window

0
 
BdLmCommented:
last 2 comments:  
----------------------------    1 -------------------------------------------------------
change    TCustomer = class
    private
      // The data fields of this new class
      CustomerName   : String;
      CustomerNumber : Integer
      CustomerAddress : String;
      CustomerSurname :String

........

----------------------------------   2 --------------------------------------------------

insterad of myList : TList  later I recommend
                  myList : TObjectList


but first ---  is this the solution you are looking for ??
To store the data into a database or text file we can discuss later on
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Geert GOracle dbaCommented:
the approach ?
the first thing, ask questions about your project

first > what budget do you have ?
none > free databases (mysql, oracle express, sql express)
no limit > oracle/sqlserver enterprise and RemObjects

second > knowledge
> delphi, are you a beginner ?
  >> look for tutorials > about.com has a lot
  >> look for ready made components or projects on torry.net or embarcadero.com
> advanced ???

third > project scope
> what time do you have ?
> what does the project do ?
> how many users ?
> how much data ?
> lan/wan ?
> type ? website ? exe ? service ?
> can you find all the resources ?

fourth > 
> project blueprint
> development plan
> testing plan
> deployment plan

>finally > you can get started with coding ... :)
0
 
error77Author Commented:
2 Things...

1.   On myList : TObjectList  I get Undeclared identifier: 'TObjectList'

and also...How can I visually test this please?

THanks

0
 
Geert GOracle dbaCommented:
i would advice looking for complete projects on torry.net

a CD catalogueing program with a database is a nice start for learning ...
http://www.torry.net/pages.php?id=1216


0
 
BdLmCommented:


can you get the demo code running - compiled , .....  (if not I can create a zip and post that code here)

TObjectList    http://delphi.wikia.com/wiki/TObjectList_Class

need a unit to be added    

 uses ContNrs;
0
 
error77Author Commented:
Geert_Gruwez: Source is not available on these projects.
0
 
error77Author Commented:
BdLm: The project now compiles fine but it just shows a blank form.

What do I need to do to have a visual please?
0
 
BdLmCommented:
place a TMemo on you form and a Button, double click the button and add code here like :




form1.Bottonclick (.....)
var  i : Integer;
begin
  // And redisplay the list
  for i := 0 to myList.Count-1 do
  begin
      memo1.lines.add(TCustomer(myList[i]).Name+' is customer number '+ IntToStr(TCustomer(myList[i]).Number));
  end;

end;

Open in new window

0
 
BdLmCommented:
you may add other buttons for add elements, clear the list ......    do you need more help on that also ?
0
 
error77Author Commented:
I get access violation here: for i := 0 to myList.Count-1 do

Here is what I have in full below

 
unit Unit1;

interface

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

type
  // The customer class definition
  TCustomer = class
    private
      // The data fields of this new class
       // The data fields of this new class
      CustomerName   : String;
      CustomerNumber : Integer;
      CustomerAddress : String;
      CustomerSurname :String;

    public
      // Properties to read these data values
      property Name : String
          read CustomerName;
      property Number : Integer
          read CustomerNumber;

      // Constructor
      constructor Create(const CustomerName   : String;
                         const CustomerNumber : Integer);
  end;

  // The form class definition
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);

  private
    // The TList object we use in this code
      myList : TObjectList;

    // Method to show the contents of our list object
    procedure ShowListContents;

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


// Customer constructor
// --------------------------------------------------------------------------
constructor TCustomer.Create(const CustomerName   : String;
                             const CustomerNumber : Integer);
begin
  // Save the passed parameters
  self.CustomerName   := CustomerName;
  self.CustomerNumber := CustomerNumber;
end;


// TList sort routine : compare customers by name
// --------------------------------------------------------------------------
// The returned integer has the following value :
//
//   > 0 : (positive) Item1 is less than Item2
//     0 : Item1 is equal to Item2
//   < 0 : (negative) Item1 is greater than Item2
function compareByName(Item1 : Pointer; Item2 : Pointer) : Integer;
var
  customer1, customer2 : TCustomer;
begin
  // We start by viewing the object pointers as TCustomer objects
  customer1 := TCustomer(Item1);
  customer2 := TCustomer(Item2);

  // Now compare by string
  if      customer1.Name > customer2.Name
  then Result := 1
  else if customer1.Name = customer2.Name
  then Result := 0
  else Result := -1;
end;


// A routine to display the contents of our list
// --------------------------------------------------------------------------
procedure TForm1.ShowListContents;
var
  i : Integer;
begin
  // And redisplay the list
  for i := 0 to myList.Count-1 do
  begin
    ShowMessage(TCustomer(myList[i]).Name+' is customer number '+
                IntToStr(TCustomer(myList[i]).Number));
  end;
end;

// Form constructor
// --------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var  i : Integer;
begin
  // And redisplay the list
  for i := 0 to myList.Count-1 do
  begin
      memo1.lines.add(TCustomer(myList[i]).Name+' is customer number '+ IntToStr(TCustomer(myList[i]).Number));
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  customer : TCustomer;

begin
  // Create the TList object to hold a set of customer objects
  myList := TObjectList.Create;

  // Create some customer objects and add to our object list
  customer := TCustomer.Create('Neil Moffatt', 123);
  myList.Add(customer);
  customer := TCustomer.Create('Bill Gates', 64);
  myList.Add(customer);

  // We can add the object without assigning to an intermediate variable
  myList.Add(TCustomer.Create('Henry Cooper', 999));
  myList.Add(TCustomer.Create('Alan Sugar', 2));

  // Now display the list
  ShowListContents;

  // We will now sort the list into name sequence and redisplay
  myList.Sort(compareByName);

  // And redisplay the list
  ShowListContents;

  // Now do some object inserts and deletes
  // Note that indices start at 0
  myList.Insert(2, TCustomer.Create('Added as item 3', 33));
  myList.Delete(4);

  // And redisplay the list
  ShowListContents;

  // Free up the list
  myList.free;
end;

end.
Show full unit code
   Neil Moffatt is customer number 123
   Bill Gates is customer number 64
   Henry Cooper is customer number 999
   Alan Sugar is customer number 2
  
   Alan Sugar is customer number 2
   Bill Gates is customer number 64
   Henry Cooper is customer number 999
   Neil Moffatt is customer number 123
  
   Alan Sugar is customer number 2
   Bill Gates is customer number 64
   Added as item 3 is customer number 33
   Henry Cooper is customer number 999

Open in new window

0
 
Geert GOracle dbaCommented:
google ... your best resource ...

code central in edn on embarcadero.com

http://ddaf.sourceforge.net/
http://sourceforge.net/projects/acctgsys/

0
 
Geert GOracle dbaCommented:
what bdlm is trying to show you is the very tip of the iceberg ...
the start of a database framework

how you do it
> create a TCustomer class
> this class will read and write customer info to the database

> next create a TCustomers container > for managing multiple customers

this implies that you know all the ins and outs of delphi database programming ...
0
 
error77Author Commented:
What I'm after is a delphi version of a html table ... each instance of the table would be added at runtime.

Is this more clear?
0
 
Geert GOracle dbaCommented:
ugh ?
this is completely different than your original question...
0
 
error77Author Commented:
Might be my way of explaining it :o/ Will open another question.
0
 
Geert GOracle dbaCommented:
are you talking about a stringgrid or a dbgrid ?

place a grid on your form
you can set the columns and rows with that

grid:
http://desktoppub.about.com/od/grids/Grids_for_Desktop_Publishing.htm

dbgrid:
http://delphi.about.com/od/usedbvcl/a/tdbgrid.htm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 6
  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now