Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Access Violation - OpenDialog & SaveDialg

559 views
Skip to first unread message

Wayne

unread,
May 3, 2001, 4:20:04 PM5/3/01
to
Hi All,

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


--


Mauro Patino

unread,
May 3, 2001, 4:43:55 PM5/3/01
to
Wayne wrote:

Can you show us the code you are using?

Wayne

unread,
May 3, 2001, 5:52:39 PM5/3/01
to
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);
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...

Francesco Savastano

unread,
May 3, 2001, 5:48:55 PM5/3/01
to

Wayne <wa...@mweb.co.za> wrote in message 3af1d37c_1@dnews...
When you click cancel the code will try to free the stringlist but if you
see well it has not been created because the "if opendialog1.execute" test
has not been satisfied !!

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;


Mauro Patino

unread,
May 4, 2001, 9:37:12 AM5/4/01
to
Wayne wrote:

> 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.


0 new messages