Jump to content
Sign in to follow this  
Inpho

Arrays - am I doing it right for this situation?

Recommended Posts

Inpho

Hi all, I swear I got better since my last thread...

 

I'm making a stocktaking script for a small from-home business I have fixing and refurb'ing mobile 'phones and would love some input on whether I'm using arrays correctly for this. I'm especially concerned about running into memory issues. Do I need to clear memory more? Am I making any drastically nooby mistakes (You can probably tell I've rushed my learning stages :p)?

Script is unfinished so you'll see some references to stuff that isn't implemented yet.

Also, I had some trouble "deactivating" the guictrlcreatedummy while the Edit ($NOTbox) is in focus, you can see my workaround. My problem is that I want the Enter key to cycle through Input fields, except when the Edit is in focus; where I want it to CRLF. Is there a better way to do this other than basically destroy the contents then recreate with an appened CRLF?

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\ICONS\Stock.ico
#AutoIt3Wrapper_Outfile=StockTake.Exe
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <Constants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIButton.au3>
#include <WinAPI.au3>
#include <Array.au3>

Opt("trayicondebug", 1)
$create = 0
Global $Fields[15]
$db = @ScriptDir & "/Jobs/"

$GUI = GUICreate("StockTake", 650, 337, -1, -1)
$Label14 = GUICtrlCreateLabel("StoreKeeper", 16, 11, 568, 17, $SS_CENTER)
$IMinlbl = GUICtrlCreateLabel("IMEI In:", 16, 40, 41, 17)
$Fields[1] = GUICtrlCreateInput("", 64, 38, 113, 21, $SS_CENTER)
$SNinlbl = GUICtrlCreateLabel("SN In:", 16, 64, 34, 17)
$Fields[2] = GUICtrlCreateInput("", 64, 62, 113, 21, $SS_CENTER)
$MDLlbl = GUICtrlCreateLabel("Model:", 16, 88, 36, 17)
$Fields[3] = GUICtrlCreateInput("", 64, 86, 241, 21, $SS_CENTER)
$PRODinlbl = GUICtrlCreateLabel("Prod In:", 16, 112, 69, 17)
$Fields[4] = GUICtrlCreateInput("", 64, 110, 113, 21, $SS_CENTER)
$SWinlbl = GUICtrlCreateLabel("SW In:", 16, 136, 77, 17)
$Fields[5] = GUICtrlCreateInput("", 64, 134, 113, 21, $SS_CENTER)
$SRClbl = GUICtrlCreateLabel("Source:", 16, 160, 41, 17)
$Fields[6] = GUICtrlCreateInput("", 72, 158, 257, 21, $SS_CENTER)
$PRCinlbl = GUICtrlCreateLabel("Price In:", 16, 184, 43, 17)
$Fields[7] = GUICtrlCreateInput("", 72, 182, 65, 21, $SS_CENTER)
$PTSlbl = GUICtrlCreateLabel("Parts:", 16, 208, 31, 17)
$Fields[8] = GUICtrlCreateInput("", 72, 206, 65, 21, $SS_CENTER)
$PRCoutlbl = GUICtrlCreateLabel("Price Out:", 16, 232, 51, 17)
$Fields[9] = GUICtrlCreateInput("", 72, 230, 65, 21, $SS_CENTER)
$IMoutlbl = GUICtrlCreateLabel("IMEI Out:", 192, 40, 49, 17)
$Fields[10] = GUICtrlCreateInput("", 248, 38, 113, 21, $SS_CENTER)
$SNoutlbl = GUICtrlCreateLabel("SN Out:", 192, 64, 42, 17)
$Fields[11] = GUICtrlCreateInput("", 248, 62, 113, 21, $SS_CENTER)
$PRODoutlbl = GUICtrlCreateLabel("Prod Out:", 192, 112, 49, 17)
$Fields[12] = GUICtrlCreateInput("", 248, 110, 113, 21, $SS_CENTER)
$SWoutlbl = GUICtrlCreateLabel("SW Out:", 192, 136, 49, 17)
$Fields[13] = GUICtrlCreateInput("", 248, 134, 113, 21, $SS_CENTER)
$STSlbl = GUICtrlCreateLabel("Status:", 156, 208, 35, 17)
$Fields[14] = GUICtrlCreateInput("", 200, 206, 100, 17, $SS_CENTER)
$NOTbox = GUICtrlCreateEdit("", 400, 40, 220, 275, $ES_AUTOvSCROLL)

$OPNbut = GUICtrlCreateButton("Open", 75, 272, 43, 41)
$UPDbut = GUICtrlCreateButton("Update", 175, 272, 43, 41)
$CLRbut = GUICtrlCreateButton("Clear", 275, 272, 43, 41)


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


GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OPNbut
            Global $i = 2
            Global $i2 = 2
            Open()
        Case $UPDbut
            Update()
        Case $CLRbut
            Global $i = 1
            Global $i2 = 1
            Clear()
        Case $dum
            If $create = 1 Then
                Send("{TAB}")
                ContinueLoop
            EndIf
            FieldMove()
    EndSwitch
WEnd

Func Open()
    $IMinput = GUICtrlRead($Fields[1])
    If $IMinput = "" Then Return
    $data = IniReadSection($db & $IMinput & ".ini", "MAIN")
    If @error <> 0 Then
        $data = ""
        TrayTip("Creating Record", "IMEI Number doesn't exist, creating job.", 30)
        $create = 1
        Return
    EndIf
    Clear()
    $notesdir = $db & GUICtrlRead($Fields[1]) & ".txt"
    $noteshandle = FileOpen($notesdir, 0)
    GUICtrlSetData($NOTbox, FileRead($notesdir))
    FileClose($noteshandle)
    Global $i = 1
    For $i = 1 To 14
        GUICtrlSetData($Fields[$i], $data[$i][1])
    Next
EndFunc   ;==>Open

Func Update()
    $IMinput = GUICtrlRead($Fields[1])
    If $IMinput = "" Then Return
    $toud = MsgBox(4, "Confirm", "Sure to update with these details?", "", $GUI)
    If $toud = 7 Then
        Return
    Else
        FileCopy("template.ini", $db & $IMinput & ".ini", 9)
        $data2 = IniReadSection($db & $IMinput & ".ini", "MAIN")
        Global $i = 1
        Global $i2 = 1
        For $i = $i2 To 14
            $fieldsread = GUICtrlRead($Fields[$i])
            IniWrite($db & $IMinput & ".ini", "MAIN", $data2[$i][0], $fieldsread)
        Next
    EndIf
    $notesread = GUICtrlRead($NOTbox)
    $noteshandle = FileOpen($db & $IMinput & ".txt", 10)
    FileWrite($noteshandle, $notesread)
    FileClose($noteshandle)
    $create = 0
EndFunc   ;==>Update

Func Clear()
    For $i = $i2 to 14
        GUICtrlSetData($Fields[$i], "")
    Next
    GUICtrlSetData($NOTbox, "")
    Return
EndFunc

Func FieldMove()
    $fieldchk = ControlGetFocus($GUI)
    If $fieldchk = "Edit1" Then
        ControlClick("StockTake", "", $OPNbut)
        GUICtrlSetState($Fields[2], 256)
        Return
    EndIf
    If $fieldchk = "Button1" Then
        ControlClick("StockTake", "", $OPNbut)
        GUICtrlSetState($Fields[2], 256)
        Return
    EndIf
    If $fieldchk = "Button2" Then
        ControlClick("StockTake", "", $UPDbut)
        GUICtrlSetState($Fields[2], 256)
        Return
    EndIf
    If $fieldchk = "Edit15" Then
        GUICtrlSetData($NOTbox, GUICtrlRead($NOTbox) & @CRLF)
    Else
        Send("{TAB}")
    EndIf
EndFunc   ;==>FieldMove

.ini is in this format:

[MAIN]
IMin =353737xxxxxxxxxx
SNin =R28xxxxxxxx
MDL =GT-I9195 (Galaxy S4 Mini LTE)
PRODin =GT-I9195ZKABTU
SWin =BTUCNK1
SRC =ebay, xxxxxxxxxxxxxxxxxxxxxxxxxxx
PRCin =20.50
PTS =52.89
PRCout =125
IMout =353737xxxxxxxxxx
SNout =R28xxxxxxxx
PRODout =GT-I9195ZKABTU
SWout =OXACOA2
STS =SOLD

 

Thankyou in advance

Share this post


Link to post
Share on other sites
JohnOne

Regarding TAB perhaps something like...

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OPNbut
            Global $i = 2
            Global $i2 = 2
            Open()
        Case $UPDbut
            Update()
        Case $CLRbut
            Global $i = 1
            Global $i2 = 1
            Clear()
        Case $dum
            If ControlGetFocus($GUI) <> "Edit15" Then
                If $create = 1 Then
                    Send("{TAB}")
                    ContinueLoop
                EndIf
            EndIf
            FieldMove()
    EndSwitch
WEnd

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Inpho

Thanks for the reply, I tried this way but when focussed on the edit(Edit15) box, enter doesnt perform CR-LF, I'm guessing because accelerator is set for Enter. I tried to manually force a CRLF a bunch of ways.

Maybe I attack it from another angle. Is there a way to check if Enter key is pressed while in a GUI loop without using GUISetAccelerator? I tried _IsPressed but gave unpredictable results.

Edited by Inpho

Share this post


Link to post
Share on other sites
Inpho

I went without Accelerator in the end, as it seemed to lock out adding @CRLF in Edit15.

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\ICONS\Stock.ico
#AutoIt3Wrapper_Outfile=StockTake.Exe
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <Constants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIButton.au3>
#include <WinAPI.au3>
#include <Array.au3>
#include <Misc.au3>
Global $hDLL = DllOpen("user32.dll")
Opt("trayicondebug", 1)
$create = 0
Global $Fields[15]
$db = @ScriptDir & "/Jobs/"

$GUI = GUICreate("StockTake", 650, 337, -1, -1)
$Label14 = GUICtrlCreateLabel("StoreKeeper", 16, 11, 568, 17, $SS_CENTER)
$IMinlbl = GUICtrlCreateLabel("IMEI In:", 16, 40, 41, 17)
$Fields[1] = GUICtrlCreateInput("", 64, 38, 113, 21, $SS_CENTER)
$SNinlbl = GUICtrlCreateLabel("SN In:", 16, 64, 34, 17)
$Fields[2] = GUICtrlCreateInput("", 64, 62, 113, 21, $SS_CENTER)
$MDLlbl = GUICtrlCreateLabel("Model:", 16, 88, 36, 17)
$Fields[3] = GUICtrlCreateInput("", 64, 86, 241, 21, $SS_CENTER)
$PRODinlbl = GUICtrlCreateLabel("Prod In:", 16, 112, 69, 17)
$Fields[4] = GUICtrlCreateInput("", 64, 110, 113, 21, $SS_CENTER)
$SWinlbl = GUICtrlCreateLabel("SW In:", 16, 136, 77, 17)
$Fields[5] = GUICtrlCreateInput("", 64, 134, 113, 21, $SS_CENTER)
$SRClbl = GUICtrlCreateLabel("Source:", 16, 160, 41, 17)
$Fields[6] = GUICtrlCreateInput("", 72, 158, 257, 21, $SS_CENTER)
$PRCinlbl = GUICtrlCreateLabel("Price In:", 16, 184, 43, 17)
$Fields[7] = GUICtrlCreateInput("", 72, 182, 65, 21, $SS_CENTER)
$PTSlbl = GUICtrlCreateLabel("Parts:", 16, 208, 31, 17)
$Fields[8] = GUICtrlCreateInput("", 72, 206, 65, 21, $SS_CENTER)
$PRCoutlbl = GUICtrlCreateLabel("Price Out:", 16, 232, 51, 17)
$Fields[9] = GUICtrlCreateInput("", 72, 230, 65, 21, $SS_CENTER)
$IMoutlbl = GUICtrlCreateLabel("IMEI Out:", 192, 40, 49, 17)
$Fields[10] = GUICtrlCreateInput("", 248, 38, 113, 21, $SS_CENTER)
$SNoutlbl = GUICtrlCreateLabel("SN Out:", 192, 64, 42, 17)
$Fields[11] = GUICtrlCreateInput("", 248, 62, 113, 21, $SS_CENTER)
$PRODoutlbl = GUICtrlCreateLabel("Prod Out:", 192, 112, 49, 17)
$Fields[12] = GUICtrlCreateInput("", 248, 110, 113, 21, $SS_CENTER)
$SWoutlbl = GUICtrlCreateLabel("SW Out:", 192, 136, 49, 17)
$Fields[13] = GUICtrlCreateInput("", 248, 134, 113, 21, $SS_CENTER)
$STSlbl = GUICtrlCreateLabel("Status:", 156, 208, 35, 17)
$Fields[14] = GUICtrlCreateInput("", 200, 206, 100, 17, $SS_CENTER)
$NOTbox = GUICtrlCreateEdit("", 400, 40, 220, 275, $ES_AUTOvSCROLL)

$OPNbut = GUICtrlCreateButton("Open", 75, 272, 43, 41)
$UPDbut = GUICtrlCreateButton("Update", 175, 272, 43, 41)
$CLRbut = GUICtrlCreateButton("Clear", 275, 272, 43, 41)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            DllClose($hDLL)
            Exit
        Case $OPNbut
            Global $i = 2
            Global $i2 = 2
            Open()
            GUICtrlSetState($NOTbox, 256)
        Case $UPDbut
            Update()
        Case $CLRbut
            Global $i = 1
            Global $i2 = 1
            Clear()
            GUICtrlSetState($Fields[1], 256)
    EndSwitch
    If _IsPressed("0D", $hDLL) Then
        FieldMove()
        Send("{ENTER UP}")
    EndIf
WEnd

Func Open()
    $IMinput = GUICtrlRead($Fields[1])
    If $IMinput = "" Then Return
    $data = IniReadSection($db & $IMinput & ".ini", "MAIN")
    If @error <> 0 Then
        $data = ""
        TrayTip("Creating Record", "IMEI Number doesn't exist, creating job.", 30)
        $create = 1
        Return
    EndIf
    Clear()
    $notesdir = $db & GUICtrlRead($Fields[1]) & ".txt"
    $noteshandle = FileOpen($notesdir, 0)
    GUICtrlSetData($NOTbox, FileRead($notesdir))
    FileClose($noteshandle)
    Global $i = 1
    For $i = 1 To 14
        GUICtrlSetData($Fields[$i], $data[$i][1])
    Next
EndFunc   ;==>Open

Func Update()
    $IMinput = GUICtrlRead($Fields[1])
    If $IMinput = "" Then Return
    $toud = MsgBox(4, "Confirm", "Sure to update with these details?", "", $GUI)
    If $toud = 7 Then
        Return
    Else
        FileCopy("template.ini", $db & $IMinput & ".ini", 9)
        $data2 = IniReadSection($db & $IMinput & ".ini", "MAIN")
        Global $i = 1
        Global $i2 = 1
        For $i = $i2 To 14
            $fieldsread = GUICtrlRead($Fields[$i])
            IniWrite($db & $IMinput & ".ini", "MAIN", $data2[$i][0], $fieldsread)
        Next
    EndIf
    $notesread = GUICtrlRead($NOTbox)
    $noteshandle = FileOpen($db & $IMinput & ".txt", 10)
    FileWrite($noteshandle, $notesread)
    FileClose($noteshandle)
    $create = 0
EndFunc   ;==>Update

Func Clear()
    For $i = $i2 to 14
        GUICtrlSetData($Fields[$i], "")
    Next
    GUICtrlSetData($NOTbox, "")
    Return
EndFunc

Func FieldMove()
    If $create = 1 Then
        Send("{TAB}")
        Return
    EndIf
    $fieldchk = ControlGetFocus($GUI)
    If $fieldchk = "Edit1" Then
        ControlClick("StockTake", "", $OPNbut)
        GUICtrlSetState($Fields[2], 256)
        Return
    ElseIf $fieldchk = "Button1" Then
        ControlClick("StockTake", "", $OPNbut)
        GUICtrlSetState($Fields[2], 256)
        Return
    ElseIf $fieldchk = "Button2" Then
        ControlClick("StockTake", "", $UPDbut)
        GUICtrlSetState($Fields[2], 256)
        Return
    ElseIf $fieldchk = "Button3" Then
        ControlClick("StockTake", "", $CLRbut)
        Return
    ElseIf $fieldchk = "Edit15" Then
        Send(@CRLF)
    ElseIf $fieldchk = "Edit14" Then
        Send("{TAB}")
        Send("{DOWN}")
    Else
        Send("{TAB}")
    EndIf
EndFunc   ;==>FieldMove

 

Share this post


Link to post
Share on other sites
Inpho

Its to enable me to move fields automatically. I enter a phones IMEI and press Enter, if it doesnt exist in my database, then I enter the details and use Enter to advance through fields. I could jsut get used to using the physical Tab button to advance through fields but I would much rather find a way to use Enter instead.

Edited by Inpho

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  

×