vb.net 确定是否在列表视图框中选择了一个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5492240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if an item is selected in a listview box
提问by StealthRT
Using VB.net 2010 i am trying to figure out if an item was selected or not. Reason being is that if the user clicks on an item and pushes a button then everything works just fine. However, if the user selects an item and then clicks on a blank spot on the bottom of the listview and then clicks the button then it crashes.
使用 VB.net 2010 我试图弄清楚是否选择了一个项目。原因是如果用户点击一个项目并按下一个按钮,那么一切正常。但是,如果用户选择一个项目,然后单击列表视图底部的空白点,然后单击按钮,则它会崩溃。
My code is this:
我的代码是这样的:
If (lstMaster.SelectedItems(0).SubItems(1).Text) Is Nothing Then
MsgBox("test")
End If
Any help would be great! :o)
任何帮助都会很棒!:o)
David
大卫
回答by Grammarian
Ensure that something is selected first by checking that SelectedItems
is not empty.
通过检查SelectedItems
不为空来确保首先选择某些内容。
lstMaster.SelectedItems.Count > 0
回答by p4th0log1st
Not sure if I've understood you correctly - Try using the ListView MouseMove event and check that lstMaster.SelectedItems.Count > 0 if you want to change the Enable property of a Button based on whether a row has been selected or not within your ListView control.
不确定我是否理解正确 - 尝试使用 ListView MouseMove 事件并检查 lstMaster.SelectedItems.Count > 0 如果要根据是否在 ListView 中选择了行来更改 Button 的 Enable 属性控制。
回答by Aleksey
Use this checking with "If/EndIf" construction: ListView1.Items(0).Selected = True
将此检查与“If/EndIf”构造一起使用:ListView1.Items(0).Selected = True