vb.net 从 ListView 子项中获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20133849/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:18:09  来源:igfitidea点击:

Get Text From a ListView SubItem

vb.net

提问by Jake2k13

I am creating an application for windows desktop using Visual Basic 2010 Express. The program I am designing has three main controls: a ListView, a TextBox, and a Button. What I need is when the user clicks a row inside of the ListView control, the TextBox will show the text within the first SubItem. To elaborate, the ListView control has two columns (Name and Description). In the SelectedIndexChanged event, I need code that will display the Description text in the TextBox (the ListView SubItem).

我正在使用 Visual Basic 2010 Express 创建 Windows 桌面应用程序。我正在设计的程序具有三个主要控件:ListView、TextBox 和 Button。我需要的是当用户单击 ListView 控件内的一行时,TextBox 将显示第一个 SubItem 中的文本。详细地说,ListView 控件有两列(名称和说明)。在 SelectedIndexChanged 事件中,我需要在 TextBox(ListView SubItem)中显示描述文本的代码。

I would post my code to show what I have done, but I do not know where to even start, for all my code has just given me errors. I tried something like this:

我会发布我的代码来展示我做了什么,但我什至不知道从哪里开始,因为我所有的代码都给了我错误。我试过这样的事情:

textbox1.text = listview1.items.subitems.tostring

But obviously this method is useless and completely off track. I know this is basic, but I do not understand it. Thanks

但显然这种方法是无用的,完全偏离了轨道。我知道这是基本的,但我不明白。谢谢

回答by ??ssa P?ngj?rdenlarp

for the text of the selected LV Item:

对于所选 LV 项目的文本:

 textbox1.text = listview1.SelectedItem.ToString   

For the text of SubItem N of the First selected Item:

对于第一个选定项的子项 N 的文本:

  textbox1.text = listview1.SelectedItems(0).SubItems(N).Text

you can also get it using listview1.Items(X).SubItems(N).Textwhere X is the index of the Item (row) you want

您还可以使用listview1.Items(X).SubItems(N).Text其中 X 是您想要的项目(行)的索引来获取它

回答by Eddy Jawed

If you want to get the subitem you clicked on from a listview control simply use ListViewHitTestInfo in a mouse click event i.e

如果您想从列表视图控件中获取您单击的子项,只需在鼠标单击事件中使用 ListViewHitTestInfo 即可

 Dim info As ListViewHitTestInfo = lstvw1.HitTest(e.X, e.Y)
 MsgBox(info.SubItem.Text)