📄 faq15.htm
字号:
<HTML>
<HEAD>
<TITLE>Add items to a ListView from code 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>
Add items to a ListView from code at runtime.
</H3>
<P>
The <TT>ListView</TT> control provides a nice interface for adding
items at design time from the IDE. However, you will usually need to
fill in the <TT>ListView</TT> based on runtime information. This code snippet
shows how to create <TT>ListView</TT> items and subitems from code.
</P>
<pre>
<b>int</b> NumEntries <b>=</b> <font color="blue">20</font><b>;</b>
TListItem <b>*</b>NewEntry<b>;</b>
ListView1<b>-></b>Items<b>-></b>BeginUpdate<b>(</b><b>)</b><b>;</b>
ListView1<b>-></b>Items<b>-></b>Clear<b>(</b><b>)</b><b>;</b>
<b>for</b> <b>(</b><b>int</b> j<b>=</b><font color="blue">0</font><b>;</b> j < NumEntries<b>;</b> j<b>++</b><b>)</b>
<b>{</b>
NewEntry <b>=</b> ListView1<b>-></b>Items<b>-></b>Add<b>(</b><b>)</b><b>;</b>
NewEntry<b>-></b>Caption <b>=</b> <font color="blue">"Item "</font> <b>+</b> IntToStr<b>(</b>j<b>)</b><b>;</b>
NewEntry<b>-></b>SubItems<b>-></b>Add<b>(</b><font color="blue">"column 2"</font><b>)</b><b>;</b>
NewEntry<b>-></b>SubItems<b>-></b>Add<b>(</b><font color="blue">"column 3"</font><b>)</b><b>;</b>
<font color="navy">// NewEntry->ImageIndex = j; // requires an imagelist</font>
<b>}</b>
ListView1<b>-></b>Items<b>-></b>EndUpdate<b>(</b><b>)</b><b>;</b>
</pre>
<P>
<B>Note:</B> The <TT>BeginUpdate</TT> function prevents the <TT>ListView</TT> from
flickering as the items are added. Both <TT>TListBox</TT> and
<TT>TListView</TT> provide a <TT>BeginUpdate</TT> function that you can call to keep the
control from flashing as you manipulate the control's contents. The <TT>EndUpdate</TT>
call signals that you have finished altering the control and that the control should now
paint itself.
</P>
<P>
<B>Note:</B> Notice that you obtain the <TT>TListItem</TT> pointer by calling the <TT>Add</TT>
function, rather than calling <TT>new</TT> directly.
</P>
<P>
<B>Note: </B> Also notice that you change the characteristics of the new item after calling
the <TT>Add</TT> function.
</P>
<P>
<B>Note: </B> The <TT>Caption</TT> string appears next to the item's icon, and will always
appear regardless of the <TT>ViewStyle</TT> setting of the <TT>ListView</TT>. The
<TT>SubItems</TT> appear in separate columns when <TT>ViewStyle</TT> is set to
<TT>vsReport</TT>. For other modes, the <TT>SubItems</TT> are invisible.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -