如何在C#-Windows应用程序的listview-control中选择/清除选择? [英] How to select/clear selection in listview-control in C#-Windows-application?

查看:80
本文介绍了如何在C#-Windows应用程序的listview-control中选择/清除选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含txtSearch TextBox和ListView控件的表单.
我专注于文本框.
我已设置文本框的textbox_changed事件以在listview控件中搜索项目.
在其中我使用FindItemWithText方法来查找文本框文本,如下所示

I have a form containing a txtSearch TextBox and a ListView Control.
I have focus on text box.
I have set a textbox_changed event of text box to search items in listview control.
In which i use FindItemWithText method to find the textbox text as following

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    try
    {
        ListViewItem foundItem = lstHelp.FindItemWithText(txtSearch.Text,           false,0,true);
        if (foundItem != null)
        {
            lstHelp.TopItem = foundItem;
        }
    }
    catch { }
}




但是随着文本框的更改,事件列表框的选择不会更改.
我从列表视图控件中选择/清除选择的属性/方法.

请帮帮我.




But with textbox changed event listbox selection is not changing.
From which property/method I select/clear selection in listview control.

Please help me.

推荐答案

您的问题有些令人困惑:如果要从ListView删除项目,为什么要将其移至首先是ListView的顶部?

"FindItemWithText"工作正常,您可以调用foundItem.Remove();.删除项目后,检查后发现搜索结果为非空.

而且,我确实想知道您的目标是实际删除项目,还是只是将其从显示中删除...可能因此您可以稍后将其重新添加到ListViewItems中?

假设有一个原因要将ListViewItem移到ListView的顶部:我将讨论如何执行此操作的更有趣的问题:

在我看来,您已经找到了旧的ListView控件中的一个缺陷:仅当您将设置为TopItem的项目滚动到当前ListView的视图之外时,设置TopItem才会起作用.这似乎很愚蠢,因为ListView还支持ListViewItem的"EnsureVisible"方法,如果滚动到视图外,该方法将确保ListViewItem可见.

您可以改用此方法:
Your question is a bit puzzling: if you want to delete an item from a ListView, why do you want to move it to the top of the ListView first ?

''FindItemWithText'' works fine, and you can just call foundItem.Remove(); to delete the item, after you''ve checked you have a non-null result of your search.

And, I do wonder here if your goal is to actually delete the item, or simply to remove it from being displayed ... possibly so you can add it back to the ListViewItems later ?

Assuming there''s a reason you want to move the ListViewItem to the top of the ListView: I''m going to talk about the much more interesting issue of how to do that:

You have located what, in my humble opinion, is a flaw in the old ListView control: setting TopItem will only have an effect if the item you set to TopItem is scrolled out of view in the current ListView. This seems very stupid because ListView also supports the ''EnsureVisible'' method on a ListViewItem which will make sure the ListViewItem is made visible, if scrolled out of view.

You can use this instead:
private ListViewItem foundItem;
// here we are triggering the search by a button click
private void button1_Click(object sender, EventArgs e)
{
   foundItem = listView1.FindItemWithText(textBox1.Text);

   if (foundItem != null)
   {
     listView1.Items.Remove(foundItem);
     listView1.Items.Insert(0, foundItem);
   }
   else
   {
     // do something here if no match ?
   }
}

可以建议您考虑不要使用TextBox的TextChanged事件来触发搜索:因为每次按下键盘上的任何键时,它将执行新的搜索.

如果您不想通过按下按钮来触发搜索(如上面的示例代码中所示),则可以考虑查看诸如"AcceptsReturn"之类的TextBox属性,并考虑基于定义一些按键事件的策略在识别出已按下回车/回车"键时触发搜索(这是一种惯例).

May I suggest you consider not using the TextChanged event of the TextBox to trigger your search: because it''s going to perform a new search each time any key on the keyboard is pressed.

If you don''t want to trigger the search by a button press (as in the example code above), you might consider looking at TextBox properties like ''AcceptsReturn'' and thinking about a strategy based on defining some key-down Event that triggers the search when it recognizes Return/Enter has been pressed (which is kind of a convention).


请尝试以下操作.
Try the following.
// Set HideSelection property to false.
// This is to retain selection when the control is out of focus

lstHelp.HideSelection = false;

// set selected index when found a match

ListViewItem foundItem = lstHelp.FindItemWithText(txtSearch.Text,       false,0,true);
if (foundItem != null)
{
  lstHelp.Items[foundItem.Index].Selected = true;        
}
else
{
  lstHelp.SelectedItems.Clear(); // clear existing selections if any
}

注意:-默认情况下,当列表视图未聚焦时,所选项目将显示为灰色.根据需要更改所选项目的样式.

Note:- By default, when the list view is not focused, selected item is grayed out. Change the selected item style as required.


这篇关于如何在C#-Windows应用程序的listview-control中选择/清除选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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