Search code examples
user-interfaceautoit

Is there a way of clearing the window and rebuilding it?


My application dynamically generates a GUI window with tabs, labels, buttons and checkboxes from an .ini file. I want to load a different .ini file and recreate the GUI window but I can't find any function to clear the existing window.

Opt('GUIOnEventMode', 1)                    ;   use events instead of loop
AutoitSetOption('ExpandVarStrings',1)       ;   enable ' … $var$ … '

Global $gui
Global $buttons[]

init('first.ini')

Func init($ini)
    ;   If $gui Then GUIDelete($gui)        ;   deletes gui, but won’t re-create
    If $gui Then GUIDelete($gui)            ;   actually, this is OK
    $gui = GUICreate('Test', 200, 160)
    GUISetOnEvent(-3, 'quit')   ;   quit on close
    Local $tabControl = GUICtrlCreateTab(10, 10, 180, 100)

    Local $sectionNames = IniReadSectionNames($ini)
    For $i = 1 to $sectionNames[0]
        Local $section = $sectionNames[$i]
        Local $values = IniReadSection($ini, $section)
        Local $tab = GUICtrlCreateTabItem($section)
        For $j = 1 to $values[0][0]
            GUICtrlCreateButton($values[$j][0], 20, 20+$j*20)
        Next
    Next

    GUICtrlCreateTabItem("")
    GUISwitch($gui)

    Local $button = GUICtrlCreateButton('Doit', 20, 120)
    GUICtrlSetOnEvent($button, "doitButton")

    GUISetState(@SW_SHOW,$gui)      ;   show gui (moved from below)

EndFunc

Func quit()
    GUIDelete($gui)
    Exit
EndFunc

Func doitButton()
    init('second.ini')
EndFunc

;   GUISetState(@SW_SHOW,$gui)  ;   delete from here


While 1
    Sleep(1)
WEnd
GUIDelete($gui)

The ini files:

;   first.ini
[Fruit]
    Apple=1
    Banana=2
[Animals]
    Aardvark=1

and

;   second.ini
[Instruments]
    Accordion=1
    Banjo=2
    Cor Anglais=3

I thought GUICreate() would replace the window, but it doesn't. Then I thought if I start with GUIDelete() it might do the job, but the application just hangs. Is there a way of clearing the window and rebuilding it?

Edit Thanks to @user4157124’s answer below, I have added the changes in the above code with comments.


Solution

  • but the application just hangs.

    The script waits for events on the second GUI which does get created but isn't set to @SW_SHOW. Uncomment If $gui Then GUIDelete($gui) and move GUISetState(@SW_SHOW,$gui) into init($ini).