Sign in to follow this  
Followers 0
emendelson

Help with returning value from function?

4 posts in this topic

This is a beginner's question that I should have figured out long ago, and I will be very grateful for any help.

I am trying to adapt the excellent code by ScottL and mfacteau described here:

I want to use this GUI in a function that will let the user select a printer and return its name to the main script. All I can figure out to do is this, but it doesn't return the $printername variable to the main script:

#include <Array.au3>
#include <GUIConstants.au3>

Global $currentprinter
Global $defaultprinter
Global $printer_list[1]
Global $printer_list_ext[1]
Global $printer_radio_array[1]
Global $imprimante
Global $printername
Global $gh

GetPrinter()
MsgBox(0, "", "I got the name " & $printername)
Exit

Func GetPrinter()
$regprinters = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices"
$currentprinter = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\", "Device")
$defaultprinter = StringLeft($currentprinter, StringInStr($currentprinter, ",") - 1)
Dim $i = 1
Dim $erreur_reg = False
While Not $erreur_reg
  $imprimante = RegEnumVal($regprinters, $i)
  $erreur_reg = @error
  If Not $erreur_reg Then
   _ArrayAdd($printer_list, $imprimante)
   _ArrayAdd($printer_list_ext, $imprimante & "," & RegRead($regprinters, $imprimante))
  EndIf
  $i = $i + 1
WEnd
_ArrayDelete($printer_list, 0)
_ArrayDelete($printer_list_ext, 0)
If UBound($printer_list) >= 2 Then ;if 2 or more printers available, we show the dialog
  Dim $groupheight = (UBound($printer_list) + 1) * 30
  Dim $guiheight = $groupheight + 50
  Dim $buttontop = $groupheight + 20
  Opt("GUIOnEventMode", 1)
  $gh = GUICreate("Select a printer for this PDF file:", 400, $guiheight)
  Dim $font = "Verdana"
  GUISetFont(10, 400, 0, $font)
  GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
  GUISetFont(10, 400, 0, $font)
  GUICtrlCreateGroup("Available printers:", 10, 10, 380, $groupheight)
  Dim $position_vertical = 0
  For $i = 0 To UBound($printer_list) - 1 Step 1
   GUISetFont(10, 400, 0, $font)
   $position_vertical = $position_vertical + 30
   $radio = GUICtrlCreateRadio($printer_list[$i], 20, $position_vertical, 350, 20)
   _ArrayAdd($printer_radio_array, $radio)
   If $currentprinter = $printer_list_ext[$i] Then
    GUICtrlSetState($radio, $GUI_CHECKED)
   EndIf
  Next
  _ArrayDelete($printer_radio_array, 0)
  GUISetFont(10, 400, 0, $font)
  $okbutton = GUICtrlCreateButton("OK", 10, $buttontop, 50, 25)
  GUICtrlSetOnEvent($okbutton, "OKButton")
  GUISetState()
  While 1
   Sleep(1000)
  WEnd
EndIf
EndFunc   ;==>GetPrinter

Func OKButton()
For $i = 0 To UBound($printer_radio_array) - 1 Step 1
  If GUICtrlRead($printer_radio_array[$i]) = 1 Then
   $printername = StringLeft($printer_list_ext[$i], StringInStr($printer_list_ext[$i], ",") - 1)
  EndIf
Next
GUIDelete($gh)
; MsgBox(0, "", $printername)
Return $printername
EndFunc   ;==>OKButton

Func CLOSEClicked()
$printername = $defaultprinter
GUIDelete($gh)
; MsgBox(0, "", $printername)
Return $printername
EndFunc   ;==>CLOSEClicked

I am clearly missing something that is very elementary. I will be very grateful if someone could tell me what I have left out or done incorrectly. Many thanks for any help.

Share this post


Link to post
Share on other sites



This works but I would look into using guigetmsg()

#include <Array.au3>
#include <GUIConstants.au3>
Global $currentprinter
Global $defaultprinter
Global $printer_list[1]
Global $printer_list_ext[1]
Global $printer_radio_array[1]
Global $imprimante
Global $printername
Global $gh
GetPrinter()
Do
Sleep(100)
Until $printername
MsgBox(0,'',$printername)
Func GetPrinter()
$regprinters = "HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionDevices"
$currentprinter = RegRead("HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionWindows", "Device")
$defaultprinter = StringLeft($currentprinter, StringInStr($currentprinter, ",") - 1)
Dim $i = 1
Dim $erreur_reg = False
While Not $erreur_reg
  $imprimante = RegEnumVal($regprinters, $i)
  $erreur_reg = @error
  If Not $erreur_reg Then
   _ArrayAdd($printer_list, $imprimante)
   _ArrayAdd($printer_list_ext, $imprimante & "," & RegRead($regprinters, $imprimante))
  EndIf
  $i = $i + 1
WEnd
_ArrayDelete($printer_list, 0)
_ArrayDelete($printer_list_ext, 0)
If UBound($printer_list) >= 2 Then ;if 2 or more printers available, we show the dialog
  Dim $groupheight = (UBound($printer_list) + 1) * 30
  Dim $guiheight = $groupheight + 50
  Dim $buttontop = $groupheight + 20
  Opt("GUIOnEventMode", 1)
  $gh = GUICreate("Select a printer for this PDF file:", 400, $guiheight)
  Dim $font = "Verdana"
  GUISetFont(10, 400, 0, $font)
  GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
  GUISetFont(10, 400, 0, $font)
  GUICtrlCreateGroup("Available printers:", 10, 10, 380, $groupheight)
  Dim $position_vertical = 0
  For $i = 0 To UBound($printer_list) - 1 Step 1
   GUISetFont(10, 400, 0, $font)
   $position_vertical = $position_vertical + 30
   $radio = GUICtrlCreateRadio($printer_list[$i], 20, $position_vertical, 350, 20)
   _ArrayAdd($printer_radio_array, $radio)
   If $currentprinter = $printer_list_ext[$i] Then
    GUICtrlSetState($radio, $GUI_CHECKED)
   EndIf
  Next
  _ArrayDelete($printer_radio_array, 0)
  GUISetFont(10, 400, 0, $font)
  $okbutton = GUICtrlCreateButton("OK", 10, $buttontop, 50, 25)
  GUICtrlSetOnEvent($okbutton, "OKButton")
  GUISetState()
EndIf
EndFunc   ;==>GetPrinter
Func OKButton()
For $i = 0 To UBound($printer_radio_array) - 1 Step 1
  If GUICtrlRead($printer_radio_array[$i]) = 1 Then
   $printername = StringLeft($printer_list_ext[$i], StringInStr($printer_list_ext[$i], ",") - 1)
  EndIf
Next
GUIDelete($gh)
; MsgBox(0, "", $printername)
  ConsoleWrite($printername & @LF)
EndFunc   ;==>OKButton
Func CLOSEClicked()
$printername = $defaultprinter
GUIDelete($gh)
; MsgBox(0, "", $printername)
Return $printername
EndFunc   ;==>CLOSEClicked
1 person likes this

Share this post


Link to post
Share on other sites

Thank you! That definitely works, but GUIGetMsg will be faster and more efficient, so I'll learn how to use it.

I've learned not to be surprised at the fast, helpful answers around here. Thank you again

Share this post


Link to post
Share on other sites

No problem :)

No matter how silly the problem may be, if you post your code and describe the problem your having (as you just did), you'll always find some someone around here to help.

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  
Followers 0