我看过几篇帖子提出类似问题,但我无法成功复制代码中的答案。 以下代码将项及其值成员添加到列表框中。
Public Shared Sub ListFiles(hTab As Hashtable)
Debug.Print("create file and key" & Now)
Dim Enumerator As IDictionaryEnumerator
Enumerator = hTab.GetEnumerator()
Dim MyKeys As ICollection
Dim Key As Object
MyKeys = hTab.Keys()
If (hTab.Count > 0) Then
For Each Key In MyKeys
Dim sfileName As String = hTab(Key)
Dim first As Integer = sfileName.IndexOf("_")
Dim last As Integer = sfileName.LastIndexOfAny("_")
Dim first2 = (first + 1)
Dim splitFile = sfileName.Substring(first2)
frmViewFiles.ListBox1.Items.Add(splitFile)
frmViewFiles.ListBox1.ValueMember = Key
frmViewFiles.ListBox1.SelectedValue = Key
Next
End If
End Sub
当我运行我的代码以获取所选项目的值成员时
Dim file = ListBox1.ValueMember.ToString()
我可以访问我选择的第一个项目,但后续选择不会将值成员更改为所选项目的值。
请指导我。
感谢您的回答。这是我的新代码:
Public Shared Sub runListFiles(CustomerId As String)
Dim cfp As New CloudFilesProvider(cloudId)
Dim containerObjectList As IEnumerable(Of ContainerObject) = cfp.ListObjects(container:="EstherTest", identity:=cloudId, prefix:=CustomerId & "_")
For Each file As ContainerObject In containerObjectList
Dim sFullFileName As String = file.Name
Dim first As Integer = sFullFileName.IndexOf("_")
Dim first2 = (first + 1)
Dim splitFile = sFullFileName.Substring(first2)
'frmViewFiles.ListBox1.Items.Add(splitFile)
'frmViewFiles.ListBox1.ValueMember = sFullFileName
Dim fb = New myFile
fb.FileName = splitFile
fb.FullPath = sFullFileName
frmViewFiles.ListBox1.Items.Add(fb)
frmViewFiles.ListBox1.DisplayMember = fb.FileName
frmViewFiles.ListBox1.ValueMember = fb.FullPath
这是我的班级:
Public Class myFile
Public Property FileName As String
Public Property FullPath As String
Public Sub New(f As String, b As String)
FileName = f
FullPath = b
End Sub
结束班
请参阅下面的评论和协助
答案 0 :(得分:2)
ValueMember
应该指示添加到Items集合中的对象的属性名称:the property to use as the actual value for the items in the ListControl.
您没有向控件添加对象,因此哈希表中的Key
与ValueMember
毫无意义。你的帖子在传递中引用了一个文件变量,所以我假设这是围绕显示文件名,同时想要在选择/点击时获得完整的路径名。 WebForms / Winforms / WPF没有显示,我假设WinForms:
Public Class myFile
Public Property FileName As String
Public Property FullPath As String
Public Property FileSize As Int64 ' just so there is something else
Public Sub New(f as String, p as String, s as Int64)
FileName = f
FullPath = b
FileSize = s
End Sub
End Class
让我们说我们想要将其中的一些添加到ListBox中,对于我们希望FileName
添加的每个项目显示为文本,但希望通过FullPath
将其取回:
Dim f As myFile
' assume these come from a fileinfo
For Each fi as FileInfo in DirectoryInfo.GetFiles(searchFor)
f = New myFile
f.FileName = fi.Name
f.FullPath = fi.FullPath
f.FileSize = fi.Length
' myFile accepts all the prop values in the constructor
' so creating a new one could also be written as:
' f = New myFile(fi.Name, fi.FullPath, fi.Length)
myListBox.Items.Add(f)
Next n
如果将myFile对象存储到List(of myFile)
而不是将它们添加到控件中,我们可以将List绑定为DataSource,而不必迭代或复制:
mylistBox.DataSource = myFileList
无论哪种方式,Display-和ValueMember都会引用我们希望使用的属性名称:
myListBox.DisplayMember = "FileName" ' indicate property name of obj to SHOW
myListBox.ValueMember = "FullPath" ' prop name of object to return
当您选择列表框项目时,myListBox.SelectedValue
会引用所点击的FullPath
对象的myFile
。 SelectedIndex
仍然会引用列表中项目的索引。
<强> TL;博士强>
ValueMember和DisplayMember指的是列表中显示的对象的属性名称。
注意:
答案 1 :(得分:0)
我知道这是很多年后的事了,但仍然是相关信息。 我花了一段时间来解析上面所说的内容,直到我完全理解它,所以我认为如果我稍微重申一下可能会有所帮助。
当您选择列表框项时,
myListBox.SelectedValue
是字段 myListBox.ValueMember
的内容。 ValueMember
包含 Field Name
,SelectedValue
包含字段的内容。myListBox.SelectedItem
是字段 myListBox.DisplayMember
的内容。 DisplayMember
包含字段名称,SelectedItem
包含字段的值。SelectedIndex
指的是列表中项目的索引。要查看选择了哪个项目,请参考 myListBox.SelectedIndex
。例如,您可以使用 myListBox.SelectedIndex = myListBox.Items.Count - 1
如果要显示值,则
Console.WriteLine("The value of {0} is {1}",myListBoxDisplayMember,myListBox.SelectedItem)
Console.WriteLine("The Value of {0} is {1}",myListBox.ValueMember,myListBox.SelectedValue)