Jump to content

Read input box using GUIOnEventMode1 without global variables


Recommended Posts

Hello, I have already searched for this for a couple hours.  

I to make my own inputbox function.  Upon pressing enter, I want my function to return the value inside the input text box.

It is painfully simple how to do with without the option GUIOnEventMode=1, but if I didn't have that option set, I have to rely on a global variable.

 

While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop
      Case $buttondef
         Return GUICtrlRead($Input)
   EndSwitch
WEnd

 

Since the main loop of my code already has GUIOnEventMode already set, Im afraid that toggling it off/on might clear my other events already set.  Because of this, I'm trying to make an inputbox that doesn't have to change that toggle.  Here is my simple code.

#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>
Opt("GUIOnEventMode", 1)

$result = _InputBox("title", "input", 123)                                  ;input box function test
MsgBox(0,"", $result)                                                       ;result of input box function
Exit

While(1)
   ;Main Prog Here
   Sleep(1000)
WEnd

func _InputBox($title, $prompt, $default)
   $width = 150
   $height = 60
   $InputForm = GUICreate($title, $width, $height)                          ;create input box
      GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
      GUISetBkColor(0xA6CAF0)
   $InputLabel = GUICtrlCreateLabel($prompt, 20, 5, $width-40)              ;prompt for text
   $Input = GUICtrlCreateInput($default, 20, 25, $width-40)                 ;text input area
   $buttondef = GUICtrlCreateButton("OK", 0, 0, 1, 1,$BS_DEFPUSHBUTTON)     ;invisible button linked to enter keypress
      GUICtrlSetOnEvent($buttondef, "_Enter")
   GUISetState(@SW_SHOW)

   Global $pressed = false                                                  ;toggled true upon enter keypress from GUICtrlSetOnEvent _Enter function
   While not($pressed)
      Sleep(100)
   WEnd

   $result = GUICtrlRead($Input)                                            ;save input
   GUIDelete($InputForm)                                                    ;delete inputbox
   return $result                                                           ;return input
EndFunc

func _Enter()
   $pressed = true
EndFunc

func _Close()
   Exit
EndFunc

While it is working, it is messy due to relying on a global variable :/

 

How can I avoid this?

 


 
Link to comment
Share on other sites

EDIT: SPELLING ERRORS FIXED, COULD NOT FIND EDIT BUTTON

Hello, I have already searched for a solution to this for a couple hours.  

I'm trying to make my own inputbox function.  Upon pressing enter, I want my function to return the value inside the text box.

It is painfully simple how to do with without the option GUIOnEventMode=1, but if I have that option set, I have to rely on a global variable to get it working.

 

While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop
      Case $buttondef
         Return GUICtrlRead($Input)
   EndSwitch
WEnd

 

Since the main loop of my code already has GUIOnEventMode already set, Im afraid that toggling it off/on might clear my other events that are already set.  Because of this, I'm trying to make an inputbox that doesn't have to toggle the event mode.  Here is my simple example.

#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>
Opt("GUIOnEventMode", 1)

$result = _InputBox("title", "input", 123)                                  ;input box function test
MsgBox(0,"", $result)                                                       ;result of input box function
Exit

While(1)
   ;Main Prog Here
   Sleep(1000)
WEnd

func _InputBox($title, $prompt, $default)
   $width = 150
   $height = 60
   $InputForm = GUICreate($title, $width, $height)                          ;create input box
      GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
      GUISetBkColor(0xA6CAF0)
   $InputLabel = GUICtrlCreateLabel($prompt, 20, 5, $width-40)              ;prompt for text
   $Input = GUICtrlCreateInput($default, 20, 25, $width-40)                 ;text input area
   $buttondef = GUICtrlCreateButton("OK", 0, 0, 1, 1,$BS_DEFPUSHBUTTON)     ;invisible button linked to enter keypress
      GUICtrlSetOnEvent($buttondef, "_Enter")
   GUISetState(@SW_SHOW)

   Global $pressed = false                                                  ;toggled true upon enter keypress from GUICtrlSetOnEvent _Enter function
   While not($pressed)
      Sleep(100)
   WEnd

   $result = GUICtrlRead($Input)                                            ;save input
   GUIDelete($InputForm)                                                    ;delete inputbox
   return $result                                                           ;return input
EndFunc

func _Enter()
   $pressed = true
EndFunc

func _Close()
   Exit
EndFunc

While my example is working, I think it is messy because I relied on a global variable :/

 

How can I avoid this and make the function self contained?

Link to comment
Share on other sites

Use an extra function and a local static instead of a global in this way:

#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>
Opt("GUIOnEventMode", 1)

$result = _InputBox("title", "input", 123)                                  ;input box function test
MsgBox(0,"", $result)                                                       ;result of input box function
Exit

While(1)
  ;Main Prog Here
  Sleep(1000)
WEnd

func _InputBox($title, $prompt, $default)
  $width = 150
  $height = 60
  $InputForm = GUICreate($title, $width, $height)                          ;create input box
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
    GUISetBkColor(0xA6CAF0)
  $InputLabel = GUICtrlCreateLabel($prompt, 20, 5, $width-40)              ;prompt for text
  $Input = GUICtrlCreateInput($default, 20, 25, $width-40)                 ;text input area
  $buttondef = GUICtrlCreateButton("OK", 0, 0, 1, 1,$BS_DEFPUSHBUTTON)     ;invisible button linked to enter keypress
    GUICtrlSetOnEvent($buttondef, "_Enter")
  GUISetState(@SW_SHOW)

  _Pressed( 1 )
  Local $pressed = false                                                  ;toggled true upon enter keypress from GUICtrlSetOnEvent _Enter function
  While not($pressed)
    $pressed = _Pressed( 2 )
    Sleep(10)
  WEnd

  $result = GUICtrlRead($Input)                                            ;save input
  GUIDelete($InputForm)                                                    ;delete inputbox
  return $result                                                           ;return input
EndFunc

Func _Enter()
  _Pressed()
EndFunc

Func _Pressed( $mode = 0 )
  Local Static $pressed
  Switch $mode
    Case 0 ; Set
      $pressed = True
    Case 1 ; Reset
      $pressed = False
    Case 2 ; Get
      Return $pressed
  EndSwitch
EndFunc

func _Close()
  Exit
EndFunc

 

Link to comment
Share on other sites

  • Moderators

Patyadigg,

Unless you are expecting other user input during its existence (which seems unlikely) I would just switch to MessageLoop mode for the duration of the input:

#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

$result = _InputBox("title", "input", 123) ;input box function test
MsgBox(0, "", $result) ;result of input box function
Exit

While (1)
    ;Main Prog Here
    Sleep(1000)
WEnd

Func _InputBox($title, $prompt, $default)

    Local $result = ""

    $width = 150
    $height = 60
    $InputForm = GUICreate($title, $width, $height) ;create input box
    GUISetBkColor(0xA6CAF0)
    $InputLabel = GUICtrlCreateLabel($prompt, 20, 5, $width - 40) ;prompt for text
    $Input = GUICtrlCreateInput($default, 20, 25, $width - 40) ;text input area

    $cDummy = GUICtrlCreateDummy()

    GUISetState(@SW_SHOW)

    Local $aAccelKeys[1][2] = [["{ENTER}", $cDummy]]
    GUISetAccelerators($aAccelKeys, $InputForm)

    Opt("GUIOnEventMode", 0)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $cDummy
                $result = GUICtrlRead($Input)
                ExitLoop
        EndSwitch
    WEnd

    Opt("GUIOnEventMode", 1)

    Return $result ;return input
EndFunc   ;==>_InputBox

It simplifies the code a lot.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...