listView1.SelectedItems.Count在返回1时返回0 [英] listView1.SelectedItems.Count returns 0 when it should return 1

查看:442
本文介绍了listView1.SelectedItems.Count在返回1时返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表视图,上面每个都有一个标签,我要做的是在选择listview中的任何元素时检索标签文本,但它总是转到else分支选择或不选择(defaultPath是以前定义的
类型字符串的变量)。


if(listView1.SelectedItems.Count> 0)

            {

                return label1.Text;

            }¥b $ b           否则if(listView2.SelectedItems.Count> 0)

            {

                return label2.Text;

            }¥b $ b           否则

                return defaultPath;




解决方案

嗨Cristina Raileanu,


您可以使用CheckedItem属性从列表视图中获取所选项目:

 listView1.CheckedItems.Count> 0 



 


I have two List Views and above every of them a label, what I'm trying to do is to retrieve the label text when any element from a listview is selected, but it always goes to the else branch whether selecting something or not (defaultPath is a variable of type string that is defined before).

if (listView1.SelectedItems.Count > 0)
            {
                return label1.Text;
            }
            else if (listView2.SelectedItems.Count > 0)
            {
                return label2.Text;
            }
            else
                return defaultPath;


解决方案

Hi Cristina Raileanu,

You can use CheckedItem property to get selected items from listview as:

listView1.CheckedItems.Count > 0

 


这篇关于listView1.SelectedItems.Count在返回1时返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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