确定多重选择列表控件 (List Control) 中的选定内容

博客介绍确定多重选择列表控件中选定内容的方法,即依次通过控件的 Items 集合,分别测试每一项的 Selected 属性,并给出测试多重选择 ListBox 控件选定内容的示例,代码可在标签中显示选定项列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

确定多重选择列表控件 (List Control) 中的选定内容
依次通过控件的 Items 集合,分别测试每一项的 Selected 属性。
下面的示例显示如何测试多重选择 ListBox 控件(名为 ListBox1)的选定内容。代码将在一个标签中显示选定项的列表。

 1None.gif' Visual Basic
 2ExpandedBlockStart.gifContractedBlock.gifPublic Sub Button1_Click()Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click
 3InBlock.gif   Dim msg As String
 4InBlock.gif   Dim li As ListItem
 5InBlock.gif   msg = ""
 6InBlock.gif   For Each li In ListBox1.Items
 7InBlock.gif      If li.Selected = True Then
 8InBlock.gif         msg = msg & "<BR>" & li.Text & " selected."
 9InBlock.gif   End If
10InBlock.gif   Next
11InBlock.gif   Label1.Text = msg
12ExpandedBlockEnd.gifEnd Sub

 

 1None.gif// C#
 2None.gifpublic void Button1_Click(object sender, System.EventArgs e)
 3ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 4InBlock.gif   // Text that will display selected items
 5InBlock.gif   string msg = "" ;   
 6InBlock.gif   foreach(ListItem li in ListBox1.Items)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
 8InBlock.gif      if(li.Selected == true)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
10InBlock.gif            msg += "<BR>" + li.Text + " is selected.";
11ExpandedSubBlockEnd.gif         }

12ExpandedSubBlockEnd.gif   }

13InBlock.gif   Label1.Text = msg;
14ExpandedBlockEnd.gif}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值