I am using a SaveDialog and an OpenDialog. When I save or open Files it
works Fine. However When I Open the Dialog (either one) and then press the
Cancel button, I get an Access Violation in my EXE File..
What is wrong??
TIA
Wayne
--
Can you show us the code you are using?
procedure TStringGridForm.SaveBtnClick(Sender: TObject);
var
F : TStringlist;
I : Integer;
begin
if saveDialog1.Execute then
F := TStringList.Create;
try
F.Add(IntToStr(StringGrid1.Rowcount));
F.Add(IntToStr(StringGrid1.ColCount));
for I := 0 to StringGrid1.RowCount - 1 do
F.Add(StringGrid1.Rows[I].CommaText);
F.SaveToFile(SaveDialog1.FileName);
finally
F.Free;
end;
end;
procedure TStringGridForm.OpenBtnClick(Sender: TObject);
var
F : TStringlist;
I : Integer;
begin
if OpenDialog1.Execute then
F := TStringList.Create;
try
F.LoadFromFile(OpenDialog1.FileName);
StringGrid1.RowCount := StrToInt(F[0]);
StringGrid1.ColCount := StrToInt(F[1]);
for I := 0 to (StringGrid1.RowCount - 1) do
StringGrid1.Rows[I].CommaText := F[I + 2];
finally
F.Free;
end;
StringGrid1.ColWidths[0] := 1;
StringGrid1.ColWidths[1] := 15;
StringGrid1.FixedColor := clYellow;
end;
Thanks
Wayne
Mauro Patino <M-Pa...@govst.edu> wrote in message
news:3AF1C30B...@govst.edu...
I think you should write :
if OpenDialog1.Execute then
begin
try
F := TStringList.Create;
F.LoadFromFile(OpenDialog1.FileName);
StringGrid1.RowCount := StrToInt(F[0]);
StringGrid1.ColCount := StrToInt(F[1]);
for I := 0 to (StringGrid1.RowCount - 1) do
StringGrid1.Rows[I].CommaText := F[I + 2];
finally
F.Free;
end;
end;
> No Problem, Here is.
>
> procedure TStringGridForm.SaveBtnClick(Sender: TObject);
> var
> F : TStringlist;
> I : Integer;
> begin
> if saveDialog1.Execute then
> F := TStringList.Create;
> try
> F.Add(IntToStr(StringGrid1.Rowcount));
> F.Add(IntToStr(StringGrid1.ColCount));
> for I := 0 to StringGrid1.RowCount - 1 do
> F.Add(StringGrid1.Rows[I].CommaText);
> F.SaveToFile(SaveDialog1.FileName);
The code after
if saveDialog1.Execute then
F := TStringList.Create;
always gets executed so when you hit cancel your stringlist does not get
created but you still try to use it's methods. That's why you get access
violations because you are trying to use an object that has not been allocated.
You can fix it by blocking the entire code after your 'if' statement.
procedure TForm1.Button1Click(Sender: TObject);
var
F : TStringlist;
I : Integer;
begin
if saveDialog1.Execute then
begin
{--Execute only on save--}
F := TStringList.Create;
try
F.Add(IntToStr(StringGrid1.Rowcount));
F.Add(IntToStr(StringGrid1.ColCount));
for I := 0 to StringGrid1.RowCount - 1 do
F.Add(StringGrid1.Rows[I].CommaText);
F.SaveToFile(SaveDialog1.FileName);
finally
F.Free;
end;
{------------------------}
end; {if saveDialog1.Execute then}
end;
Same applies to the other code.