往自定义控件中的Tlistview控件添加项目时,如何操纵Tlistitem?(50分)

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

samuelzhu

Unregistered / Unconfirmed
GUEST, unregistred user!
自定义控件中有一Tlistview控件,希望在生成控件时,往TlistView加一些项目,
但调用Tlistitem时,出错:control '' has no parent window
请问是何原因? 如何往里面加项目?

程序附后

unit test;

interface

uses
Windows, Messages, Classes, Graphics, ComCtrls, Controls, StdCtrls, ExtCtrls;

type
Ttest = class(TcustomControl)
private
ListView1: TListView;
protected
procedure FillInListView;
public
Constructor Create(Aowner: Tcomponent); Override;
Destructor Destroy; Override;
published
end;

procedure Register;

implementation

Constructor Ttest.Create(Aowner: Tcomponent);
begin
Inherited Create(Aowner);
ListView1:=Tlistview.Create(self);
with ListView1 do begin
Parent :=self;
ViewStyle := vsReport;
Top :=3;
Left :=3;
height:=200;
end;
FillInListView;
end;

Destructor Ttest.Destroy;
Begin
ListView1.Free ;
Inherited Destroy;
End;

procedure Ttest.FillInListView;
var ListItem: TListItem;
begin
ListItem := ListView1.Items.Add;//出错信息为:control '' has no parent window
Listitem.Caption :='test';
end;

procedure Register;
begin
RegisterComponents('Samples', [Ttest]);
end;

end.
 
procedure TForm1.opentxt(caption:string);
var
richedit:Trichedit;
tabsheet:Ttabsheet;
begin
tabsheet:=Ttabsheet.Create(nil);
tabsheet.Name:='tabsheet'+inttostr(i);
tabsheet.PageControl:=PageControl1;
tabsheet.Caption:=extractfilename(caption);
tabsheet.Hint:=caption;
tabsheet.ShowHint:=false;
pageControl1.ShowHint:=false;
richedit:=trichedit.Create(nil);
richedit.Name:='richedit'+inttostr(i);
richedit.Parent:=tabsheet;
richedit.ScrollBars:=ssBoth;
richedit.HideScrollBars :=false;
richedit.Align:=alClient;
richedit.PopupMenu:=PopupMenu1;
richedit.PopupMenu.Items[5].Checked:=false;
richedit.Font.Name:='宋体';
richedit.Font.Size:=12;
richedit.Lines.LoadFromFile(Filelistbox1.FileName);

PageControl1.ActivePage:=tabsheet;
i:=i+1;


end;

 
>>procedure Ttest.FillInListView;
>>var ListItem: TListItem;
>>begin
>> ListItem := ListView1.Items.Add;//出错信息为:control '' has no parent window
>> Listitem.Caption :='test';
>>end;
你的ListView还没有columns,怎么加ListItem.
Demo:
var
NewColumn: TListColumn;
ListItem: TListItem;
ListView: TListView;
begin
ListView := TListView.Create(Self);
with ListView do
begin
Parent := Self;
Align := alClient;
ViewStyle := vsReport;
NewColumn := Columns.Add;
NewColumn.Caption := 'First';
ListItem := Items.Add;
ListItem.Caption := 'Second';
ListItem.SubItems.Add('Third');
end;
end;
 
to antic_ant: 你怎么乱贴贴子?
to bluerain: 我照你的程序加上了columns,可还是出现同样的错误,
在普通应用程序中,上述程序没有问题,但移植到自定义控件中就出错了,不知为什么.
 
我将其故障现象再详细第描述一下:

1. 在控件程序中无论是否加入下面的两行,均能compile,并生成控件;
ListItem := Items.Add;
Listitem.Caption :='test';
2.当不加入上述两行时,控件能被正常的调用(但不加入项目不是我所需求的);
当加入上述两行时,应用程序中调用该控件时出现错误: control '' has no parent window
3.按下述步骤操作,可得到想要的结果:
a. 注释掉上述两行,生成控件;
b. 在project1中调用该控件,此时得到的控件没有添加项目;将project1存盘,退出;
c. 在控件源码中,将上述两行的注释去掉,编译生成控件;
d. 重新打开project1,发现控件中已加入了'test'项目;

综合上述症状, 我认为问题出在listitem没有父窗口上:
在正常调用时,此时本自定义控件还未生成,所以listitem没有父窗口,因而出错,
但按上述3.d打开已存在控件时,由于该控件已存在,所以listitem有父窗口,就能正常工作.

以上是我的猜测,不知正确如否.

但我还是没有找到根除问题的办法,请大虾门指点.
 
我知道了:不能在create段执行添加项目的操作.
可在paint添加.
 
后退
顶部