Jump to content

Retrieving Assigned ID with Eval


JoeBar
 Share

Recommended Posts

Hi, i started a dynamically GUI generation with Assign to create TabItems and populate them with some controls (buttons, text, image...).

All is generated through a loop like this :

For $i = 0 To UBound($aCSV) - 1 Step 4
Assign("idtab" & $i, GUICtrlCreateTabItem($aCSV[$i]))
GUISwitch($DltGui, Eval("idtab" & $i))
GUICtrlCreateLabel($aCSV[$i], 170, 95, 393, 44, $SS_CENTER, -1)
GUICtrlSetFont(-1, 28, 400, 0, "Segoe UI Symbol")
GUICtrlSetColor(-1, "0x000080"))
            
Assign("btnopen" & $i, GUICtrlCreateButton("Open the file", 445, 231, 133, 37, -1, -1))
GUICtrlSetFont(-1, 12, 400, 0, "MS Reference Sans Serif")
GUICtrlSetTip(-1, "Open the DLed file")
GUICtrlSetOnEvent(-1, _Launch)
GUICtrlSetState(-1, $GUI_DISABLE)

So logically, the button IDs "Open the file" are being stored in "btnopen0", "btnopen4", "btnopen8", etc...

 

All is working great, but further in a func, i try to enable the "btnopen" & $i with the following command :

GUICtrlSetState(Eval("btnopen" & GUICtrlRead($tab) * 4), $GUI_ENABLE)

I'm trying to retrieve the button ID associated with the current TabItem, to enable it, but i don't manage to.


Have you got an idea ?

Edited by JoeBar
Link to comment
Share on other sites

Try writing "btnopen" & GUICtrlRead($tab) * 4 to the console to see what you variable you are reading. Also, posting a reproduce-able script is helpful since we don't know what $tab is or how it's assigned :)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Nothing is displayed in the console, that's why i'm asking if i'm correct with the good value.

$tab is the handle of the Tab, "idtab" & $i represents the TabItems IDs as you can see.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiListBox.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <String.au3>

Opt("GUIOnEventMode", 1)

Global $DltGui = GUICreate("DltGui",600,315,-1,-1,-1,-1)
Global $tab = GUICtrlCreatetab(20,85,560,200,-1,-1)
GuiCtrlSetState(-1,2048)
GUICtrlCreateTabItem("")
_GUICtrlTab_SetCurFocus($tab,-1)
Global $hConnection = GUICtrlCreateButton("Connection",20,20,135,50,-1,-1)
GUICtrlSetOnEvent(-1,"_Retrieve")
GUICtrlSetFont(-1,15,400,0,"MS Reference Sans Serif")
GUICtrlSetTip(-1,"Retrieve games list")
Global $sconnected = GUICtrlCreateLabel("Not connected",170,40,98,15,-1,-1)
GUICtrlSetFont(-1,10,400,0,"MS Reference Sans Serif")
GUICtrlSetColor(-1,"0xFF0000")
GUICtrlSetBkColor(-1,"-2")
GUICtrlSetTip(-1,"Statut")
Global $Quit = GUICtrlCreateButton("Quit",445,20,133,50,-1,-1)
GUICtrlSetOnEvent(-1,"_Exit")
GUICtrlSetFont(-1,15,400,0,"MS Reference Sans Serif")
GUICtrlSetTip(-1,"Quit")
Global $hprogress = GUICtrlCreateProgress(380,289,198,20,-1,-1)
GUICtrlSetTip(-1,"DL progress")
Global $LabelPourcent = GUICtrlCreateLabel("",462,291,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
_GUICtrlTab_SetCurFocus($tab,0)

That's for the static GUI part : a window with some buttons and a Tab.

Now the GUI generation :

Func _Retrieve()
    Local $dData = InetRead("https:myserver.com/games.csv",  2)
    If $dData = "" Then
        MsgBox(16, "Error", "Access problem")
    Else
        GUICtrlSetData($sconnected, "Connected")
        GUICtrlSetColor($sconnected, $COLOR_GREEN)
        GUICtrlSetState($hConnexion, $GUI_DISABLE)
        Local $sData = BinaryToString($dData)
        Global $aCSV = StringRegExp($sData, ";*\s*([\w :\/\.'!-]+)", 3)
;~      _ArrayDisplay($aCSV, "Capture CSV")
        For $i = 0 To UBound($aCSV) - 1 Step 4
            Assign("idtab" & $i, GUICtrlCreateTabItem($aCSV[$i]))
            GUISwitch($DltGui, Eval("idtab" & $i))
            GUICtrlCreateLabel($aCSV[$i], 170, 95, 393, 44, $SS_CENTER, -1)
            GUICtrlSetFont(-1, 28, 400, 0, "Segoe UI Symbol")
            GUICtrlSetColor(-1, "0x000080")
            Assign("btndl" & $i, GUICtrlCreateButton("DL", 445, 166, 133, 37, -1, -1))
            GUICtrlSetFont(-1, 14, 400, 0, "MS Reference Sans Serif")
            GUICtrlSetTip(-1, "DL the game")
            GUICtrlSetOnEvent(-1, _DL)
            Assign("btnopen" & $i, GUICtrlCreateButton("Open file", 445, 231, 133, 37, -1, -1))
;~          ConsoleWrite(Eval("btnopen" & $i) & @CRLF)
            GUICtrlSetFont(-1, 12, 400, 0, "MS Reference Sans Serif")
            GUICtrlSetTip(-1, "Open the DLed game")
            GUICtrlSetOnEvent(-1, _Launch)
            GUICtrlSetState(-1, $GUI_DISABLE)
        Next
            For $i = 3 To UBound($aCSV) - 1 Step 4
            GUISwitch($DltGui, Eval("idtab" & $i - 3))
            Assign("description" & $i, GUICtrlCreateLabel($aCSV[$i - 1], 170, 145, 271, 123, $SS_CENTER, -1))
            GUICtrlSetFont(-1, 18, 400, 0, "Segoe UI Symbol")
            GUICtrlSetColor(-1, "0x800000")
        Next
        GUICtrlSetState(Eval("idtab0"), $GUI_SHOW)
    EndIf
EndFunc   ;==>_Retrieve


Func _DL()
;~  Local $snamefiledl = _ArrayToString(StringRegExp($aCSV[GUICtrlRead($tab) * 4 + 3], "\/([\w\-\.]+)$", 1))
;~  Local $sSaveFile = FileSaveDialog("Save file", @DesktopDir, "Tous (*.*)", 16, $snamefiledl)
;~  If @error Then Return
;~  Local $urlfilauth =  _StringInsert($aCSV[GUICtrlRead($tab) * 4 + 3], "login:password@", 8)
;~  Local $hInetget = InetGet($urlfilauth, $sSaveFile, 1, 1)
;~ 
;~     Local $size = InetGetSize($urlfilauth)
;~     Local $nowRead = 0
;~     Local $pourcent = 0
;~     GUICtrlSetData($LabelSize,$size)
;~     Do
;~         Sleep(50)
;~         $nowRead = InetGetInfo($hInetget, 0)
;~         $pourcent = Int(($nowRead/$size)*100)
;~         GUICtrlSetData($hprogress,$pourcent)
;~         GUICtrlSetData($LabelPourcent,$pourcent&"%")
;~     Until InetGetInfo($hInetget, 2) ; Check if the download is complete.
;~      InetClose($hInetget)
        
;~      ConsoleWrite(GUICtrlRead($tab) * 4 & @CRLF)

        GUICtrlSetState(Eval("btnopen" & GUICtrlRead($tab) * 4), $GUI_ENABLE)
EndFunc   ;==>_DL

All is working flawlessly, except the last line : GUICtrlSetState(Eval("btnopen" & GUICtrlRead($tab) * 4), $GUI_ENABLE)

 

I'm just trying to enable the formerly disabled button and i don't manage to find the correct button ID of "Open File" on the current selected TabItem ...

Edited by JoeBar
Changed variable names for english understanding
Link to comment
Share on other sites

It's just a tool to DL files from our server to friends, it's for sharing some files (other programs, games, our selfmade musics, etc..) without getting them by a browser or FTP.

It's just to have a nice GUI to do it.

the csv is named games as we began with this, it's not related with game automation or whatever...

 

Thanks for your help.

Edited by JoeBar
Link to comment
Share on other sites

I would suggest using an array for holding your variables at least for next time... they're much easier to debug :)

Is it possible that it's off by one tab? You create a blank tab at the beginning so the button would be switched on the tab after the one you click.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

I know for Arrays, i will do with them next time.

When i click on the button "DL", i check every time all the tabs, and no "Open" buttons are enabled.

I see what you say, but for example GUISwitch($DltGui, Eval("idtab" & $i)) is poiting to the good TabItem each iteration.

Am i obligated to Guiswitch for my GUICtrlSetState to work ?

Link to comment
Share on other sites

Ok, i just found the guilty, when assigning, you must pass the flag 2 to declare Globally.

So just  Assign("btnopen" & $i, GUICtrlCreateButton("Open file", 445, 231, 133, 37, -1, -1), 2) is working.

 

I'm curious at nobody pointed this rapidely, this could be solved just after my 1st post 😊

 

Thanks for everybody's help anyway  :thumbsup:

Link to comment
Share on other sites

2 minutes ago, JoeBar said:

I'm curious at nobody pointed this rapidely

I can't say that I ever use Assign and Eval... there's really not a great reason to use them that I've seen. I thought that they were declared globally by default. Nice job finding it

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

10 hours ago, seadoggie01 said:

I can't say that I ever use Assign and Eval... there's really not a great reason to use them that I've seen. I thought that they were declared globally by default. Nice job finding it

I was looking for a way to dynamically assign vars in a loop, and i've found 2 ways : Assign & Array. As i knew Arrays, i gave a try to Assign.

The main reason it's not used as many of the Arrays is, i think, the syntax which is heavy (excuse my english i'm french) and not user friendly.

Thank you 🤓.

Link to comment
Share on other sites

Yep, that and it's bad style in 99.999% of contexts.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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...