简体   繁体   English

从列表框中选择一项,一个接一个

[英]Select items from Listbox, one by one

I am working on Microsoft Visual C# 2010 Express. 我正在使用Microsoft Visual C#2010 Express。 Actually, when I execute my code, I get a listbox containing N items. 实际上,当我执行代码时,我得到一个包含N个项目的列表框。 There is a list of items with their size in my database. 我的数据库中有一个项目列表及其大小。 I am using Microsoft Access 2007 database. 我正在使用Microsoft Access 2007数据库。 I want to display the total size of items present in listbox into a label below the listbox. 我想在列表框下方的标签中显示列表框中存在的项目的总大小。 I have my code for displaying the size. 我有显示尺寸的代码。 I just want to know how to select already present data in a listbox without any button click. 我只想知道如何在不单击任何按钮的情况下选择列表框中已经存在的数据。 I think loop will be used. 我认为将使用循环。

Do what you want to do but i don't understand why you need to select an item form listbox. 做您想做的事,但我不明白为什么您需要选择一个项目表单列表框。 but you can get the items by 但是你可以通过

listBox1.Items[i] and then can calculate the size. listBox1.Items[i] ,然后可以计算大小。

Use below code to do that what you wanted. 使用下面的代码执行所需的操作。

  for (int i = 0; i < listBox1.Items.Count; i++)
        {
            listBox1.SelectedItem = listBox1.Items[i];
            //Calculate the total size of items present in listbox here 
        }

I just want to know how to select already present data in a listbox without any button click. 我只想知道如何在不单击任何按钮的情况下选择列表框中已经存在的数据。

I think the MouseDoubleClick event is what you want to handle. 我认为MouseDoubleClick事件是您要处理的事件。 To add the handler for that, in the designer, in the properties window, click on the little yellow lightning bolt icon. 要为此添加处理程序,请在设计器的属性窗口中,单击黄色的小闪电图标。 This will show a list of events. 这将显示事件列表。 Double-Click on the one you want to handle and the system will create the stub for it and add the handler to the control. 双击要处理的代码,系统将为其创建存根并将处理程序添加到控件中。 A loop in side here will allow you get what you need: 在这里循环可以让您得到所需的东西:

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        foreach (string item in listBox1.Items)
        {
            //Pass item to your size routine here
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM