BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Adding data to dynamically created controls

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Neil Howard
Guest





PostPosted: Tue Mar 02, 2004 11:25 pm    Post subject: Adding data to dynamically created controls Reply with 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

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





PostPosted: Wed Mar 03, 2004 5:57 am    Post subject: Re: Adding data to dynamically created controls Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.