Thread View: borland.public.cppbuilder.nativeapi
11 messages
11 total messages
Started by JohnC
Thu, 31 Jan 2008 13:12
Create VCL Control from within an Object
Author: JohnC
Date: Thu, 31 Jan 2008 13:12
Date: Thu, 31 Jan 2008 13:12
13 lines
1689 bytes
1689 bytes
I have some fairly complicated arrangements of VCL controls on some forms
and I have been trying (unsuccessfully) to create an object which I can add
to a form to automatically add the items along with the code that I need for
their interaction to the form when the form is created. I would like to
hide all the details of their operation and interaction since they would be
common for all uses. I don't especially need to see the objects during
design time - I was planning on just putting a panel where the items would
go.
Is this possible?
If it is, how do I do it? If not, is there any other way I can do this?
Thanks,
John
Re: Create VCL Control from within an Object
Author: Asger Joergensen
Date: Thu, 31 Jan 2008 14:09
Date: Thu, 31 Jan 2008 14:09
44 lines
2356 bytes
2356 bytes
Hi JohnC
JohnC says:
> I have some fairly complicated arrangements of VCL controls on some forms
> and I have been trying (unsuccessfully) to create an object which I can add
> to a form to automatically add the items along with the code that I need for
> their interaction to the form when the form is created. I would like to
> hide all the details of their operation and interaction since they would be
> common for all uses. I don't especially need to see the objects during
> design time - I was planning on just putting a panel where the items would
> go.
>
> Is this possible?
If I understand You correctly, that shouldn't be a problem, but
please explain what kind of object, what kind of items.
You can create VCL components by using the keyword new, but
if You want them to be visible You have to provide a Parent.
void __fastcall TYourClass::ButtonClick(TObject *Sender)
{
}
//-----------------------------------------------------
TButton *Button = new TButton(NULL); //You must delete
or
TButton *Button = new TButton(Form1); // deleted automaticly
Button->SetBounds(10,10, 150, 23);
Button->Parent = Panel2;
Button->OnClick = ButtonClick;
If You provide the owner the component is deletet by the owner
but if You don't need it anymore You can still delete it You self
like You have to if You dont provide the owner.
P.s. questions like this are better asked in:
borland.public.cppbuilder.vcl.components.using
Kind regards
Asger
Re: Create VCL Control from within an Object
Author: JohnC
Date: Thu, 31 Jan 2008 14:30
Date: Thu, 31 Jan 2008 14:30
19 lines
1864 bytes
1864 bytes
Thanks Asger: The one I am working on right now has a TStringGrid and several TShape which I need to allocate into an array. I thought I was giving them a Parent, but I'll fool around with that a little more. Since nothing was showing up, I thought that perhaps there was something special about the controls that they just needed to be manually placed on a form. If you think I should move this over to the other group, just say so and I will do so. I don't normally access that group. Thanks, John ----- Original Message ----- From: "Asger Joergensen" <junk@Asger-P.dk> > If I understand You correctly, that shouldn't be a problem, but > please explain what kind of object, what kind of items. > > You can create VCL components by using the keyword new, but > if You want them to be visible You have to provide a Parent.
Re: Create VCL Control from within an Object
Author: JohnC
Date: Thu, 31 Jan 2008 15:14
Date: Thu, 31 Jan 2008 15:14
55 lines
2602 bytes
2602 bytes
OK, Here are the details of what I'm doing:
class TJMCColorTable
{
public:
TJMCColorTable(TWinControl * InParent);
...
and more stuff.
//---------------------------------------------------------------------------
TJMCColorTable::TJMCColorTable(TWinControl * InParent)
{
Parent = InParent;
NmbColorsShown = 6;
InitTable();
Table->Repaint();
}
//---------------------------------------------------------------------------
void TJMCColorTable::InitTable()
{
Table = new TStringGrid(Parent);
Table->Left = 0;
Table->Top = 0;
Table->Width = 443;
Table->Height = 181;
Table->ColCount = 6;
Table->RowCount = NmbColorsShown+1;
Table->DefaultColWidth = 64;
Table->DefaultRowHeight = 24;
Table->ScrollBars = ssBoth;
InitGridTitlesAndWidths();
InitGrid(6);
Table->Visible;
}
//---------------------------------------------------------------------------
void TJMCColorTable::InitGrid(int N)
{
NumberColors = N;
Table->RowCount = N + 1;
for(int i=1;i<=NumberColors;i++)
Table->Cells[0][i] = IntToStr(i);
}
//---------------------------------------------------------------------------
And here is the call from the main:
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
ColorTable = new TJMCColorTable(MainForm);
}
//---------------------------------------------------------------------------
It runs, but nothing shows up.
I'm probably doing something stupid.
In place of the "MainForm" as a parameter to the constructor, I've also
tried adding another control to the form and passing it's parent as the
parameter. That didn't work either.
John
Re: Create VCL Control from within an Object
Author: JohnC
Date: Thu, 31 Jan 2008 15:20
Date: Thu, 31 Jan 2008 15:20
5 lines
1078 bytes
1078 bytes
OK, I got it. I added and explicit set of the Parent of the control (not just in the constructor) and it worked. Thanks for your time, John
Re: Create VCL Control from within an Object
Author: Asger Joergensen
Date: Thu, 31 Jan 2008 18:55
Date: Thu, 31 Jan 2008 18:55
40 lines
2221 bytes
2221 bytes
Hi JohnC
JohnC says:
> Table = new TStringGrid(Parent);
> Table->Left = 0;
> Table->Top = 0;
> Table->Width = 443;
> Table->Height = 181;
Table->SetBounds(0,0,443,181);
is faster.
> And here is the call from the main:
> //---------------------------------------------------------------------------
> void __fastcall TMainForm::FormCreate(TObject *Sender)
> {
> ColorTable = new TJMCColorTable(MainForm);
> }
> //---------------------------------------------------------------------------
You should not use FormCreate when You are working with
BCB, it is a Delphi Event.
Use the constructor instead.
Or if You want the aplication to be fast on screen You
can use a TTimer set to 50 miliseconds that You enable
in the OnShowEvent.
Then You create Your colortable in the TimerEvent.
To be able to use the designer You could also build everything
on a panel at design time and then just make it invisible when
You don't need it.
Or You can create an extra form, with BorderStyle = bsNone;
Take it of the autocreate list and then when You need it
You create it with new just like Your TJMCColorTable.
and use Show(); when You want to see it.
Kind regards
Asger
Re: Create VCL Control from within an Object
Author: JohnC
Date: Thu, 31 Jan 2008 20:23
Date: Thu, 31 Jan 2008 20:23
32 lines
2174 bytes
2174 bytes
"Asger Joergensen" <junk@Asger-P.dk> wrote in message news:MPG.220c37f435e9dd16989a27@newsgroups.borland.com... > You should not use FormCreate when You are working with > BCB, it is a Delphi Event. This one confuses me. Why do you say it is a Delphi event? I've been using it for years with C++ code. > Use the constructor instead. How would one use the constructor except through a hook? FormCreate is just a hook (into the constructor, I believe). > To be able to use the designer You could also build everything > on a panel at design time and then just make it invisible when > You don't need it. I've used this trick to avoid crowded forms. I agree, it can be very useful. The mess I'm trying to avoid is in the code. What I'm doing is extracting things into objects which will allow me to have cleaner code for the main form and which will allow me to reuse the composite controls in other similar or related applications. I've got it working now - what I needed was to explicitely set the Parent - I had thought that it was enough to include it in the constructor call. > Kind regards > Asger Thanks, John
Re: Create VCL Control from within an Object
Author: Asger Joergensen
Date: Thu, 31 Jan 2008 21:47
Date: Thu, 31 Jan 2008 21:47
49 lines
2502 bytes
2502 bytes
Hi JohnC
JohnC says:
>
> "Asger Joergensen" <junk@Asger-P.dk> wrote in message
> news:MPG.220c37f435e9dd16989a27@newsgroups.borland.com...
> > You should not use FormCreate when You are working with
> > BCB, it is a Delphi Event.
>
> This one confuses me. Why do you say it is a Delphi event?
Because it is.
> I've been using it for years with C++ code.
So have a lot of us until we got into trouble once, because of
the creation order.
> How would one use the constructor except through a hook?
If You look at the top of the form unit file e.g. unit1.cpp
You will see the constructor
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-------------------------------------------------------
> I've used this trick to avoid crowded forms. I agree, it can be very
> useful.
> The mess I'm trying to avoid is in the code.
Oooh I see, then the panel solution is no good.
> What I'm doing is extracting
> things into objects which will allow me to have cleaner code for the main
> form and which will allow me to reuse the composite controls in other
> similar or related applications.
You could also have a look at "Component Templates"
and maybe "Add To Repository"
I'm not in any way saying that Your aproach is wrong, I'm just
giving You some options.
Kind regards
Asger
Re: Create VCL Control from within an Object
Author: Bob Gonder
Date: Thu, 31 Jan 2008 22:32
Date: Thu, 31 Jan 2008 22:32
24 lines
1927 bytes
1927 bytes
JohnC wrote:
>This one confuses me. Why do you say it is a Delphi event? I've been using
>it for years with C++ code.
Delphi and C++ construct in different orders.
<HELP>
TCustomForm::OnCreate Event
Note:
Use of the OnCreate event is discouraged in C++ code because it can interact badly with
the form's constructor (see OldCreateOrder). It is recommended that you override the form
constructor instead.
</HELP>
>How would one use the constructor except through a hook? FormCreate is just
>a hook (into the constructor, I believe).
Form1::Form1()
{ C++ constructor. Startup code goes here.
}
Form1::OnCreate()
{ Delphi constructor. Leave empty.
}
Re: Create VCL Control from within an Object
Author: JohnC
Date: Thu, 31 Jan 2008 23:01
Date: Thu, 31 Jan 2008 23:01
3 lines
1157 bytes
1157 bytes
Learn something new every day! Thanks Gents! John
Re: Create VCL Control from within an Object
Author: JohnC
Date: Fri, 01 Feb 2008 11:43
Date: Fri, 01 Feb 2008 11:43
15 lines
1586 bytes
1586 bytes
OK, here's the documentation I found in the BCB6 Help files: Note: Use of the OnCreate event is discouraged in C++Builder because it can interact badly with the form's constructor (see OldCreateOrder). It is recommended that you override the form constructor instead. Now I'm convinced. John "JohnC" <jcibulskis@sbcglobal.net> wrote in message news:47a2533f$1@newsgroups.borland.com... > Learn something new every day! > Thanks Gents! > John >
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads