📄 faq13.htm
字号:
<HTML>
<HEAD>
<TITLE>Select an item in a ListBox or a ComboBox from code.</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Select an item in a ListBox or a ComboBox from code.
</H3>
<P>
For ComboBox and single selection ListBox controls, you select an item by assigning
a number to the <TT>ItemIndex</TT> property of the ListBox or ComboBox. Assigning a
value of zero selects the first item. Assigning -1 unselects all items.
</P>
<pre>
<font color="navy">// single selection ListBox examples</font>
ListBox1<b>-></b>ItemIndex <b>=</b> <font color="blue">0</font><b>;</b> <font color="navy">// selects first item</font>
ListBox1<b>-></b>ItemIndex <b>=</b> <font color="blue">2</font><b>;</b> <font color="navy">// selects third item</font>
ListBox1<b>-></b>ItemIndex<b>++</b><b>;</b> <font color="navy">// selects next item down</font>
ListBox1<b>-></b>ItemIndex <b>=</b> <b>-</b><font color="blue">1</font><b>;</b> <font color="navy">// unselects all items</font>
</pre>
<P>
If you would rather select an item by specifying a string, use the
<TT>IndexOf</TT> function to return the index of a string, and then assign
that index to the <TT>ItemIndex</TT> property. Selecting the
item who's text is "Uncle Bob" would look like this:</P>
<pre>
ListBox1<b>-></b>ItemIndex <b>=</b> ListBox1<b>-></b>Items<b>-></b>IndexOf<b>(</b><font color="blue">"Uncle Bob"</font><b>)</b><b>;</b>
</pre>
<P>
For multi-select ListBox controls, you select and unselect items by reading and writing to the
<TT>Selected</TT> property of the ListBox. The <TT>Selected</TT> property functions as an array
of <TT>bool</TT>. The index of the array corresponds to the index of an item in the ListBox.
To select an item, set its <TT>Selected</TT> index value to <TT>true</TT>. Here are some code
examples.
</P>
<pre>
<font color="navy">// multi-selection ListBox examples.</font>
ListBox1<b>-></b>Selected<b>[</b><font color="blue">0</font><b>]</b> <b>=</b> <b>true</b><b>;</b> <font color="navy">// adds the first item to the selection</font>
ListBox1<b>-></b>Selected<b>[</b><font color="blue">0</font><b>]</b> <b>=</b> <b>false</b><b>;</b> <font color="navy">// removes first item from the selection</font>
<font color="navy">// toggles the 10th item</font>
ListBox1<b>-></b>Selected<b>[</b><font color="blue">9</font><b>]</b> <b>=</b> <b>!</b>ListBox1<b>-></b>Selected<b>[</b><font color="blue">9</font><b>]</b>
<font color="navy">// selects a range of items</font>
<b>for</b> <b>(</b><b>int</b> j<b>=</b><font color="blue">10</font><b>;</b> j<b><=</b><font color="blue">19</font><b>;</b> j<b>++</b><b>)</b>
ListBox1<b>-></b>Selected<b>[</b>j<b>]</b> <b>=</b> <b>true</b><b>;</b>
</pre>
<P>
<B>Note:</B> Reading a value from the <TT>Selected</TT> array of a multi-select ListBox tells you if an item is
currently selected. A value of true means that the item is selected, and false means that the item is not selected.
Writing to the <TT>Selected</TT> array sets the selection status of an item. Writing to the <TT>Selected</TT> array selects an item if you assign
true, and it unselects an item if you assign false. Reading or writing to one item in the ListBox does not
affect other items in the ListBox.
</P>
<P>
<B>Note:</B> Do not use the <TT>Selected</TT> array in single selection ListBox controls
</P>
<P>
<B>Note:</B> In multi-select ListBox controls, you can read the <TT>ItemIndex</TT> property to determine
which ListBox item has the input focus. However, assigning a value to <TT>ItemIndex</TT> has no effect in
multi-select ListBox controls. When you write a value to <TT>ItemIndex</TT>, the VCL sends an <TT>LB_SETCURSEL</TT>
to the ListBox control. Multi-select ListBox controls ignore this message. See the Win32 API help for more info.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -