释放combobox的item.data属性时出现的问题(100分)

  • 主题发起人 主题发起人 sunshine1750
  • 开始时间 开始时间
S

sunshine1750

Unregistered / Unconfirmed
GUEST, unregistred user!
想在这个控件的析构事件里,释放ITEM里保存的对象时出错
请帮偶看看,先谢过了

————————————————————————————————
unit ComboboxExRm;

interface

uses
SysUtils, Classes, Windows, Messages, Controls, StdCtrls, ExtCtrls;

type
TSampObj = class
str: string;
end;

TComboboxExRM = class(TCustomComboBox)
private
protected
public
procedure AddItemRm (const ACaption, AID: string);virtual;
function GetCurrCaption: string;
function GetCurrID: string;
destructor Destroy; override;
published
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('RMComponent', [TComboboxExRM]);
end;

{ TComboboxExRM }

destructor TComboboxExRM.Destroy;
var
Cnt: integer;
begin
for Cnt := self.Items.Count - 1 downto 0 do
if Assigned(Items.Objects[Cnt]) then
Items.Objects[Cnt].Free;
inherited;
end;

procedure TComboboxExRM.AddItemRm(const ACaption, AID: string);
var
SampObj: TSampObj;
begin
SampObj := TSampObj.Create;
SampObj.str := AID;
AddItem(ACaption, SampObj);
end;

function TComboboxExRM.GetCurrCaption: string;
begin
Result := Text;
end;

function TComboboxExRM.GetCurrID: string;
begin
result := TSampObj(Items.Objects[ItemIndex]).str;
end;

end.

 
改為下面的:
for Cnt := self.Items.Count - 1 downto 0 do
if Assigned(Items.Objects[Cnt]) then
begin
Items.Objects[Cnt].Free;
Items.Objects[Cnt]:=nil; <-------加入..
end;
inherited;
 
to: kouchun
与你说的没关系,还没到那就错了

destructor TComboboxExRM.Destroy;
var
Cnt: integer;
begin
for Cnt := self.Items.Count - 1 downto 0 do //执行到这里,获取Count属性时出错
if Assigned(Items.Objects[Cnt]) then
Items.Objects[Cnt].Free;
inherited;
end;

??
 
自已顶一下
 
代码应该没错,把报错的详细信息贴出来看看。
 
楼主别绕弯了,看看这吧:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2641049
 

Similar threads

I
回复
0
查看
649
import
I
I
回复
0
查看
686
import
I
I
回复
0
查看
588
import
I
I
回复
0
查看
698
import
I
后退
顶部