如何获取列表框中项目的数量

6
在我的程序中,我使用了一个列表框,在这个列表框中添加了许多项目。
现在我想知道如何获取列表框项目的数量,并在文本框中显示项目数,当项目数不固定时。
2个回答

18

假设你的列表框名为 ListBox1,文本框名为 TextBox1。那么你需要的代码是:

TextBox1.Text = ListBox1.Items.Count.ToString();

当然,了解您所使用的平台(WinForms、WebForms、Html等)会对获得更准确的答案有很大帮助。


通过 ASP.Net 标签,我非常确定这是 WebForms。 - HenryZhang
4
那一定是一个看不见的标签。 - Tim Schmelter

0
我想为使用WPF的人们提供一个答案。
<ListBox  ItemsSource="{Binding Path=Parts}">                   
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" >
                <Label Content="{Binding CurrentPart, Mode=OneWay}" />
                <Label Content="{Binding Path=Parts.Count}"                           
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接