获取 ListBox 中选定项的 ValueMember [英] Get ValueMember of Selected item in ListBox

查看:26
本文介绍了获取 ListBox 中选定项的 ValueMember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've seen a couple of posts asking a similar question but I have not been able to duplicate the answers in my code successfully. The following code adds items and their value member to a list box.

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 文件 = ListBox1.ValueMember.ToString()我可以访问我选择的第一个项目,但随后的选择不会将值成员更改为所选项目的值成员.

When I run my code to get the selected items value member Dim file = ListBox1.ValueMember.ToString() I can acess the first item I choose but subsequent selections dont change the value member to that of the selected item.

请指导我.

感谢您的回答.这是我的新代码:

Thank you for your answers. this is my new code:

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

结束类

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

Please see my comment below and assist

推荐答案

ValueMember 应该表示添加到 Items 集合中的对象的属性名称:用作ListControl 中项目的实际值.

ValueMember is supposed to indicate the property name of an object added to the Items collection: the property to use as the actual value for the items in the ListControl.

您没有向控件添加对象,因此哈希表中的 KeyValueMember 一样毫无意义.您的帖子在传递中引用了一个文件变量,因此我假设这围绕显示文件名,同时希望在选择/单击时获取完整路径名.没有指明 WebForms/Winforms/WPF,我假设是 WinForms:

You are not adding objects to the control, so Key from the hashtable is meaningless as the ValueMember. Your post references a file variable in passing, so I will assume this revolves around showing the filename while wanting to get the full pathname when selected/clicked. WebForms/Winforms/WPF was not indicated, I am assuming 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:

Lets say we want to add some of these to a ListBox, for each item added we want FileName to display as the text, but want to get them back by 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 而不必迭代或复制:

If the myFile objects were stored to a List(of myFile) rather than adding them to the control, we can bind the List as the DataSource and not have to iterate or copy:

mylistBox.DataSource = myFileList

无论哪种方式,Display- 和 ValueMember 都是指我们希望使用的属性名称:

Either way, Display- and ValueMember refer to the property names we wish to use:

myListBox.DisplayMember = "FileName"    ' indicate property name of obj to SHOW
myListBox.ValueMember = "FullPath"      ' prop name of object to return

当您选择一个列表框项时,myListBox.SelectedValue 将引用单击的 myFile 对象的 FullPath.SelectedIndex 仍将引用列表中项目的索引.

When you select a listbox item, myListBox.SelectedValue would refer to the FullPath of the myFile object clicked on. The SelectedIndex would still refer to the index of the item in the list.

tl;dr

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

ValueMember and DisplayMember refers to the Property Names of Objects represented in the list.

注意:

这篇关于获取 ListBox 中选定项的 ValueMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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