我编写了以下程序来查找LDAP用户并返回一个属性。我需要它的工作方式如下:首先我将一个用户ID列表加载到ListBox1
,然后当我点击一个按钮时,一个属性(如DisplayName)将被追加到ListBox2
。现在我必须单击ListBox1
中的项目,然后单击按钮它可以工作,但我希望它遍历ListBox1
中的每个ID并将所有这些属性写入{{ 1}}我无需点击每个用户ID。如何在每个循环中放入以下内容?
ListBox2
更新
我尝试使用Dim de As New DirectoryEntry("LDAP://test.com/DC=test,DC=com")
Dim LdapFilter As String = "(sAMAccountName=" & ListBox1.Text & ")"
Dim searcher As New DirectorySearcher(de, LdapFilter)
Dim result As SearchResult = searcher.FindOne()
ListBox2.Items.Add(result.Properties("displayName")(0).ToString())
显示两列,如建议的那样。但是,它不起作用。它只是添加ListView
列:
ListView
答案 0 :(得分:2)
您不应该使用ListBox1.Text
属性。这让人很困惑。在这种情况下,您希望循环遍历ListBox1.Items
列表中的所有字符串(假设它们实际上是字符串)。例如:
For Each i As String in ListBox1.Items
Dim de As New DirectoryEntry("LDAP://test.com/DC=test,DC=com")
Dim LdapFilter As String = "(sAMAccountName=" & i & ")"
Dim searcher As New DirectorySearcher(de, LdapFilter)
Dim result As SearchResult = searcher.FindOne()
ListBox2.Items.Add(result.Properties("displayName")(0).ToString())
Next
或者,如果ListBox1
实际上不包含字符串,则可以将其作为Object
循环播放,并在每个字符串上调用ToString
方法,如下所示:
For Each i As Object in ListBox1.Items
Dim de As New DirectoryEntry("LDAP://test.com/DC=test,DC=com")
Dim LdapFilter As String = "(sAMAccountName=" & i.ToString() & ")"
Dim searcher As New DirectorySearcher(de, LdapFilter)
Dim result As SearchResult = searcher.FindOne()
ListBox2.Items.Add(result.Properties("displayName")(0).ToString())
Next
正如我在下面的评论中提到的,不是使用两个单独的ListBox
控件,而是最好使用带有两列的ListView
控件。例如,如果你有一个ListView1
控件有三列(标题为" ID","用户名"和"标题"),那么你可以添加如下项目:
Dim userIds As IEnumerable(Of String) = getAllLdapUserIds() ' Get the list of ID's using whatever means you are currently using
For Each i As String In userIds
Dim de As New DirectoryEntry("LDAP://test.com/DC=test,DC=com")
Dim LdapFilter As String = "(sAMAccountName=" & i & ")"
Dim searcher As New DirectorySearcher(de, LdapFilter)
Dim result As SearchResult = searcher.FindOne()
Dim item As ListViewItem = ListView1.Items.Add(i)
item.SubItems.Add(result.Properties("sAMAccountName")(0).ToString())
item.SubItems.Add(result.Properties("title")(0).ToString())
Next