📄 faq2.htm
字号:
<HTML>
<HEAD>
<TITLE>Create a control at runtime</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Create a control at runtime
</H3>
<P>
<B>Step 1:</B> Add a variable to the form class that will contain the
component. Since this component is not being managed by the IDE, do not
put the declaration in the <B><TT>__published</TT></B> section. Also,
make sure the variable is listed as a pointer.</P>
<P>
<pre>
<b>class</b> TForm1 <b>:</b> <b>public</b> TForm
<b>{</b>
<b>__published</b><b>:</b> <font color="navy">// IDE-managed Components</font>
<b>private</b><b>:</b> <font color="navy">// User declarations</font>
TButton <b>*</b>OKButton<b>;</b>
<b>...</b>
</pre>
</P>
<P>
Technically, the variable does not have to reside in the
form class that the control will appear in. You could place the <TT>OKButton</TT>
declaration in <TT>TForm1</TT>, even if you will display the button in another
form. However, you should avoid this practice for the sake of code
clarity.
</P>
<P>
<B>Step 2:</B> Create the control and assign properties. If you want the
control to appear when the form appears, place this code in the
constructor of the form.</P>
<P>
<P>
<pre>
<b>__fastcall</b> TForm1<b>:</b><b>:</b>TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b>
<b>:</b> TForm<b>(</b>Owner<b>)</b>
<b>{</b>
OKButton <b>=</b> <b>new</b> TButton<b>(</b><b>this</b><b>)</b><b>;</b>
OKButton<b>-></b>Parent <b>=</b> <b>this</b><b>;</b>
OKButton<b>-></b>Caption <b>=</b> <font color="blue">"Dynamic Button"</font><b>;</b>
OKButton<b>-></b>SetBounds<b>(</b><font color="blue">10</font><b>,</b><font color="blue">10</font><b>,</b><font color="blue">110</font><b>,</b><font color="blue">25</font><b>)</b><b>;</b>
<b>}</b>
</pre>
</P>
<P>
<B>Note:</B> The contol will not appear if you forget to assign the <TT>Parent</TT> property.
</P>
<P>
<B>Note:</B> For most dynamic controls, you will need to assign an event handler in code. This
task is separated into a different question. See the FAQ listed as how do I
<A HREF="../events messages/faq1.htm">assign event handlers at runtime</A>. The example below assigns an <TT>OnClick</TT>
handler for the button.
</P>
<pre>
<font color="navy">// header file</font>
<b>class</b> TForm1 <b>:</b> <b>public</b> TForm
<b>{</b>
<b>__published</b><b>:</b> <font color="navy">// IDE-managed Components</font>
<b>private</b><b>:</b> <font color="navy">// User declarations</font>
TButton <b>*</b>OKButton<b>;</b>
<b>void</b> <b>__fastcall</b> ButtonClick<b>(</b>TObject <b>*</b>Sender<b>)</b><b>;</b>
<b>...</b>
<b>...</b>
<font color="navy">// source file</font>
<b>__fastcall</b> TForm1<b>:</b><b>:</b>TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b>
<b>:</b> TForm<b>(</b>Owner<b>)</b>
<b>{</b>
OKButton <b>=</b> <b>new</b> TButton<b>(</b><b>this</b><b>)</b><b>;</b>
OKButton<b>-></b>Parent <b>=</b> <b>this</b><b>;</b>
OKButton<b>-></b>Caption <b>=</b> <font color="blue">"Dynamic Button"</font><b>;</b>
OKButton<b>-></b>SetBounds<b>(</b><font color="blue">10</font><b>,</b><font color="blue">10</font><b>,</b><font color="blue">110</font><b>,</b><font color="blue">25</font><b>)</b><b>;</b>
OKButton<b>-></b>OnClick <b>=</b> ButtonClick<b>;</b>
<b>}</b>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>ButtonClick<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<font color="navy">// handler code here</font>
<b>}</b>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -