Jump to content
Sign in to follow this  
matthardwick

Populating drop down with array vals, and using them

Recommended Posts

matthardwick

OK so I have an array as follows;

[0]|CE - Personnel (Penmorfa)|Canon iR 5870C EUR|131072
[1]|DECS - Early Years (FenlinFach)|HP LaserJet 2015|0
[2]|DESH - Reception (Morgan Street)|HP LaserJet 2055dn|0
[3]|DF - Recovery (Canolfan Rheidol)|HP LaserJet 4055|0
[4]|DHPW - Estates (Regent Street)|Konica Minolta BizHub C451|131072
[5]|DHPW - General Office (County Hall)|Konica Minolta BizHub C650|131072
[6]|DSS - Adult Team North (Gorwelian)|HP LaserJet 1320|0

I want to populate a drop down list with the first column, I can do this no problem with an array walk, but how would I then get the information in the other two columns to display in the GUI?

So if I selected "DHPW - General Office (County Hall)" from the drop down, I would want to display information in the window from the rest of the array - so here the model number and current status code would be "Konica Minolta BizHub C650" and "131072" respectivly.

I would appreciate some guidance as I haven't the foggiest where to start.

Thanks in advance.

Share this post


Link to post
Share on other sites
Melba23

matthardwick,

Welcome to the AutoIt forum. :idea:

It is as easy to code it as to explain: :(

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

Global $aList[7]
$aList[0] = "|CE - Personnel (Penmorfa)|Canon iR 5870C EUR|131072"
$aList[1] = "|DECS - Early Years (FenlinFach)|HP LaserJet 2015|0"
$aList[2] = "|DESH - Reception (Morgan Street)|HP LaserJet 2055dn|0"
$aList[3] = "|DF - Recovery (Canolfan Rheidol)|HP LaserJet 4055|0"
$aList[4] = "|DHPW - Estates (Regent Street)|Konica Minolta BizHub C451|131072"
$aList[5] = "|DHPW - General Office (County Hall)|Konica Minolta BizHub C650|131072"
$aList[6] = "|DSS - Adult Team North (Gorwelian)|HP LaserJet 1320|0"

$hGUI = GUICreate("Test", 500, 500)

$hCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)

$hLabel = GUICtrlCreateLabel("", 10, 50, 200, 200)

GUISetState()

$sList = "|"
For $i = 0 To UBound($aList) -1
    $aSplit = StringSplit($aList[$i], "|")
    $sList &= $aSplit[2] & "|"
Next

GUICtrlSetData($hCombo, $sList)

$sCurrSel = ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If GUICtrlRead($hCombo) <> $sCurrSel Then
        ; Set new selection value
        $sCurrSel = GUICtrlRead($hCombo)
        ; Search for the value in the array
        $iIndex = _ArraySearch($aList, $sCurrSel, 0, 0, 0, 1)
        ; Read teh whoel array element
        $sText = $aList[$iIndex]
        ; Split it
        $aSplit = StringSplit($sText, "|")
        ; Extract the data you need
        GUICtrlSetData($hLabel, $aSplit[3] & @CRLF & $aSplit[4])
    EndIf

WEnd

Please ask if anything is unclear. :)

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
matthardwick

I could have sworn I replied to this sooner, I just came back and saw I had not!

Thanks for your help, but I should have been more clear in my first post - that array is dynamic, I just gave a small selection of possible entries in the array - is there a way to make the array in the format you suggested?

Share this post


Link to post
Share on other sites
Melba23

matthardwick,

there a way to make the array in the format you suggested?

Of course. :)

But it all depends on how your data starts. Do you already have lists or is it added dynamically?

If, as I suspect, it is the latter then you will need a GUI with some inputs to receive the data and a button to run some code to add the contents of the inputs to the array. You can then refill the combo with the new list.

Give it a go - you know where we are when you run into problems. :idea:

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
matthardwick

matthardwick,

Of course. :)

But it all depends on how your data starts. Do you already have lists or is it added dynamically?

If, as I suspect, it is the latter then you will need a GUI with some inputs to receive the data and a button to run some code to add the contents of the inputs to the array. You can then refill the combo with the new list.

Give it a go - you know where we are when you run into problems. :idea:

M23

I will show you how I am building the array, that might be easier than explaining! :( Basically I am building it out of WMI output from a print server.

$strComputer = "cereprint"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer WHERE Shared = True", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    
Local $printerTotal = 0
Local $printerArray[1][3]
    

For $objItem In $colItems
        $printerArray[$printerTotal][0] = $objItem.Name
        $printerArray[$printerTotal][1] = $objItem.Comment
        $printerArray[$printerTotal][2] = $objItem.PrinterState
        $printerTotal += 1
ReDim $printerArray[$printerTotal + 1][3]
Next
ReDim $printerArray[$printerTotal][3] ; kill that dead record at the end of the array.

If I _ArrayDisplay then I get that output which I originally showed you in the first post.

Edited by matthardwick

Share this post


Link to post
Share on other sites
Melba23

matthardwick,

that might be easier than explaining

Touché! :(

If you have the values in an array then life is really easy - why bother to change it to the format you had at the beginning? Just leave it as an array:

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

Global $printerarray[7][3] = [ _
    ["CE - Personnel (Penmorfa)", "Canon iR 5870C EUR", "131072"], _
    ["DECS - Early Years (FenlinFach)", "HP LaserJet 2015", "0"], _
    ["DESH - Reception (Morgan Street)", "HP LaserJet 2055dn", "0"], _
    ["DF - Recovery (Canolfan Rheidol)", "HP LaserJet 4055", "0"], _
    ["DHPW - Estates (Regent Street)", "Konica Minolta BizHub C451", "131072"], _
    ["DHPW - General Office (County Hall)", "Konica Minolta BizHub C650", "131072"], _
    ["DSS - Adult Team North (Gorwelian)", "HP LaserJet 1320", "0"]]

$hGUI = GUICreate("Test", 500, 500)

$hCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)

$hLabel = GUICtrlCreateLabel("", 10, 50, 200, 200)

GUISetState()

$sList = "|"
For $i = 0 To UBound($printerarray) -1
    $sList &= $printerarray[$i][0] & "|"
Next

GUICtrlSetData($hCombo, $sList)

$sCurrSel = ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If GUICtrlRead($hCombo) <> $sCurrSel Then
        ; Set new selection value
        $sCurrSel = GUICtrlRead($hCombo)
        ; Search for the value in the array
        $iIndex = _ArraySearch($printerarray, $sCurrSel, 0, 0, 0, 1)
        ; Read the other array elements
        $sText_1 = $printerarray[$iIndex][1]
        $sText_2 = $printerarray[$iIndex][2]
        ; Show the data
        GUICtrlSetData($hLabel, $sText_1 & @CRLF & $sText_2)
    EndIf

WEnd

How is that? :)

I am off to watch the election results now, but I have some ideas to smarten up your array filling - look in again tomorrow. :idea:

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
matthardwick

I don't get what you mean keep the original format... what I pasted originally was output from that array I built using WMI.

I don't want to have to change my code, everytime anything is changed on the server... :idea:

Share this post


Link to post
Share on other sites
Melba23

matthardwick,

No, you have misunderstood - all I did in the script above was declare an array to match the one you created via WMI (because I obviously cannot create it directly as you did :) ). Just put your WMI code where I declared the array:

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

; get your array here
$strComputer = "cereprint"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer WHERE Shared = True", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    
Local $printerTotal = 0
Local $printerArray[1][3]

For $objItem In $colItems
        $printerArray[$printerTotal][0] = $objItem.Name
        $printerArray[$printerTotal][1] = $objItem.Comment
        $printerArray[$printerTotal][2] = $objItem.PrinterState
        $printerTotal += 1
ReDim $printerArray[$printerTotal + 1][3]
Next
ReDim $printerArray[$printerTotal][3] ; kill that dead record at the end of the array.

; create the GUI
$hGUI = GUICreate("Test", 500, 500)

$hCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)

$hLabel = GUICtrlCreateLabel("", 10, 50, 200, 200)

GUISetState()

; now use the array you have just generated to populate the combo
$sList = "|"
For $i = 0 To UBound($printerarray) -1
    $sList &= $printerarray[$i][0] & "|"
Next

GUICtrlSetData($hCombo, $sList)

$sCurrSel = ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If GUICtrlRead($hCombo) <> $sCurrSel Then
        ; Set new selection value
        $sCurrSel = GUICtrlRead($hCombo)
        ; Search for the value in the array
        $iIndex = _ArraySearch($printerarray, $sCurrSel, 0, 0, 0, 1)
        ; Read the other array elements
        $sText_1 = $printerarray[$iIndex][1]
        $sText_2 = $printerarray[$iIndex][2]
        ; Show the data
        GUICtrlSetData($hLabel, $sText_1 & @CRLF & $sText_2)
    EndIf

WEnd

Of course, if you do not want to generate the array every time you run the script, then you will need to save it somewhere. Use _FileWriteFromArray to save it and then _FileReadToArray to get it back again. :idea:

Is that closer to what you want?

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
matthardwick

OMG I am so stupid, I didn't scroll so didn't full read what you put. That code is perfect... Lol thanks so much for your help.

I feel guilty for just taking code from you - I was just after pointing in teh right direction :idea:

All I gotta do now is figure out how to change an icon acording to status code, and then add the printer on the press of the button.

Thansk again!

Edited by matthardwick

Share this post


Link to post
Share on other sites
Melba23

matthardwick

I feel guilty for just taking code from you

No reason to - it is why I posted it here! :idea:

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

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.