Patyadigg Posted May 21, 2016 Share Posted May 21, 2016 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. expandcollapse popup#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 More sharing options...
Patyadigg Posted May 21, 2016 Author Share Posted May 21, 2016 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. expandcollapse popup#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 More sharing options...
LarsJ Posted May 21, 2016 Share Posted May 21, 2016 Use an extra function and a local static instead of a global in this way: expandcollapse popup#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 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 21, 2016 Moderators Share Posted May 21, 2016 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: expandcollapse popup#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 Synapsee 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Patyadigg Posted May 24, 2016 Author Share Posted May 24, 2016 Thanks for both the tip and the solution. Appreciate the separate approach you have to the problem at hand. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now