Results 1 to 16 of 16

Thread: Think your smart enough - HELP!

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Think your smart enough - HELP!

    Ok, im making my first ever address book, and in Delphi 7. I have the following code:

    Code:
    unit Details;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons;
    
    type
      TfrmDetails = class(TForm)
        txtName: TEdit;
        lblName: TLabel;
        txtDateOfBirth: TEdit;
        lblDateOfBirth: TLabel;
        txtAddress: TMemo;
        lblAddress: TLabel;
        lblHomeNumber: TLabel;
        txtHomeNumber: TEdit;
        txtMobileNumber: TEdit;
        lblMobileNumber: TLabel;
        txtEmailAddress: TEdit;
        lblAIMContact: TLabel;
        txtNotes: TMemo;
        lblNotes: TLabel;
        cmdCancel: TBitBtn;
        cmdOK: TBitBtn;
        Procedure AddEntry(Fn,Ad,HP,M,E,N:string);
        procedure cmdCancelClick(Sender: TObject);
        procedure cmdOKClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      frmDetails: TfrmDetails;
      Rgn:THandle;
    
    
    implementation
    
    uses Main;
    
    {$R *.dfm}
    
    Procedure TfrmDetails.AddEntry(Fn,Ad,HP,M,E,N:string);
    begin
         if Not fileExists(QFileName) then rewrite(QFile)
         else if Not Edit then begin
              i:=Fsize;
         end
         else begin
              frmMain.cboNameList.Items.Delete(i);
              i:=Found;
         end;
         seek(QFile,i);
         with Entries do begin
              FullName:=Fn;
              Address:=ad;
              Hphone:=hp;
              Mobile:=m;
              Email:=E;
              Note:=N;
              write(QFile,Entries);
         end;
         closefile(QFile);
         reset(QFile);
         fsize:=filesize(QFile);
         frmMain.cboNameList.Items.add(txtName.text);
         i:=frmMain.cboNameList.Items.IndexOf(txtName.text);
         frmMain.cboNameList.ItemIndex:=i;
    end;
    
    procedure TfrmDetails.cmdCancelClick(Sender: TObject);
    begin
      Close;
    end;
    
    procedure TfrmDetails.cmdOKClick(Sender: TObject);
    var n:integer;
    begin
         if Not Edit then begin
            for n:=0 to frmMain.cboNameList.Items.count-1 do begin
                if txtName.text=frmMain.cboNameList.Items[n] then begin
                   MessageBox(handle,'The name already exists! Type a different name.',
                   'Name Exists',MB_Ok);
                   exit;
                end;
            end;
         end;
         AddEntry(txtName.text,txtAddress.text,txtHomeNumber.text,txtMobileNumber.text,txtEmailAddress.text,txtNotes.text);
         frmMain.seekRecord;
         if Rgn<>0 then DeleteObject(Rgn);
         frmDetails.Close;
         frmDetails.Release;
    end;
    
    end.
    But when i click my OK button, it errors. DOnt know the problem, anyone care to assist?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    As you have provided no details of the error I am stabbing in the dark.

    Arn't you supposed to use the assign procedure to associate the variable QFile with the file location before using any other file I/O functions? Plus if the file does exist you don't even open it.
    Code:
         Assign (QFile, QFileName);
    
         if Not fileExists(QFileName) then rewrite(QFile)
         else if Not Edit then begin
              Append (QFile);
              i:=Fsize;
         end
         else begin
              frmMain.cboNameList.Items.Delete(i);
              i:=Found;
         end;
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Thanks for that, ill test it tomorrow. I dont even know how to intercept the error messages in Delphi yet

  4. #4

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Ok, i did a check now to find out whether the file exists in APP.Path, and if it doesnt to prompt the user to create the file:

    Code:
    unit Main;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, ToolWin, StdCtrls, Menus, ImgList, mmsystem, ShellApi,
      Mask;
    
    type
      TfrmMain = class(TForm)
        procedure mnuFileNewContactClick(Sender: TObject);
        procedure mnuFileEditContactClick(Sender: TObject);
        Procedure SeekRecord;
        procedure mnuFileDeleteContactClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    type
        QRecord = record
        FullName:string[40];
        Address:string[150];
        DateOfBirth:string[40];
        Hphone,Mobile:string[20];
        Email:string[50];
        Note:string[100];
    end;
    
    var
      frmMain: TfrmMain;
      Entries: QRecord;
      QFile, TempFile: File of QRecord;
      i,FSize, Found:integer;
      Deleted, Edit:boolean;
      QFileName,TempFileName: string;
    
    implementation
    
    uses Details;
    
    {$R *.dfm}
    
    Procedure TfrmMain.SeekRecord;
    var n:Integer;
    begin
         for n:=0 to frmMain.cboNameList.Items.count-1 do begin
             seek(Qfile,n);
             Read(QFile,Entries);
             if entries.FullName=frmMain.cboNameList.Items[cboNameList.ItemIndex] then begin
                with Entries do begin
                     with frmMain do begin
                          txtAddress.text:=Address;
                          txtHomeNumber.Text:=Hphone;
                          txtMobileNumber.Text:=Mobile;
                          txtEmailAddress.Text:=Email;
                          txtNotes.Text:=Note;
                          cboNameList.ItemIndex:=i;
                     end;
                     Found:=n;
                     break;
                end;
    
             end;
         end;
    end;
    
    
    procedure TfrmMain.mnuFileNewContactClick(Sender: TObject);
    begin
      frmDetails.Caption:= 'Add new contact';
      frmDetails.ShowModal;
    end;
    
    procedure TfrmMain.mnuFileEditContactClick(Sender: TObject);
    begin
      frmDetails.Caption:= 'Edit contact';
      frmDetails.ShowModal;
    end;
    
    procedure TfrmMain.mnuFileDeleteContactClick(Sender: TObject);
    begin
      if cboNameList.Text = 'No contacts in address book' then
        ShowMessage('There is no contact to delete from Address Book')
      else
        //
    end;
    
    procedure TfrmMain.FormCreate(Sender: TObject);
    var n:integer;
    begin
    
         TempFileName:=ExtractFilePath(application.exename)+'RecTemp.dat';
         AssignFile(TempFile,TempFileName);
         if FileExists(TempFileName) then Erase(TempFile);
    
         QFileName:=ExtractFilePath(application.exename)+'Records.dat';
         Assignfile(QFile, QFileName);
         if FileExists(QfileName) then begin
            reset(QFile);
            FSize:=FileSize(QFile);
            if FSize>0 then begin
               for n:=0 to Fsize-1 do
               begin
                    seek(Qfile,n);
                    Read(QFile,Entries);
                    cboNameList.Items.add(Entries.FullName);
               end;
               cboNameList.ItemIndex:=0;
               SeekRecord;
            end
         end
         else begin
              if Application.MessageBox('Address Book requires new data file in order to save contacts'+#13#10+#13#10+'Would you like to save it now?',
                 'Address Book', MB_YesNo)=IDyes then begin
                 ReWrite(QFile);
                 Reset(QFile);
                 i:=0;
              end;
         end;
         i:=0;
    end;
    Which works perfect, now when i click File > New contact from me menu, fill in required details, and click OK it works great too. Only problem is, when i close my app, and rerun it, it errors again:
    Attached Images Attached Images  

  5. #5
    Lively Member CORONA BEER's Avatar
    Join Date
    May 2004
    Location
    THE INTERNETS!
    Posts
    82
    I dont know really unless i make the app myself cuz i'm just begining Delphi also but would this help?
    //---
    Assignfile(QFile, QFileName);
    if FileExists(QfileName) then begin
    reset(QFile);
    IF IOResult >0 THEN BEGIN
    for n:=0 to Fsize-1 do
    //---

  6. #6

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Ill try in a bit thanks,

    by the way, how do you block errors? In VB its like:

    On Error Resume Next

    or

    On Error Goto Label

    Dont know how to block them in Delphi

    CHeers

  7. #7
    Lively Member CORONA BEER's Avatar
    Join Date
    May 2004
    Location
    THE INTERNETS!
    Posts
    82
    I think Delphi blocks errors by using Try

    Begin
    Try
    Finally
    end;

    begin
    try
    except
    on E: Exception do ErrorDialog(E.Message, E.HelpContext);
    end;

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    In pascal you use compiler directives to turn error handling on and off.

    {$I-} - Turns off file IO error checking for example.

    and

    {$I+} - Turns it back on.

    So you can do somethng like this:
    Code:
    AssignFile (F, FPath);
    
    {$I-}
    ReWrite (F);
    {$I+}
    
    If IOResult = 0 then
      (* file created successfully *)
    Else
      (* file not created *)
    This might be slightly different in Delphi though.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Cheers for the tips n that dudes, will experiment now....after i get a beer chilled from the fridge

    PS: Corona, if im writing a messagebox, with a normal caption, how do iget the string in the middile?

    ShowMessage('There are' + S + ' contacts available in the Address Book)
    Last edited by Madboy; Jun 6th, 2004 at 05:30 AM.

  10. #10
    Lively Member CORONA BEER's Avatar
    Join Date
    May 2004
    Location
    THE INTERNETS!
    Posts
    82
    ShowMessage('There are '+ S +' contacts available in the Address Book')

    use the
    {$I-} - Turns off file IO error checking for example.

    and

    {$I+} - Turns it back on.

    like visualAd said i forgot bout thT.
    Last edited by CORONA BEER; Jun 6th, 2004 at 09:31 AM.

  11. #11

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Cheers VisualAd, Corona

    Last problem, i hope How do you count the number of items in a Combobox? I have:

    procedure TfrmMain.mnuViewStatisticsClick(Sender: TObject);
    var
    S:String;
    i:Integer;
    begin

    For I:=0 To cboNameList.Items.Count - 1 do
    S:= S + cboNameList.Items.Text[i] + ' ';
    ShowMessage('There are '+ S +' contacts available in the Address Book')
    end;

    end.


    But that just shows the text in the combo box, not the amount of items

  12. #12

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Oh, and incase you think the problems were solved, they wernt

  13. #13

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    OK

    I have now sucesfully got a working array
    I can now add new contacts, delete them and show them when i load my program up. There is one problem though, in which if i add a new contact, then click File > New again, it errors. I dont know why it errors (maybe i need to close the file, if so how?). If thats not the problem, maybe i can shove an error handle in there somewhere.

    See that? I would never of come close to making something like this in VB, Delphi seems to take p**s out of VB, hahahaha.


    So ill try that method, first though, how do you close a file? The reason i ask is, i mentioned that i can create a new contact with no problems, but trying to add a new one straight after errors. Well if i add a contact, close the program then re-open, i can add a contact with no problems. So i must be getting an overflow error trying to write to the file whilst it is already open, thats my theory anyway.

    Any other suggestions appreciated

    EDIT: Those error handles stated above, thats just for IO operations right? I only want to know how to do a simlilar thing to On Error Resume Next , nothing else, forget the files part, this is a question irrelated to IO
    Last edited by Madboy; Jun 6th, 2004 at 08:52 AM.

  14. #14
    Lively Member CORONA BEER's Avatar
    Join Date
    May 2004
    Location
    THE INTERNETS!
    Posts
    82
    //---
    Assignfile(QFile, QFileName);
    if FileExists(QfileName) then begin
    reset(QFile);
    FSize:=FileSize(QFile);
    if FSize>0 then begin
    for n:=0 to Fsize-1 do
    begin
    seek(Qfile,n);
    Read(QFile,Entries);
    cboNameList.Items.add(Entries.FullName);
    end;
    cboNameList.ItemIndex:=0;
    SeekRecord;
    CloseFile(QFile); // added this
    end
    end
    else begin
    //---

  15. #15

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    umm....

    that eliminates one problem, but creates another.

    Before if i tried to add a new contact, then another it errored as soon as i clicked File > New. Now this doesnt happen, it shows my form to add the new contact, but when i click OK it errors, maybe i need to open the file when the form loads? If not im confused

    Cheers

  16. #16

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    All sorted, hahaha. First program in Delphi done, took a day. Would of took a month in VB, and its complete, the Address Book.

    Cheers everyone, well VisualAd, Corona

    Still didnt get the error handling to work, but ill find it out soon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width