QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2117|回复: 0
打印 上一主题 下一主题

Select an item in a ListBox or a ComboBox from code.

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 12:25 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

- g2 Z c1 B9 t8 A: f; VAnswer: : n+ C4 [0 [/ CFor ComboBox and single selection ListBox controls, you select an item by assigning a number to the ItemIndex property of the ListBox or ComboBox. Assigning a value of zero selects the first item. Assigning -1 unselects all items.

$ p6 y, u4 Y3 y

// single selection ListBox examples & J$ D, P& ^( p) Q; m( B ListBox1->ItemIndex = 0; // selects first item1 u# g) B, Y* q6 ` ListBox1->ItemIndex = 2; // selects third item , N( ~( H5 J0 c# s# |- R ListBox1->ItemIndex++; // selects next item down # J6 q9 `# W* C k5 j3 [ ListBox1->ItemIndex = -1; // unselects all items

- a+ h, i: Y8 T: y

If you would rather select an item by specifying a string, use the IndexOf function to return the index of a string, and then assign that index to the ItemIndex property. Selecting the item who's text is "Uncle Bob" would look like this:

* g6 i% [/ e+ o

ListBox1->ItemIndex = ListBox1->Items->IndexOf("Uncle Bob");

) o' G% w# M1 B

For multi-select ListBox controls, you select and unselect items by reading and writing to the Selected property of the ListBox. The Selected property functions as an array of bool. The index of the array corresponds to the index of an item in the ListBox. To select an item, set its Selected index value to true. Here are some code examples.

3 q; q6 p0 F4 a# N+ m5 x5 K- a

// multi-selection ListBox examples.$ P ` h4 l1 ? ListBox1->Selected[0] = true; // adds the first item to the selection; f7 S8 q c) I$ b4 U ListBox1->Selected[0] = false; // removes first item from the selection

; @$ y* A0 T6 i

// toggles the 10th item 2 F/ `) {, I# u% s- c" r- m1 a ListBox1->Selected[9] = !ListBox1->Selected[9]

; K4 F# S/ v4 `9 I0 X: M1 r

// selects a range of items ! l; |- r0 y2 O9 b% X# k for (int j=10; j<=19; j++) ( {& z0 b h. N. m0 m: O ListBox1->Selected[j] = true;

5 ]0 h! \* d/ E% ^* R P9 b

Note: Reading a value from the Selected 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 Selected array sets the selection status of an item. Writing to the Selected 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.

9 V$ T5 M$ ]1 z1 r; o) F

Note: Do not use the Selected array in single selection ListBox controls

2 p: I' Z V- x X% W& ~

Note: In multi-select ListBox controls, you can read the ItemIndex property to determine which ListBox item has the input focus. However, assigning a value to ItemIndex has no effect in multi-select ListBox controls. When you write a value to ItemIndex, the VCL sends an LB_SETCURSEL to the ListBox control. Multi-select ListBox controls ignore this message. See the Win32 API help for more info. & C# v+ O1 \ w0 ?" S X

zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-18 19:11 , Processed in 0.345746 second(s), 51 queries .

回顶部