【问题标题】:Get ValueMember of Selected item in ListBox获取 ListBox 中选定项的 ValueMember
【发布时间】:2014-06-24 21:56:31
【问题描述】:

我看过一些帖子提出了类似的问题,但我无法成功地在我的代码中复制答案。 以下代码将项目及其值成员添加到列表框中。

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

结束类

请在下方查看我的评论并提供帮助

【问题讨论】:

  • 也许是关于what你想做的几句话,而不是如何可能会有所帮助。哈希表中有什么,您要显示什么以及要添加什么?基本上,您没有正确使用 ValueMember。

标签: vb.net listbox selecteditem valuemember


【解决方案1】:

ValueMember 应该表示添加到 Items 集合中的对象的属性名称:the property to use as the actual value for the items in the ListControl.

您没有向控件添加对象,因此哈希表中的KeyValueMember 一样毫无意义。您的帖子在传递中引用了一个文件变量,因此我假设这围绕显示文件名,同时希望在选择/单击时获取完整路径名。没有指明 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 将引用单击的myFile 对象的FullPathSelectedIndex 仍将引用列表中项目的索引。

tl;dr

ValueMember 和 DisplayMember 是指列表中表示的 ObjectsProperty Names

注意:

【讨论】:

  • 感谢您的帮助。似乎 ValueMember 不是要走的路,因为每个项目 valuemember 都显示为最后一个输入的项目。此外,listBox 正在输出“page.myFile”而不是项目显示成员。我已经编辑了我的代码以反映我所做的所有更改。
  • @user3772954 你仍然有一个错误,Display 和 ValueMember 是字符串,属性名称。请注意,我使用了"FileName""FullPath",即属性NAMES。您正在使用fb.FileName(这将是您看到的 TypeName)。对于 ValueMember,您将它们全部重置为 fb.FullPath,这是一个属性 value 而不是属性 name。它们不需要在循环中设置,这会映射整个 LIST 而不是每个项目。循环使它们全部设置为最后一个。您还可以在构造函数 fb = New myFile(the name, the path) 中设置名称
【解决方案2】:

我知道这是多年后的事,但仍然是相关信息。 我花了一段时间来解析上面所说的内容,直到我完全理解它,所以我认为如果我稍微重述一下它可能会有所帮助。

当您选择一个列表框项目时,

  • myListBox.SelectedValue 是字段的内容,myListBox.ValueMemberValueMember 包含Field NameSelectedValue 包含字段的内容
  • 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)

【讨论】:

    猜你喜欢
    • 2014-04-24
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多