ViciousXUSMC

GUI ComboBox to Triggle Update on Other ComboBox

8 posts in this topic

#1 ·  Posted (edited)

Was working on a script to let users easily globally add printers from the print servers.

In the past just used a .bat file but decided to wrap it up in AutoIt to make it easier to use and more versitile.

I have the code working as is, but I would like to learn something and take it to the next level.

I have 2 goals, one is probably easy, the other maybe a bit harder.

First goal is instead of having the user manually type the printer name into the inputbox, would it be possible to have the combobox for the server selection populate the 2nd box (would change the inputbox to a combobox) with the list of printer choices.

I found good info here: '?do=embed' frameborder='0' data-embedContent>>

However I would not be using a static location I would need the first combobox to be chosen and then in real time update the 2nd box based on its value, It would of course need to continue to change in real time incase a selection change is made before they hit the button to exictute the rest of the script.

The 2nd goal, once a printer is chosen to add in the 2nd combobox would it be easy enough to display the detailed information about that printer in the GUI so that they can see its location, etc and not only the printershare name.

I am not sure how much information can be gathered from a printer before it is added explorer only lets you see Comments, but it would be great if possible to view more than that.

Here is the code so far.

The GUI

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Include <_NTServices.au3>

Global $aArray[2] = ["vprintserv","vprintserv2"]
$sList = ""
For $i = 0 To UBound($aArray) -1
    $sList &= "|" & $aArray[$i]
    Next

#Region ### START Koda GUI section ### Form=C:\Users\it022565\Desktop\Add Printer GUI\Printer Form.kxf
$Form1 = GUICreate("Black Magic Automation", 351, 159, 192, 124)
$Combo1 = GUICtrlCreateCombo("print server", 16, 72, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData($Combo1, $sList, "vprintserv2")
$Input1 = GUICtrlCreateInput("printer name", 184, 72, 145, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Check to Delete", 8, 112, 97, 17)
$Button1 = GUICtrlCreateButton("GO!", 242, 111, 75, 25)
$Label1 = GUICtrlCreateLabel("Print Server", 56, 48, 59, 17)
$Label2 = GUICtrlCreateLabel("Printer", 240, 48, 34, 17)
$Label3 = GUICtrlCreateLabel("Add/Delete Global Printer GUI", 24, 8, 281, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            ExitLoop
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Local $addel = GUICtrlRead($Checkbox1)
Local $printer = GUICtrlRead($Input1)
Local $serv = GUICtrlRead($Combo1)

If $addel = 1 Then
    $addel = "gd"
Else
    $addel = "ga"
EndIf


FileInstall("globalprinter.bat", @TempDir & "\globalprinter.bat", 1)

_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$addel$",$addel)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$serv$",$serv)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$printer$",$printer)
ShellExecuteWait(@TempDir & "\globalprinter.bat")

sleep(1000)

If _ServiceStatus("Spooler") <> "Stopped" Then _ServiceStop("Spooler")
While 1
    Sleep(200)
    If _ServiceStatus("Spooler") = "Stopped" Then ExitLoop
WEnd
_ServiceStart("Spooler")

The .bat file I am executing

cd /d "%~dp0"

rundll32 printui.dll PrintUIEntry /$addel$ /n"\\$serv$\$printer$"

** Yes I know I am strange running a .bat intead of using @ComSpec its just a preference for me though in this case @ComSpec should have been easy enough. 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites



Something I stumbled upon:

>Printers management with WMI by jguinch


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

ViciousXUSMC,

I showed how to update a second combo depending on the selection in the first in this thread. I hope it proves useful. :)

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

#4 ·  Posted (edited)

ViciousXUSMC,

I showed how to update a second combo depending on the selection in the first in this thread. I hope it proves useful. :)

M23

 

I think that is what I need, I just gotta figure out how you made the magic happen and change as needed for my case :)

I see that if I put $combo1 as a Case I can then put GUICtrlSetData() for $Combo2 and this will update its information.

The part to figure out now is how to make each selection in the first combo create different information in combo2 and also it seems to keep appending for me over and over in my simple test rather than reset itself when combo1 makes a selection. 

Edit: Fixed Appending issue just needed a leading "|"

Edit2: More problems than I anticipated cant seem to get _FileListToArray to work on a network share like the printserver, normally accessed with just "vprintserv2" from explorer.  I wonder if I would have to parse CMD results or map a drive letter temporarily for this to work. 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

ViciousXUSMC,

 

how to make each selection in the first combo create different information in combo2

Read the combo selection and use some form of Switch/Select structure to determine what to do with that specific value. :)

 

it seems to keep appending for me over and over

Simple to fix - read the Help file "Remarks" for GUICtrlSetData. ;)

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

#6 ·  Posted (edited)

So here is how I accomplished this.

I could not find a good way to read the printers on the printserver in real time, and it was not even easy to find a way to simply export them to a file.

I ended up using Print Management and exporting a .txt file to get a list of printers.

 

I then incorporated those text files with _FileReadtoArray()

 

I could clean up varible names here but I was in testing mode.  As for the Case/Switch that part seems to be working well, not sure if there is a smarter/better way.

I decided to keep the text files @ScriptDir so that the user can add/delete as needed.

 

Please have a look and critique

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Include <_NTServices.au3>

Global $aArray[3] = ["Print Server","vprintserv","vprintserv2"]
$sList = ""
For $i = 0 To UBound($aArray) -1
    $sList &= "|" & $aArray[$i]
Next

Global $aArray2
$sList2 = ""
_FileReadToArray("vprintserv.txt", $aArray2, 0)
for $i = 0 to UBound($aArray2) -1
    $sList2 &= "|" & $aArray2[$i]
next

Global $aArray3
$sList3 = ""
_FileReadToArray("vprintserv2.txt", $aArray3, 0)
for $i = 0 to UBound($aArray3) -1
    $sList3 &= "|" & $aArray3[$i]
Next





#Region ### START Koda GUI section ### Form=C:\Users\it022565\Desktop\Add Printer GUI\Printer Form.kxf
$Form1 = GUICreate("Black Magic Automation", 351, 159, 192, 124)
$Combo1 = GUICtrlCreateCombo("Print Server", 16, 72, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData($Combo1, $sList)
$Combo2 = GUICtrlCreateCombo("Printer Name", 184, 72, 145, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Check to Delete", 8, 112, 97, 17)
$Button1 = GUICtrlCreateButton("GO!", 242, 111, 75, 25)
$Label1 = GUICtrlCreateLabel("Print Server", 56, 48, 59, 17)
$Label2 = GUICtrlCreateLabel("Printer", 240, 48, 34, 17)
$Label3 = GUICtrlCreateLabel("Add/Delete Global Printer GUI", 24, 8, 281, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
GUICtrlSetData($Combo1, "Print Server")
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            ExitLoop
        Case $Combo1
            Switch $Combo1
                Case GUICtrlRead($Combo1) = "vprintserv"
                     GUICtrlSetData($Combo2,$sList2, "Printer Name")
                Case GUICtrlRead($Combo1) = "vprintserv2"
                     GUICtrlSetData($Combo2,$sList3, "Printer Name")
                Case Else
                     GUICtrlSetData($Combo2, "|Printer Name", "Printer Name")
            EndSwitch
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Local $addel = GUICtrlRead($Checkbox1)
Local $printer = GUICtrlRead($Combo2)
Local $serv = GUICtrlRead($Combo1)

If $addel = 1 Then
    $addel = "gd"
Else
    $addel = "ga"
EndIf


FileInstall("globalprinter.bat", @TempDir & "\globalprinter.bat", 1)

_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$addel$",$addel)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$serv$",$serv)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$printer$",$printer)
ShellExecuteWait(@TempDir & "\globalprinter.bat")

sleep(1000)

If _ServiceStatus("Spooler") <> "Stopped" Then _ServiceStop("Spooler")
While 1
    Sleep(200)
    If _ServiceStatus("Spooler") = "Stopped" Then ExitLoop
WEnd
_ServiceStart("Spooler")
I think the last thing I'll try to impliment is instead of statically defining the choices for the first combobox, have it read the current script dir for .txt files and resolve those to choices so you can add/remove print server lists without recompiling the .exe

Edit: done! last small issue the FileList comes in with the .txt extension since its one long string not sure how to trim this off, and also I have the "Print Server" with no .txt extension (though I could add it manually in the string)

I would rather find a way to cut off the file extension than to change my case statement to look for it.

 

Global $aArray = _FileListToArray(@ScriptDir, "*.txt")
$sList = ""
For $i = 1 To UBound($aArray) -1
    $sList &= "|" & $aArray[$i]
Next
Edit sorted that with _ArrayTrim Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

Ok so this is my stopping point. I have an idea and close to making it happen but need a little help.

At this point I can drop new .txt files into the scritdir and get new listings from the first combobox.

The problem is that I will not have any arrays, strings or anything setup for those to have them populate into the 2nd combobox. Same no Case statement to check for that server name.

I have a feeling I can actually get everything I wanted with a couple more loops.

A loop that runs through all the filenames stored in $aArray and has the Case/Switch check on Combo one. When it matches a name it should pass that file name into the next loop where it creates the string for Combo2.

When completed this will be a self evolving script and new .txt files can be edited/deleted/modified as the servers change and that would be great.

My closest attempt so far was trying to make a loop under my Case $Combo1 line but the way it worked out in my head is there is no "matching" to the array results it would just cycle through every server name and results file repeatedly :/

I think I may need a 2 dimentional array to do this?

Code So Far:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Include <_NTServices.au3>

Global $aArray = _FileListToArray(@ScriptDir, "*.txt")
_ArrayTrim($aArray, 4, 1)
$sList = ""
For $i = 1 To UBound($aArray) -1
    $sList &= "|" & $aArray[$i]
Next

Global $aArray2
$sList2 = ""
_FileReadToArray("vprintserv.txt", $aArray2, 0)
for $i = 0 to UBound($aArray2) -1
    $sList2 &= "|" & $aArray2[$i]
next

Global $aArray3
$sList3 = ""
_FileReadToArray("vprintserv2.txt", $aArray3, 0)
for $i = 0 to UBound($aArray3) -1
    $sList3 &= "|" & $aArray3[$i]
Next


#Region ### START Koda GUI section ### Form=C:\Users\it022565\Desktop\Add Printer GUI\Printer Form.kxf
$Form1 = GUICreate("Black Magic Automation", 351, 159, 192, 124)
$Combo1 = GUICtrlCreateCombo("Print Server", 16, 72, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData($Combo1, $sList)
$Combo2 = GUICtrlCreateCombo("Printer Name", 184, 72, 145, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Check to Delete", 8, 112, 97, 17)
$Button1 = GUICtrlCreateButton("GO!", 242, 111, 75, 25)
$Label1 = GUICtrlCreateLabel("Print Server", 56, 48, 59, 17)
$Label2 = GUICtrlCreateLabel("Printer", 240, 48, 34, 17)
$Label3 = GUICtrlCreateLabel("Add/Delete Global Printer GUI", 24, 8, 281, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
GUICtrlSetData($Combo1, "Print Server", "Print Server")
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            ExitLoop
        Case $Combo1
            Switch $Combo1
                Case GUICtrlRead($Combo1) = "vprintserv"
                     GUICtrlSetData($Combo2,$sList2, "Printer Name")
                Case GUICtrlRead($Combo1) = "vprintserv2"
                     GUICtrlSetData($Combo2,$sList3, "Printer Name")
                Case Else
                     GUICtrlSetData($Combo2, "|Printer Name", "Printer Name")
            EndSwitch
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Local $addel = GUICtrlRead($Checkbox1)
Local $printer = GUICtrlRead($Combo2)
Local $serv = GUICtrlRead($Combo1)

If $addel = 1 Then
    $addel = "gd"
Else
    $addel = "ga"
EndIf


FileInstall("globalprinter.bat", @TempDir & "\globalprinter.bat", 1)

_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$addel$",$addel)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$serv$",$serv)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$printer$",$printer)
ShellExecuteWait(@TempDir & "\globalprinter.bat")

sleep(1000)

If _ServiceStatus("Spooler") <> "Stopped" Then _ServiceStop("Spooler")
While 1
    Sleep(200)
    If _ServiceStatus("Spooler") = "Stopped" Then ExitLoop
WEnd
_ServiceStart("Spooler")

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Almost :/

I get new files to populate to $Combo1 I get $Combo2 to update with the list of printers from that txt file but I have an issue with vprintserv2 having the printer list for both vprintserv and vprintserv2. It must have something to do with how stringinstr is matching? But I tested just the matches first to ensure that was not the case.

I tried as many string functions as I could find, and its getting pretty messy now.

Here is the half working mess.

 

$nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            ExitLoop
        Case $Combo1
            $sLoopList = ""
        Global $aLoopArray
    For $i = 0 to UBound($aArray) -1
        If StringinStr(GUICtrlRead($Combo1), $aArray[$i]) Then
    _FileReadToArray($aArrayBak[$i], $aLoopArray, 0)
    For $i2 = 0 to UBound($aLoopArray) -1
        $sLoopList &= "|" & $aLoopArray[$i2]

    Next

    If StringinStr(GUICtrlRead($Combo1), $aArray[$i]) Then GUICtrlSetData($Combo2, "|" & $sLoopList)

Edit**

OMG I did it! :) The easier way to do this was to call a function rather than having it all embeded.

I am sure the code is sloppy and terrible, that I broke a lot of best practices but it works!

Feel free to look it over and give me some pointers on how to clean it up.

Regards

Final Code

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Include <_NTServices.au3>

Global $aArray = _FileListToArray(@ScriptDir, "*.txt")
Global $aArrayBak = $aArray
_ArrayTrim($aArray, 4, 1)
$sList = ""
For $i = 1 To UBound($aArray) -1
    $sList &= "|" & $aArray[$i]
Next

Func makelist()
    Global $aLoopArray
    Global $sLoopList = ""
    _FileReadToArray($aArrayBak[$i2], $aLoopArray, 0)
    for $i = 0 to UBound($aLoopArray) -1
        $sLoopList &= "|" & $aLoopArray[$i]
    Next
        Return $sLoopList
    EndFunc



#Region ### START Koda GUI section ### Form=C:\Users\it022565\Desktop\Add Printer GUI\Printer Form.kxf
$Form1 = GUICreate("Black Magic Automation", 351, 159, 192, 124)
$Combo1 = GUICtrlCreateCombo("Print Server", 16, 72, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData($Combo1, $sList)
$Combo2 = GUICtrlCreateCombo("Printer Name", 184, 72, 145, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Check to Delete", 8, 112, 97, 17)
$Button1 = GUICtrlCreateButton("GO!", 242, 111, 75, 25)
$Label1 = GUICtrlCreateLabel("Print Server", 56, 48, 59, 17)
$Label2 = GUICtrlCreateLabel("Printer", 240, 48, 34, 17)
$Label3 = GUICtrlCreateLabel("Add/Delete Global Printer GUI", 24, 8, 281, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
GUICtrlSetData($Combo1, "Print Server", "Print Server")
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            ExitLoop

        Case $Combo1
            For $i2 = 1 to UBound($aArray) -1
    If StringinStr(GUICtrlRead($Combo1), $aArray[$i2]) Then GUICtrlSetData($Combo2, makelist(), "Printer Name")
    If GUICtrlRead($Combo1) = "Print Server" then GUICtrlSetData($Combo2, "|Printer Name", "Printer Name")
    Next

        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Local $addel = GUICtrlRead($Checkbox1)
Local $printer = GUICtrlRead($Combo2)
Local $serv = GUICtrlRead($Combo1)

If $addel = 1 Then
    $addel = "gd"
Else
    $addel = "ga"
EndIf


FileInstall("globalprinter.bat", @TempDir & "\globalprinter.bat", 1)

_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$addel$",$addel)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$serv$",$serv)
_ReplaceStringInFile(@TempDir & "\globalprinter.bat", "$printer$",$printer)
ShellExecuteWait(@TempDir & "\globalprinter.bat")

sleep(1000)

If _ServiceStatus("Spooler") <> "Stopped" Then _ServiceStop("Spooler")
While 1
    Sleep(200)
    If _ServiceStatus("Spooler") = "Stopped" Then ExitLoop
WEnd
_ServiceStart("Spooler")
Edited by ViciousXUSMC

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