单击按钮时,我使用以下代码清除Active Directory中的用户属性。我有几个按钮(标题,部门,地址,城市,州,邮编,国家,电话各1个)。我可以在下面的8个按钮点击中使用相同的代码。但是,我认为它可能更干净,并减少代码量,将每个按钮的名称传递给单个子例程,因此您只需要一次,而不是8次,然后使用
dirEntryResults.Properties("btnName").Clear()
但是,我无法确定是否可以将按钮名称传递给子例程?这是可能的,如果是的话,怎么样?
Private Sub ClearTitle_Click(sender As Object, e As EventArgs) Handles ClearTitle.Click
Dim ADName As String = GetLogonName()
Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=test.account))"
dirSearcher.SearchScope = SearchScope.Subtree
Dim searchResults As SearchResult = dirSearcher.FindOne()
If Not searchResults Is Nothing Then
Dim dirEntryResults As New DirectoryEntry(searchResults.Path)
dirEntryResults.Properties("title").Clear()
dirEntryResults.CommitChanges()
dirEntryResults.Close()
MsgBox("Attribute Cleared", MsgBoxStyle.OkOnly, "Success")
End If
dirEntry.Close()
End Sub
答案 0 :(得分:0)
像这样的东西。请注意,我猜测按钮的实际名称,并且只对所有点击使用一个处理程序。
Private Sub Clears_Click(sender As Object, e As EventArgs) Handles ClearTitle.Click, ClearAddress.Click, ClearCity.Click 'etc
Dim BTN_NAME As String = DirectCast(sender, Button).Name.ToLower.Replace("clear", "")
Dim ADName As String = GetLogonName()
Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=test.account))"
dirSearcher.SearchScope = SearchScope.Subtree
Dim searchResults As SearchResult = dirSearcher.FindOne()
If Not searchResults Is Nothing Then
Dim dirEntryResults As New DirectoryEntry(searchResults.Path)
dirEntryResults.Properties(BTN_NAME).Clear()
dirEntryResults.CommitChanges()
dirEntryResults.Close()
MsgBox("Attribute Cleared", MsgBoxStyle.OkOnly, "Success")
End If
dirEntry.Close()
End Sub