?
Solved

Project Approach advice needed

Posted on 2011-05-04
18
Medium Priority
?
463 Views
Last Modified: 2012-05-11
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
Comment
Question by:error77
  • 6
  • 6
  • 6
18 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 35688161
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
 
LVL 8

Accepted Solution

by:
BdLm earned 1000 total points
ID: 35688167
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
 
LVL 8

Expert Comment

by:BdLm
ID: 35688176
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35688273
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
 

Author Comment

by:error77
ID: 35688277
2 Things...

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

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

THanks

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35688312
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
 
LVL 8

Expert Comment

by:BdLm
ID: 35688324


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
 

Author Comment

by:error77
ID: 35688326
Geert_Gruwez: Source is not available on these projects.
0
 

Author Comment

by:error77
ID: 35688340
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
 
LVL 8

Expert Comment

by:BdLm
ID: 35688353
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
 
LVL 8

Expert Comment

by:BdLm
ID: 35688358
you may add other buttons for add elements, clear the list ......    do you need more help on that also ?
0
 

Author Comment

by:error77
ID: 35688386
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35688406
google ... your best resource ...

code central in edn on embarcadero.com

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

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35688419
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
 

Author Comment

by:error77
ID: 35688482
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35688492
ugh ?
this is completely different than your original question...
0
 

Author Comment

by:error77
ID: 35688504
Might be my way of explaining it :o/ Will open another question.
0
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 1000 total points
ID: 35688507
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 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