如何知道是否在列表视图中选择了一个项目 [英] how to know if an item in the Listview is selected

查看:77
本文介绍了如何知道是否在列表视图中选择了一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
如果列表视图中的某个项目被选中,则要知道是否选择了任何项目..
我尝试使用listview1.SelectedItems.Count和listview1.SelectedIndices.Count,但是每次选择一个项目后,它都将count设为0 ...

thnx,

hi all ,
if an item in the listview is seleced then to know if any item is selected ..
I tried using listview1.SelectedItems.Count and listview1.SelectedIndices.Count but everytime it is giving count to 0 after selecting an item ...

thnx ,

推荐答案

您可以执行以下操作:

You can do something like this:

foreach listview itm yourListView.Items)
{
   if(itm.selected)
   {
    Add in a string, its id or name or whatever
   }
}


受保护的void ListBox1_SelectedIndexChanged(object sender,EventArgs e)
{
foreach(ListBox1.Items中的ListItem li)
{
如果(li.Selected)
{
Label1.Text = li.Text.ToString();
}
}
}

label1将显示选定的项目
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected)
{
Label1.Text = li.Text.ToString();
}
}
}

label1 will display the selected item


假设ListBox选择模式为MultiSimple或MultiExtended:否则,当然没有意义:)

定义一个新的SelectedObjectCollection,然后在您的表单加载事件中将其初始化:
Assume the ListBox selection mode is MultiSimple or MultiExtended: otherwise, of course, this makes no sense :)

Define a new SelectedObjectCollection, then initialize it in your Form Load Event:
private ListBox.SelectedObjectCollection ListBoxSelections;

private void Form1_Load(object sender, EventArgs e)
{
    // initialize it here because prior to Form Load
    // a reference to the instance of listBox1 does
    // not exist to pass as a parameter

    // why initialize it ?
    // so we don't have to check if it's == null
    // when no items are selected and we want to
    // do something like access the 'Count property
    ListBoxSelections = new ListBox.SelectedObjectCollection(listBox1);
}

然后将"SelectedIndexChanged"事件连接"到您的ListBox:

Then, ''wire-up'' the SelectedIndexChanged Event to your ListBox:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ListBoxSelections = listBox1.SelectedItems;
}

现在,任何时候您需要对ListBox中的选定项目执行任何操作时,都可以使用''ListBoxSelections:如果其''Count属性==#0,则表明当前没有选定的项目.

And now any time you need to do whatever with the selected items in the ListBox, you can just use ''ListBoxSelections: if its ''Count property == #0, you know there are no current items selected.


这篇关于如何知道是否在列表视图中选择了一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆