#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Loose fileds values


    I have an application where user selects different files and then I have to loads values stored in those files in different windows.
    I tried to clone then window where I have to show values as many times as the number of files and then to load values.
    This is what happens.

    Example

    The user select two files. I show a new window and load values from the first file. Then I clone the window and load values from the second file.
    As I move the first active window I see the values of one file in both two windows. If I select the second window the values of the second file are loaded. The problem is when I come back to the first window. Both windows shows values of the second file.

    I also tried to not clone window at runtime but to duplicate the window before building application so I have to different windows to use to loads values. I have the same problem when focusing windows.

    How could I solve this problem?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    What do you mean when you say you clone the window?

    Simply "create" a form for every file you want to read values from. You can define a new constructor for the form, passing it the filepath and filename you want to read from.

    so if the form that you want to create to read the values from the file is called "ReadValuesForm" then after you define the form, add a new constructor and a private string field to hold the path/filename
    Code:
    TReadValuesForm = class(TForm)
      ..
    public
      ...
      constructor CreateWithFileName(AOwner: TComponent; const File_Name: String);
    private
      ...
      FileName: String;
    end;
    then write your new constructor
    Code:
    constructor TReadValuesForm.CreateWithFileName(AOwner: TComponent; const File_Name: String);
    begin
       inherited Create(AOwner);
       FileName := File_Name;
    end;
    Now, choose the appropriate method for your form, where you can read the file that is identified by the form's variable FileName.
    Code:
    procedure TReadValuesForm.<YourSelectedMethod>(Sender: TObject);
    var
      SL: TStringList;
      I: Integer;
    begin
      //Read in the file from file named FileName
      //if the file is a text file, you can read it into a Stringlist.
      SL := TStringList.Create;
      try
        SL.LoadFromFile(FileName);
        for I := 0 to SL.Count - 1 do
        begin
           //process your file line by line.
           //Current Line is SL[I];
        end;
      finally
        SL.Free;
      end;
    end;
    then from the calling form, where you select the files, create the form with your constructor, passing it the file name (also, add the above's unit to the uses clause).
    Code:
    uses ..., <ReadFromValuesUnitName>;
    
    ...
    
    procedure TForm1....
    var
      I: Integer;
      ReadValuesForm: TReadValuesForm;
    begin
      for I := 0 to <NumberOfFilesSelected - 1> do
      begin
        ReadValuesForm:= TReadValuesForm.CreateWithFileName(Self, <TheFileName>);
        ReadValuesForm.Show;
      end;
    end;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0
    It was my fault. I was using OnActivate event to load values.
    This way every time I clicked on a form, the procedure was called loading the last file.

    I never used constructor but it seems to me your solution is better than mine. I'll try to use it.

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    Originally Posted by Petabyt3
    It was my fault. I was using OnActivate event to load values.
    This way every time I clicked on a form, the procedure was called loading the last file.

    I never used constructor but it seems to me your solution is better than mine. I'll try to use it.

    Thanks
    Just one more note, remember to remove this 2nd form from the Auto Create List - With the project loaded in the IDE, select Project|Options menu item, select Forms from the Left pane, and move the form from the Auto Create Forms List to the Available Forms List.

IMN logo majestic logo threadwatch logo seochat tools logo