MattHiggs

(solved)Array range exceeded how?

8 posts in this topic

#1 ·  Posted (edited)

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***
#include <ExcelConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
; *** End added by AutoIt3Wrapper ***
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.15.0 (Beta)
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <Constants.au3>
#include <Excel.au3>
#include <GuiListBox.au3>
Local $oExcel = _Excel_Open(Default, Default, Default, Default, True)
opennow ()
If FileExists ( @AppDataDir & "\excelinput.txt" ) Then
    Do
        FileDelete ( @AppDataDir & "\excelinput.txt" )
    Until Not FileExists ( @AppDataDir & "\excelinput.txt" )
EndIf
$setIMEI = True
$row = 0
$name1 = ""
$string = ""
Local $array1[100]
Local $array2[100]
Local $array3[100]
$num = 0
$file = FileOpen ( @AppDataDir & "\excelinput.txt", $FO_APPEND )
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\e0280504\onedrive\always script\form\excel barcode scanner.kxf
$Form1 = GUICreate("Excel Barcode input", 420, 335, 192, 124)
$Input1 = GUICtrlCreateInput("", 48, 64, 305, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NOHIDESEL))
$Button1 = GUICtrlCreateButton("Send to Excel", 168, 264, 89, 33)
GUICtrlSetCursor (-1, 0)
$List1 = GUICtrlCreateList("", 32, 136, 137, 84, BitOR($LBS_NOTIFY,$LBS_HASSTRINGS,$LBS_WANTKEYBOARDINPUT,$WS_VSCROLL,$WS_BORDER))
$List2 = GUICtrlCreateList("", 256, 136, 137, 84, BitOR($LBS_NOTIFY,$LBS_HASSTRINGS,$LBS_WANTKEYBOARDINPUT,$WS_VSCROLL,$WS_BORDER))
$Label1 = GUICtrlCreateLabel("IMEI", 72, 104, 41, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Serial number", 264, 104, 122, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("Scan barcode into below input", 64, 16, 265, 29)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput("", 192, 168, 33, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
$Label4 = GUICtrlCreateLabel("Number", 184, 136, 52, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            If $setIMEI = True Then
                FileWrite ( $file, GUICtrlRead ($Input1) & "|" )
                $setIMEI = False
                $array1[$num] = $num + 1
                $array2[$num] = GUICtrlRead ($Input1)
                GUICtrlSetData ( $Input2, $array1[$num] )
                _GUICtrlListBox_InsertString ( $List1, $array2[$num], 0 )
                GUICtrlSetData($Input1,'')
            ElseIf $setIMEI = False Then
                FileWriteLine ( $file, GUICtrlRead ($Input1) )
                $setIMEI = True
                $array3[$num] = GUICtrlRead ($Input1)
                _GUICtrlListBox_InsertString ( $List2, $array3[$num], 0 )
                GUICtrlSetData($Input1,'')
                $num += 1
            Else
                SetError ( 2 )
            EndIf
        Case $List1
            $count2 = _GUICtrlListBox_GetCount ( $List1 )
            $oldtext = _GUICtrlListBox_GetSelItemsText ( $List1 )
            Local $count = _GUICtrlListBox_GetSelItems ( $List1 )

            $store = $count[1] + 1
            $index = $count2 - $store
            GUICtrlSetData ( $Input2, $array1[$index] )
            _GUICtrlListBox_SetCurSel ( $List2, $index )
        Case $List2
        Case $Button1
            FileClose ( $file )
            Local $aField1[2] = [1, $xlTextFormat]
            Local $aField2[2] = [2, $xlTextFormat]
            Local $aFieldInfo[2] = [$aField1, $aField2]
            If $row = 0 Then
                $oBook = _Excel_BookOpenText ( $oExcel, @AppDataDir & "\excelinput.xlsx", Default, $xlDelimited, Default, True, "|", $aFieldInfo )
            Else
                $oBook = _Excel_BookOpenText ( $oExcel, @AppDataDir & "\excelinput.xlsx", $row, $xlDelimited, Default, True, "|", $aFieldInfo )
            EndIf
            If $name1 == "" Then
                _Excel_BookSave ( $oBook )
            Else
                _Excel_BookSaveAs ( $oBook, $name1 )
            EndIf
            #Region --- CodeWizard generated code Start ---


;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=None
MsgBox($MB_OK,"Saved","Your document has been saved, either overwriting the previouos version of the document or using the file name and location previously selected.")
#EndRegion --- CodeWizard generated code End ---
Exit


    EndSwitch
WEnd


Func opennow()
#Region --- CodeWizard generated code Start ---


;MsgBox features: Title=Yes, Text=Yes, Buttons=Yes and No, Default Button=Second, Icon=None
If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox($MB_YESNO + $MB_DEFBUTTON2,"Edit existing document","Do you have a exsisting document you want to edit?  Select no if you want to create new.")
Select
    Case $iMsgBoxAnswer = $IDYES
        $name = FileOpenDialog ( "Choose your file", @MyDocumentsDir & "\", "Excel workbooks (*.xlsx;*.xlsm;*.xls)" )
        If $name == "" Then
            opennow()
        Else
            #Region --- CodeWizard generated code Start ---
;InputBox features: Title=Yes, Prompt=Yes, Default Text=No, Mandatory
If Not IsDeclared("sInputBoxAnswer") Then Local $sInputBoxAnswer
$sInputBoxAnswer = InputBox("Start Row","Open your excel document and determine at which row you would like the data to start being written, so none of your previous data gets overwritten",""," M","-1","-1","-1","-1")
Select
    Case @Error = 0 And StringIsDigit ( $sInputBoxAnswer ) = 1 ;OK - The string returned is valid
        $row = $sInputBoxAnswer
    Case @Error = 1 ;The Cancel button was pushed
        Exit
    Case Else ;The InputBox failed to open
        Exit
EndSelect
#EndRegion --- CodeWizard generated code End ---

            ;$oWorkbook = _Excel_BookOpen ( $oExcel, $name )
        EndIf


    Case $iMsgBoxAnswer = $IDNO
        $name1 = FileSaveDialog ( "Where would you like to save your new document?", @MyDocumentsDir, "Excel workbooks (*.xlsx;*.xlsm;*.xls)" )
        ;$oWorkbook = _Excel_BookNew ( $oExcel )

EndSelect
#EndRegion --- CodeWizard generated code End ---
EndFunc

When I run the following code, I will select a value from list1, but the variable $count, which should be an array, is saying that the range is exceeded when trying to access the second index (1).  I have used this function before, so I am confused as to why it will not work now.

Edited by MattHiggs

Share this post


Link to post
Share on other sites



MattHiggs,

Clicking on the List control fires the event immediately so that there is no time for the selection to register. You can check this by trying to read the return from _GUICtrlListBox_GetSelItemsText - you get an empty string. So as nothing is actually selected, you do not get any items returned by _GUICtrlListBox_GetSelItems - something you could have checked yourself as $count[0] returns 0.

I suggest you use a button to run the code rather than the click itself.

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

Hmmm.  I see.  In that case, couldn't I just use Sleep() to wait a second or so to give the GUI a chance to read that a list item is selected?  I really would like to have the action trigger when a list item is selected

Share this post


Link to post
Share on other sites

MattHiggs,

Then you will have to look for the click in a message handler and then deal with it in the main loop. Give me a minute or two and I will work up an example.

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

MattHiggs,

I had forgotten how primitive ListBoxes really are - so limited in their ability to interact with the user.

It seems you cannot use _GUICtrlListBox_GetSelItems unless you have set the $LBS_EXTENDEDSEL style allowing multiple selections. But if you only want one selection then you can do this:

Case $cList
    $sText = GUICtrlRead($cList)
    $iIndex = _GUICtrlListBox_FindInText($cList, $sText)
    ConsoleWrite($iIndex & " - " & $sText & @CRLF)

If you want multiple selections then use the above-mentioned style by setting BitOr($LBS_STANDARD, $LBS_EXTENDEDSEL) and then use:

Case $cList
    $aSel = _GUICtrlListBox_GetSelItems($cList)
    _ArrayDisplay($aSel, "", Default, 8)

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

Thank you Melba23.  Sorry for the delay.  Just got back from work.  Thank you for your example.  This really helps me out.

Share this post


Link to post
Share on other sites

@MattHiggs, I can't do what @Melba23 can do, but I started off with Lists (ListBoxes) because I thought they were easy.  I was wrong.  If you are not under pressure to use Lists, rather look into ListViews and ComboBoxes.  They provide awesome functionality. 

https://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateListView.htm


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites
3 hours ago, Skysnake said:

@MattHiggs, I can't do what @Melba23 can do, but I started off with Lists (ListBoxes) because I thought they were easy.  I was wrong.  If you are not under pressure to use Lists, rather look into ListViews and ComboBoxes.  They provide awesome functionality. 

https://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateListView.htm

I appreciate the assist.  However, I have already implemented @Melba23's method and have moved on.  Appreciate it anyway

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