 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Neil Howard Guest
|
Posted: Tue Mar 02, 2004 11:25 pm Post subject: Adding data to dynamically created controls |
|
|
I have some code to dynamicallycreate tabsheets on demand, each with a
list view in it
How do add data from an edit box into the listview in the selected
tabsheet
procedure TForm1.NewTab();
var
PageControl : TPageControl;
TabSheet : TTabSheet;
InsertItem : TlistItem;
begin
// Create a page and associate it with the PageControl
TabSheet := TTabSheet.Create(Self);
Tabsheet.Name := 'Tab' + inttostr (tabcount +1);
TabSheet.Caption := 'Tab ' + inttostr (tabcount +1);
TabSheet.PageControl := PageControl1;
tabcount := tabcount +1;
with Tlistview.Create(Self) do
begin
OnClick := TestMethod; // Assign an event handle
Parent := TabSheet;
viewstyle := vsreport;
Columns.Add.caption := 'User';
columns.Items[0].Width := 70;
Columns.Add.caption := 'IP';
Columns.Items[1].Width := 95;
Columns.Add.caption := 'Date';
Columns.Items[2].Width := 105;
Columns.Add.caption := 'Comments';
Columns.Items[3].Width := 150;
end;
|
|
| Back to top |
|
 |
Rob Kennedy Guest
|
Posted: Wed Mar 03, 2004 5:57 am Post subject: Re: Adding data to dynamically created controls |
|
|
Neil Howard wrote:
| Quote: | I have some code to dynamicallycreate tabsheets on demand, each with a
list view in it
How do add data from an edit box into the listview in the selected
tabsheet
|
You can make things easier on yourself by declaring a new TTabSheet
descendant, which has all the properties you need.
type
TListTabSheet = class(TTabSheet)
private
FListView: TListView;
public
constructor Create(AOwner: TComponent; const APageControl:
TPageControl; const ACaption: TCaption; const ListViewClick:
TNotifyEvent); reintroduce;
property ListView: TListView read FListView;
end;
constructor TListTabSheet.Create;
begin
inherited Create(AOwner);
// Don't bother setting this new component's Name unless you
// actually need to refer to it by name later in your code,
// like via FindComponent.
Caption := ACaption;
PageControl := APageControl;
FListView := TListView.Create(Self);
FListView.Parent := Self;
FListView.ViewStyle := vsReport;
FListView.OnClick := ListViewClick;
with FListView.Columns.Add do begin
Caption := 'User';
Width := 70;
end;
with FListView.Columns.Add do begin
Caption := 'IP';
Width := 95;
end;
with FListView.Columns.Add do begin
Caption := 'Date';
Width := 105;
end;
with FListView.Columns.Add do begin
Caption := 'Commets';
Width := 150;
end;
end;
The TForm1.NewTab method that you posted would be turned into this:
var
TabSheet: TTabSheet;
begin
Inc(TabCount);
try
TabSheet := TListTabSheet.Create(Self, PageControl1, Format('Tab
%d', [TabCount]), TestMethod);
except
Dec(TabCount);
raise;
end;
end;
If you need to further modify the list view after the sheet has been
created, then get a reference to the tab sheet and type cast it to
TListTabSheet. Then you can use the object's ListView property.
var
TabSheet: TListTabSheet;
TabSheet := PageControl1.Pages[3] as TListTabSheet;
TabSheet.ListView.Items.Add.Caption := 'Item';
Rather than keeping a separate TabCount variable, which you need to
remember to maintain yourself, would it make sense for you to use the
page control's PageCount property instead?
--
Rob
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|