Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:46:27+00:00 2026-05-27T20:46:27+00:00

First part of the code works OK while the second (commented) does not. It

  • 0

First part of the code works OK while the second (commented) does not.
It overwrites my A1 file although it should write to A2.

procedure TForm1.AdvGlowButton12Click(Sender: TObject);
var
  i,j: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
begin
  if (cxRadiogroup3.ItemIndex and cxRadiogroup2.ItemIndex) = 0 then begin
    ApplicationPath:= ExtractFileDir(Application.ExeName);
    Seznam:= TStringList.Create;
    try
      for i:=0 to advStringGrid2.ColCount-1 do
        Seznam.AddStrings(advStringGrid2.Cols [i]);
      for i:=0 to advStringGrid2.rowCount-1 do
        Seznam.AddStrings(advStringGrid2.rows [j]);
      Seznam.SaveToFile(ApplicationPath+'\A1.txt');
    finally
      seznam.free;
    end;
  end ;
  //if cxRadiogroup3.ItemIndex = 1 and cxRadiogroup2.ItemIndex = 0 then begin
  //  ApplicationPath:= ExtractFileDir(Application.ExeName);
  //  Seznam:= TStringList.Create;
  //  try
  //    for i:=0 to advStringGrid2.ColCount-1 do
  //      Seznam.AddStrings(advStringGrid2.Cols [i]);
  //    for i:=0 to advStringGrid2.rowCount-1 do
  //      Seznam.AddStrings(advStringGrid2.rows [j]);
  //    Seznam.SaveToFile(ApplicationPath+'\A2.txt');
  //  finally
  //    seznam.free;
  //  end ;
  //end
end;

What am I doing wrong ?
Also why is the stringgrid giving listindex out of bounds when I try to load into it contents from an empty text file? If I save empty stringgrid to that file,later ,though it has nothing in the file,it does not complain? Strange…

This is how I load A1 and A2 into the stringgrid.

procedure TForm1.cxRadioGroup2Click(Sender: TObject);
Var
  I,j,k: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
begin
  case cxradioGroup2.ItemIndex of
    0: begin
         if cxradioGroup3.ItemIndex = 0 then begin
           Seznam:= TStringList.Create;
           AdvStringgrid2.ClearAll;
           try
             Seznam.LoadFromFile('A1.txt');
             k:= 0;
             for i:=0 to advStringGrid2.ColCount-1 do
               for j:=0 to advStringGrid2.RowCount-1 do begin
                 advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
                 Inc(k);
               end;
           finally
             seznam.free;
           end;
         end;
         if cxradioGroup3.ItemIndex = 1 then begin
           Seznam:= TStringList.Create;
           AdvStringgrid2.ClearAll;
           try
             Seznam.LoadFromFile('A2.txt');
             k:=0;
             for i:=0 to advStringGrid2.ColCount-1 do
               for j:=0 to advStringGrid2.RowCount-1 do begin
                 advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
                 Inc(k);
               end;
           finally
             seznam.free;
           end;
         end;
       end;
  end;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:46:28+00:00Added an answer on May 27, 2026 at 8:46 pm

    here is an old tipp from SwissDelphiCenter that could help you

    // Save StringGrid1 to 'c:\temp.txt':
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SaveStringGrid(StringGrid1, 'c:\temp.txt');
    end;
    
    // Load StringGrid1 from 'c:\temp.txt':
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      LoadStringGrid(StringGrid1, 'c:\temp.txt');
    end;
    
    // Save a TStringGrid to a file
    
    procedure SaveStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
    var
      f:    TextFile;
      i, k: Integer;
    begin
      AssignFile(f, FileName);
      Rewrite(f);
      with StringGrid do
      begin
        // Write number of Columns/Rows
        Writeln(f, ColCount);
        Writeln(f, RowCount);
        // loop through cells
        for i := 0 to ColCount - 1 do
          for k := 0 to RowCount - 1 do
            Writeln(F, Cells[i, k]);
      end;
      CloseFile(F);
    end;
    
    // Load a TStringGrid from a file
    
    procedure LoadStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
    var
      f:          TextFile;
      iTmp, i, k: Integer;
      strTemp:    String;
    begin
      AssignFile(f, FileName);
      Reset(f);
      with StringGrid do
      begin
        // Get number of columns
        Readln(f, iTmp);
        ColCount := iTmp;
        // Get number of rows
        Readln(f, iTmp);
        RowCount := iTmp;
        // loop through cells & fill in values
        for i := 0 to ColCount - 1 do
          for k := 0 to RowCount - 1 do
          begin
            Readln(f, strTemp);
            Cells[i, k] := strTemp;
          end;
      end;
      CloseFile(f);
    end;
    

    I’m trying to understand your code and tried him as good as it is possible for me to rewrite. (it’s not tested)

    procedure TForm1.AdvGlowButton12Click(Sender: TObject);
    var
      i, j: Integer;
      Seznam: TStrings;
      ApplicationPath: string;
      fileName: string;
      line: string;
    begin
      if (cxRadiogroup2.ItemIndex = 0) then begin
        if (cxRadiogroup3.ItemIndex = 0) then
          fileName:= 'A1.txt'
        else
          fileName:= 'A2.txt'
    
        ApplicationPath:= ExtractFileDir(Application.ExeName);
        Seznam:= TStringList.Create;
        try
          for k:=0 to advStringGrid2.RowCount-1 do begin
            line:= '';
            for i:=0 to advStringGrid2.ColCount-1 do
              line = line + '|' + advStringGrid2.Cells[i, k];
            Seznam.AddStrings(line);
          end;
          Seznam.SaveToFile(ApplicationPath + '\' + fileName);
        finally
          seznam.Free;
        end;
      end;
    end;
    
    procedure TForm1.cxRadioGroup2Click(Sender: TObject);
    var
      splitList: TStringList;
      i, j: Integer;
      Seznam: TStrings;
      ApplicationPath: string;
      fileName: string;
      line: string;
      sepIndex: integer;
    begin
      if (cxRadiogroup2.ItemIndex = 0) then begin
        if (cxRadiogroup3.ItemIndex = 0) then
          fileName:= 'A1.txt'
        else
          fileName:= 'A2.txt'
    
        AdvStringgrid2.ClearAll; // don't know what this does
    
        ApplicationPath:= ExtractFileDir(Application.ExeName);
        Seznam:= TStringList.Create;
        try
          Seznam.LoadFromFile(fileName);
          advstringGrid2.RowCount:= Seznam.Count;
          splitList:= TStringList.Create;
          for i:=0 to Seznam.Count-1 do begin
            line:= Seznam.Strings [i];
            Split('|', line, splitList);
            advStringGrid2.ColCount:= Max(advStringGrid2.ColCount, splitList.Count);
            for k:=0 to splitList.Count-1 do
              advStringGrid2.Cells[i, k]:= splitList[k];
          end; 
        finally
          splitList.Free;
          seznam.Free;
        end;
      end;
    end;
    
    procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings);
    begin
       Assert(Assigned(Strings));
       Strings.Clear;
       Strings.Delimiter:= Delimiter;
       Strings.DelimitedText:= Input;
    end;
    

    hope that helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the first part of my controller code: public class ControlMController : Controller
This is actually a two part question. First,does the HttpContext.Current correspond to the current
I am trying to read some objects from a file. The code works fine
i have this part of a code and i cant understund why the second
I am using in the first part of my program On Error GoTo start
I have a couple of scripts for which the first part of them looks
I need a regular expression to basically get the first part of a string,
I just recorded some song and cut it into two parts. The first part
This is a two-part question: First, I am interested to know what the best
I've been developing my first iPhone app part-time and would like to start using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.