amphoric

List Box - ADD items and store selected in variable.

18 posts in this topic

#1 ·  Posted

Good Morning,

I would love if someone could help me with the following;

I have this code provided by 'Water' to list groups within an OU from AD

Global $sGroupToProcess = "specify group to process here"
$aGroups = _AD_GetObjectsInOU("","(objectCategory=group)", 0, "displayname")

What i would like to do now is list these in a list box which has a search function to narrow down the results, and then finally be able to select on of the list items and store that in a variable for later use.

 

I hope this makes sense and i look forward to any help I receive.

Thanks 

Share this post


Link to post
Share on other sites



#2 ·  Posted

evansmike881,

This old script which I have posted many times allows you to enter a search string and limit the list content to those which match:

#include <GUIConstantsEx.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $hGUI, $hInput, $hList, $sPartialData, $asKeyWords[100]

; Create list full of random 5 character "words"
Keywords()

$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 190, 20)
$hList = GUICtrlCreateList("", 5, 30, 190, 325, BitOR(0x00100000, 0x00200000))
$hButton = GUICtrlCreateButton("Read", 60, 360, 80, 30)
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList
            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then GUICtrlSetData($hInput, $sChosen)
        Case $hButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                If _ArraySearch($asKeyWords, $sFinal) > 0 Then
                    MsgBox(0, "Chosen", $sFinal)
                EndIf
            EndIf
        Case $hUP
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hDOWN
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hENTER
            If $iCurrIndex <> -1 Then
                $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
                GUICtrlSetData($hInput, $sText)
                $iCurrIndex = -1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    EndIf

WEnd

Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 0 To 99
            If StringInStr($asKeyWords[$i], $sInput) <> 0 Then $sPartialData &= $asKeyWords[$i] & "|"
        Next
        GUICtrlSetData($hList, $sPartialData)
    EndIf
EndFunc   ;==>CheckInputText

Func Keywords()

    Local $sData
    For $i = 0 To 99
        $asKeyWords[$i] = Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1)) & Chr(Random(65, 90, 1))
        $sData &= $asKeyWords[$i] & "|"
    Next
    GUICtrlSetData($hList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)

EndFunc   ;==>Keywords

Let me know if you run into any difficulties incorporating this into your script.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

This is brilliant! Thanks Melba23!

I am literally days into starting to use Autoit , I will try my best to incorporate this in to my project :)

Very much appreciated.

Share this post


Link to post
Share on other sites

#4 ·  Posted

evansmike881,

My pleasure as always.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

Im struggling to link both scripts together... i cant seem to get 'Water's script to work with yours? 

Can you please help?

 

Thanks

Share this post


Link to post
Share on other sites

#6 ·  Posted

evansmike881,

I do not use AD, so can you let me see an example of the array that is returned from water's code - I suggest using _ArrayDisplay and copying the data to a file which you can post (after redacting anything sensitive). That data is what replaces the $asKeyWords array in my script, so once I have an example I can see what might be going wrong.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#7 ·  Posted

Im sorry melba23, how would i go about doing that?

 

Thanks

Share this post


Link to post
Share on other sites

#8 ·  Posted

Try something like:

#include <AD.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $hGUI, $hInput, $hList, $sPartialData, $aGroups

; Create list full of random 5 character "words"
Keywords()

$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 190, 20)
$hList = GUICtrlCreateList("", 5, 30, 190, 325, BitOR(0x00100000, 0x00200000))
$hButton = GUICtrlCreateButton("Read", 60, 360, 80, 30)
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList
            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then GUICtrlSetData($hInput, $sChosen)
        Case $hButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                If _ArraySearch($aGroups, $sFinal) > 0 Then
                    MsgBox(0, "Chosen", $sFinal)
                EndIf
            EndIf
        Case $hUP
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hDOWN
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hENTER
            If $iCurrIndex <> -1 Then
                $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
                GUICtrlSetData($hInput, $sText)
                $iCurrIndex = -1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    EndIf

WEnd

Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 0 To $aGroups[0]
            If StringInStr($aGroups[$i], $sInput) <> 0 Then $sPartialData &= $aGroups[$i] & "|"
        Next
        GUICtrlSetData($hList, $sPartialData)
    EndIf
EndFunc   ;==>CheckInputText

Func Keywords()
    Local $sGroups
    _AD_Open()
        $aGroups = _AD_GetObjectsInOU("",'(&(objectCategory=group)(displayName=*))', 2, "displayName")
    _AD_Close()
    $sGroups = _ArrayToString($aGroups, "|", 1)
    GUICtrlSetData($hList, $sGroups)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
EndFunc   ;==>Keywords

 

Share this post


Link to post
Share on other sites

#9 ·  Posted

3 minutes ago, Subz said:

Try something like:

#include <AD.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $hGUI, $hInput, $hList, $sPartialData, $aGroups

; Create list full of random 5 character "words"
Keywords()

$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 190, 20)
$hList = GUICtrlCreateList("", 5, 30, 190, 325, BitOR(0x00100000, 0x00200000))
$hButton = GUICtrlCreateButton("Read", 60, 360, 80, 30)
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList
            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then GUICtrlSetData($hInput, $sChosen)
        Case $hButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                If _ArraySearch($aGroups, $sFinal) > 0 Then
                    MsgBox(0, "Chosen", $sFinal)
                EndIf
            EndIf
        Case $hUP
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hDOWN
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hENTER
            If $iCurrIndex <> -1 Then
                $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
                GUICtrlSetData($hInput, $sText)
                $iCurrIndex = -1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    EndIf

WEnd

Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 0 To $aGroups[0]
            If StringInStr($aGroups[$i], $sInput) <> 0 Then $sPartialData &= $aGroups[$i] & "|"
        Next
        GUICtrlSetData($hList, $sPartialData)
    EndIf
EndFunc   ;==>CheckInputText

Func Keywords()
    Local $sGroups
    _AD_Open()
        $aGroups = _AD_GetObjectsInOU("",'(&(objectCategory=group)(displayName=*))', 2, "displayName")
    _AD_Close()
    $sGroups = _ArrayToString($aGroups, "|", 1)
    GUICtrlSetData($hList, $sGroups)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
EndFunc   ;==>Keywords

 

Thanks Subz, although im getting a Subscript used on non-accessible variable. error on line 77

Share this post


Link to post
Share on other sites

#10 ·  Posted

After _AD_Close() add _ArrayDisplay($aGroups) let us know if you see a list of group display names, the first item should be the count.

Share this post


Link to post
Share on other sites

#11 ·  Posted

Thanks Subz!!! I have got that bit working now, although when I try specify i certain OU all i see in the Listbox is -1

 

Do you know where im going wrong?

Cheers

Share this post


Link to post
Share on other sites

#12 ·  Posted

Can only assume you're not using FQDN for example:

_AD_GetObjectsInOU('OU=Groups,DC=domain,DC=com','(&(objectCategory=group)(displayName=*))', 2, "displayName")

 

Share this post


Link to post
Share on other sites

#13 ·  Posted

Im pretty sure i am :/

Could you please give me an example of what it may look like for OU:

one.two.ad\Three\four\five

 

Thanks Again

Share this post


Link to post
Share on other sites

#14 ·  Posted

Not sure what you mean, my last post shows an example of how it should look, can you post what you have tried.

Share this post


Link to post
Share on other sites

#15 ·  Posted

_AD_GetObjectsInOU('OU=three,OU=four,OU=five,DC=one,DC=two,DC=ad','(&(objectCategory=group)(displayName=*))', 2, "displayName")

Would that be correct if i wanted to look into the following OU?

one.two.ad\Three\four\five

Share this post


Link to post
Share on other sites

#16 ·  Posted

Here is an example domain structure:

  • Domain.local (Domain)
  • Groups (OU)
    • Group One (OU)
      • Group Two (OU)
        • Group Three (OU)

If you wanted to only list objects in Group Three OU, your FQDN would be:

_AD_GetObjectsInOU('OU=Group Three,OU=Group Two,OU=Group One,OU=Groups,DC=domain,DC=local','(&(objectCategory=group)(displayName=*))', 2, "displayName")

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Thanks for your reply Subz! Much appreciated...

I think i understand this now, although the below code still doesn't work to show me the groups from my specific OU, although works great when i don't try and specify a OU:

Func Keywords()
    Local $sGroups
    _AD_Open($User,$Pass,"","my.domain.com")
   Global $sOU = "OU=Group Three,OU=Group Two,OU=Group One,OU=Groups,DC=my,DC=domain,DC=com"
 
        $aGroups = _AD_GetObjectsInOU($sOU ,'(&(objectCategory=group)(displayName=*))', 2, "displayName")
    _AD_Close()
_ArrayDisplay($aGroups)
    $sGroups = _ArrayToString($aGroups, "|", 1)
    GUICtrlSetData($List1, $sGroups)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($List1, $iCurrIndex)
EndFunc   ;==>Keywords

This would be for OU: 

my.domain.com\Groups\Group One\Group Two\Group Three

 

Thanks Again

Edited by amphoric

Share this post


Link to post
Share on other sites

#18 ·  Posted

In Active Directory Users and Computers management console, if you right click the OU and select properties and then the Attribute Editor tab, can you verify the distinguishedName attribute is the same as what you're adding.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now