VS 2012 Pass Button Name to Function-VBForums
Results 1 to 3 of 3

Thread: Pass Button Name to Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    7

    Question Pass Button Name to Function

    Hi,
    I'm using the below code to clear a user attribute in Active Directory when a button is clicked. I have several of these buttons (1 each for title, department, address, city, state, zip, country, phone). I could just have the same code below for each of the 8 button clicks. However, I thought it might be cleaner, and reduce the amount of code, to pass the name of each button to a single sub routine, so you only need it once, not 8 times, and then use
    Code:
    dirEntryResults.Properties("btnName").Clear()
    . However, I can't see if it's possible to pass the button name to a sub routine? Is this possible, and if so, how?

    Code:
        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

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,915

    Re: Pass Button Name to Function

    I think I just answered this at SO

    Code:
        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
    Something like this. Note that I am guessing at the actual names of the buttons, and using only one handler for all of the clicks.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- SerialPort Answer

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    7

    Resolved Re: Pass Button Name to Function

    Yes, thank you for answering in both places. I usually ask in a few forums, as some are more active than others!

    I'd found a few other posts after I posted my question, which suggested adding the
    Code:
    Dim btnName As String
    or
    Code:
    Dim btnName as Button
    but I was getting the error
    Error 2 Value of type 'System.Windows.Forms.Button' cannot be converted to 'String'.
    Adding the
    Code:
    DirectCast(sender, Button).Name
    line that you suggested fixed it, and it now works! Full code below:

    Code:
        Private Sub ClearBtn_Click(sender As Object, e As EventArgs) Handles title.Click, physicalDeliveryOfficeName.Click, department.Click, telephoneNumber.Click, streetAddress.Click, st.Click, postalCode.Click, mobile.Click, l.Click, c.Click
            Dim btnName As String = DirectCast(sender, Button).Name
            Dim ADName As String = GetLogonName()
            Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
            Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
            dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" & ADName & "))"
            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(btnName).Clear()
                dirEntryResults.CommitChanges()
                dirEntryResults.Close()
                MsgBox("Attribute Cleared", MsgBoxStyle.OkOnly, "Success")
                Form1_Load(Me, New System.EventArgs)
            End If
            dirEntry.Close()
        End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.