Selected ListBox Not Passing

This topic contains 0 replies, has 1 voice, and was last updated by  Chad Nathan Jones 6 months ago.

  • Author
    Posts
  • #71416

    Chad Nathan Jones
    Participant

    Here is the beginning of the code and what I am trying to do is when someone selects something on the ListBox it would then pass that value to a variable where I could use later on in the code. Nothing is passing though. I have just for testing
    $info.Text = $x which should be the SelectedItem in $UserList

    Function MakeNewForm {
    	$GUI.Close()
    	$GUI.Dispose()
    	MakeForm
    }
    ### Remove user for all AD Groups
    Function RemoveMemberships
    
     {
    
     param([string]$disableUser)  
     
     $disuser = Get-ADUser $($disableUser) -properties memberof
     
     $userGroups = $disuser.memberof
    
     $userGroups | %{get-adgroup $_ | Remove-ADGroupMember -confirm:$false -member $($disableUser)}
    
     $userGroups = $null
    
     }
    ###################### END OF FUNCTIONS #########################
    
    Function MakeForm{
    Add-Type -AssemblyName System.Windows.Forms
    $GUI = New-Object system.Windows.Forms.Form
    $GUI.Text = "Disable User Account"
    $GUI.BackColor = "#ffffff"
    $GUI.TopMost = $true
    $GUI.BackgroundImage = [system.drawing.image]::FromFile("yay.png")
    $GUI.Width = 510
    $GUI.Height = 203
    $GUI.StartPosition = "manual"
    $GUI.FormBorderStyle = 3
    $GUI.Location = New-Object System.Drawing.Size(600, 300)
    
    
    
    $disUser = New-Object system.windows.Forms.TextBox
    $disUser.Width = 100
    $disUser.Height = 20
    $disUser.location = new-object system.drawing.point(23,20)
    $disUser.Font = "Microsoft Sans Serif,10"
    $GUI.controls.Add($disUser)
    
    $User = New-Object system.windows.Forms.Label
    $User.Text = "Disable Username"
    $User.AutoSize = $true
    $User.Width = 25
    $User.Height = 10
    $User.location = new-object system.drawing.point(23,3)
    $User.Font = "Arial,10"
    $GUI.controls.Add($User)
    
    $search = New-Object system.windows.Forms.Button
    $search.BackColor = "#cccccc"
    $search.Text = "Search"
    $search.ForeColor = "#0b6ea8"
    $search.Width = 62
    $search.Height = 20
    $search.Add_Click({
    
    $UserList = New-Object system.windows.Forms.ListBox
    $UserList.Width = 134
    $UserList.Height = 125
    
    $disableUser = $disUser.Text
    $names = Get-ADUser -Filter {name -like $disableUser} | Select -ExpandProperty name
    foreach ($name in $names) {
    $UserList.items.add($name) | Out-Null
    }
    if ($UserList.SelectedIndex -gt -1)
    {
    $global:x = $UserList.SelectedItem
    }
    $UserList.location = new-object system.drawing.point(212,20)
    $GUI.controls.Add($UserList)
    })
    
    $search.location = new-object system.drawing.point(140,20)
    $search.Font = "Microsoft Sans Serif,10"
    $GUI.controls.Add($search)
    
    $label6 = New-Object system.windows.Forms.Label
    $label6.Text = "Assign Full Access to Mailbox"
    $label6.AutoSize = $true
    $label6.Width = 25
    $label6.Height = 10
    $label6.location = new-object system.drawing.point(23,45)
    $label6.Font = "Microsoft Sans Serif,10"
    $GUI.controls.Add($label6)
    
    $faUser = New-Object system.windows.Forms.TextBox
    $faUser.Width = 100
    $faUser.Height = 20
    $faUser.location = new-object system.drawing.point(23,65)
    $faUser.Font = "Microsoft Sans Serif,10"
    $GUI.controls.Add($faUser)
    
    $ticketin = New-Object system.windows.Forms.TextBox
    $ticketin.Width = 100
    $ticketin.Height = 20
    $ticketin.location = new-object system.drawing.point(23,108)
    $ticketin.Font = "Microsoft Sans Serif,10"
    $GUI.controls.Add($ticketin)
    
    $ticket = New-Object system.windows.Forms.Label
    $ticket.Text = "Ticket Number"
    $ticket.AutoSize = $true
    $ticket.Width = 25
    $ticket.Height = 10
    $ticket.location = new-object system.drawing.point(23,89)
    $ticket.Font = "Microsoft Sans Serif,10"
    $GUI.controls.Add($ticket)
    
    $reset = New-Object system.windows.Forms.Button
    $reset.BackColor = "#cccccc"
    $reset.Text = "Reset"
    $reset.ForeColor = "#0b6ea8"
    $reset.Width = 62
    $reset.Height = 20
    $reset.Add_Click({MakeNewForm})
    $reset.location = new-object system.drawing.point(105,140)
    $reset.Font = "Microsoft Sans Serif,10"
    $GUI.controls.Add($reset)
    
    $execute = New-Object system.windows.Forms.Button
    $execute.BackColor = "#cccccc"
    $execute.Text = "Disable"
    $execute.ForeColor = "#ff0000"
    $execute.Width = 62
    $execute.Height = 20
    $execute.Add_MouseClick({
    #Code to Execute
    
    Import-Module ActiveDirectory
    $Today=Get-date -Format "MMM/d/yyyy"
    $LoggedInUser=$env:USERNAME
    
    ####if ($UserList.SelectedItem -eq $null)
    ####{
    ####$disableUser = $disUser.Text
    ####}
    ####else
    ###{
    ####$disableUser = $UserList.SelectedItem
    ####}
    
    $info = New-Object system.windows.Forms.Label
    $info.Text = $x
    $info.ForeColor = "#FF0000"
    $info.AutoSize = $true
    $info.Width = 25
    $info.Height = 10
    $info.location = new-object system.drawing.point(180,140)
    $info.Font = "Microsoft Sans Serif,10,style=Bold"
    $GUI.controls.Add($info)
    })
    
    $execute.location = new-object system.drawing.point(24,140)
    $execute.Font = "Microsoft Sans Serif,10,style=Bold"
    $GUI.controls.Add($execute)
    
    [void]$GUI.ShowDialog()
    $GUI.Dispose()
    }
    
    MakeForm
    
    

The topic ‘Selected ListBox Not Passing’ is closed to new replies.