PDA

View Full Version : مشکل در حذف لیبل در ران تایم



seyed_farid
شنبه 01 اردیبهشت 1397, 02:45 صبح
با سلام خدمت متخصصان عزیز

من برای حذف لیبل در ران تابم از دستور زیر استفاده میکنم.


procedure TMain.Button10Click(Sender: TObject);

var i : Integer;
lbl : tlabel;
begin
Memo1.Lines.add(IntToStr(Panel3.ControlCount)+' Controls found' );

for i := Panel3.ControlCount -1 downto 0 do
begin
if Panel3.Controls[i] is TLabel then
begin
lbl := Panel3.Controls[i] as TLabel;
lbl.Parent := nil;

//FreeAndNil(lbl);

// TWinControl((Panel3.Controls[i]).parent).
// RemoveControl((Panel3.Controls[i]));
// (Panel3.Controls[i]).Parent := nil;
// (Panel3.Controls[i]).free;
end;

end;
end;





پیام خطای زیر میآید. مشکل چیست؟

Access violation at address 0041EA72 in module 'AIM.exe'. Read of address 00000008

:تشویق::تشویق::تشویق::تشویق:

برای حذف و اضافه نمودن لیبل پیشنهاد شما چیست؟

Mask
یک شنبه 02 اردیبهشت 1397, 15:33 عصر
بهتره به این شکل عمل کنین:

procedure TForm1.Button1Click(Sender: TObject);var
i: Integer;
begin
Memo1.Lines.add(IntToStr(Panel3.ControlCount) + ' Controls found');


for i := Panel3.ControlCount - 1 downto 0 do
if Panel3.Controls[i] is TLabel then
Panel3.Controls[i].Free;
end;

seyed_farid
یک شنبه 02 اردیبهشت 1397, 21:33 عصر
ممنون از پاسختون
ولی با دستور Free هم همون خطا رو میده. :افسرده:

Mask
یک شنبه 02 اردیبهشت 1397, 23:54 عصر
کدی که قرار دادم این ارور رو میده؟
من تست کردم مشکلی نداشت.
اگه کد رو تغییر دادید و به این مشکل میخورید، کدتون رو اینجا قرار بدید.

seyed_farid
دوشنبه 03 اردیبهشت 1397, 18:13 عصر
با سلام . تشکر

مشکل حل شد.
مورد این بود که بلافاصله بعد از ساخت لیبل ، دستور معرفی Parent رو میدادم. بعد از کلی کار با کشیدن این دستور به آخر ، مشکل برطرف شد .

این هم نمونه کد مورد استفاده من :
قرمز موقعیت دستور که موجب بروز خطا می شد و سبز موقعیت جدید که درست کار میکنه.


Procedure TMain.LoadComponents(AFileName: string);
var
S: string;
I: Integer;
Index: Integer;
IniFile: TIniFile;
Sections: TStringList;
Lab : TLabel;
Sh : TShape;
begin
IniFile := TIniFile.Create(AFileName);
try
Sections := TStringList.Create;
try
IniFile.ReadSections(Sections);
for I := 0 to Sections.Count - 1 do
begin
S := Sections[I];
if s = 'Panel3' then
Main.Panel3.Height := IniFile.ReadInteger(s, 'Height' , 90)
else
if LeftStr(S,8)='NewLabel' then
begin
Lab := TLabel.Create(self);
//Lab.Parent := Main.Panel3;
lab.Name := s;
Lab.Left := IniFile.ReadInteger(s, 'Left' , 20);
Lab.Top := IniFile.ReadInteger(s, 'Top' , 20);
Lab.Height := IniFile.ReadInteger(s, 'Height' , 20);
Lab.Width := IniFile.ReadInteger(s, 'Width' , 20);
Lab.Font.Size := IniFile.ReadInteger(s, 'FontSize' , 12);
Lab.Font.Color := StringToColor(IniFile.ReadString(s, 'Color' , '0'));
Lab.Caption := IniFile.ReadString(s, 'Caption' , '');
Lab.Hint := IniFile.ReadString(s, 'Hint' , '');
Lab.Font.Style:= [fsBold];
Lab.Transparent := True;
Lab.ShowHint := True;
Lab.AutoSize := True;
Lab.Parent := Main.Panel3;
end
else
if LeftStr(S,8)='NewShape' then
begin
Sh := TShape.Create(main.Panel3);
Sh.Name := s;
SetShape(Sh, IniFile.ReadString(s, 'Shape' , 'Solid'));
SetFillStyle(Sh, IniFile.ReadString(s, 'FillStyle' , 'Solid'));
Sh.Left := IniFile.ReadInteger(s, 'Left' , 20);
Sh.Top := IniFile.ReadInteger(s, 'Top' , 20);
Sh.Height := IniFile.ReadInteger(s, 'Height' , 20);
Sh.Width := IniFile.ReadInteger(s, 'Width' , 20);
Sh.Hint := IniFile.ReadString(s, 'Hint' , '');
Sh.ShowHint := True;
Sh.Pen.Color := StringToColor(IniFile.ReadString(s, 'PenColor' , '0'));
Sh.Brush.Color := StringToColor(IniFile.ReadString(s, 'BrushColor' , '0'));
Sh.pen.Width := IniFile.ReadInteger(s, 'FrameWidth' , 12);
Sh.Parent := Main.Panel3;
end;
end;
finally
Sections.Free;
end;
finally
IniFile.Free;
end;
end;


در پست اول هم میبینید که چهار مدل برا حذف لیبل نوشتم که همه شون هم با شرایط جدید کار میکنه.