Jump to content

Reloading GUI after iniwrite


Go to solution Solved by BrewManNH,

Recommended Posts

Hi folks,

what im trying to do is to write to my ini file which stores the buttons for the gui and then restart the whole gui. What is happening now is that the GUI is indeed restarting but then not responding to the events, so its just stuck. Any hints what could be the error here?

#include <GUIConstantsEx.au3>
#include <File.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#RequireAdmin

Opt('GuiOnEventMode', 1)

global $input
global $maingui

func _createGUI()
   if FileExists(@ScriptDir&"\regstart.ini") Then
      local $aConfig=IniReadSection(@ScriptDir&"\regstart.ini","config")
      local $aButs[1]
      $maingui=GUICreate('RegLink',200,($aConfig[0][0]+2)*30-15)
      GUISetOnEvent($GUI_EVENT_CLOSE, 'quit')
      Dim $aButs[$aConfig[0][0]]
      For $i = 1 To $aConfig[0][0]
         $butname=$aConfig[$i][0]
         $regkey=$aConfig[$i][1]
         $aButs[$i-1] = GUICtrlCreateButton($butname,10,($i-1)*30+10,180)
         GUICtrlSetOnEvent(-1, 'btnsfunc')
         $x = $i
      next
      $x+=1
      $input=GUICtrlCreateInput("RegKey",10,($x-1)*30+10,145)
      $go=GUICtrlCreateButton("go",160,($x-1)*30+10,30)
      GUICtrlSetOnEvent(-1, 'btnsfunc')
      GUISetState(@SW_SHOW)
   Else
      msgbox(0,"Error","No ini File found!")
      exit
   EndIf
   While 1
      sleep(100)
   WEnd
EndFunc

_createGUI()

Func btnsfunc()
   local $aConfig=IniReadSection(@ScriptDir&"\regstart.ini","config")
   $buttext=GUICtrlRead(@GUI_CtrlId)
   if _ArraySearch($aConfig,$buttext,0,0,0,1) <> -1 then
      $index=_ArraySearch($aConfig,$buttext,0,0,0,1)
      $regkey=$aConfig[$index][1]
      RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", "REG_SZ",$regkey)
      run("regedit")
   Else
      $regkey="Computer\"&GUICtrlRead($input)
      $newbutname=InputBox("Neuer Knopf!","Bitte Namen für den neuen Knopf angeben!")
      IniWrite(@ScriptDir&"\regstart.ini","config",$newbutname,$regkey)
      GUIDelete($maingui)
      _createGUI()
   endif
EndFunc

Func quit()
    Exit
EndFunc
Link to comment
Share on other sites

  • Solution

Try it this way. It will eliminate recursing the functions _CreateGUI and _btnsfunc.

#include <GUIConstantsEx.au3>
#include <File.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#RequireAdmin

Opt('GuiOnEventMode', 1)

Global $input
Global $maingui, $Restart = False
_createGUI()
While 1
    Sleep(100)
    If $Restart Then
        _createGUI()
        $Restart = Not $Restart
    EndIf
WEnd


Func _createGUI()
    If FileExists(@ScriptDir & "\regstart.ini") Then
        Local $aConfig = IniReadSection(@ScriptDir & "\regstart.ini", "config")
        Local $aButs[$aConfig[0][0]]
        $maingui = GUICreate('RegLink', 200, ($aConfig[0][0] + 2) * 30 - 15)
        GUISetOnEvent($GUI_EVENT_CLOSE, 'quit')
        For $i = 1 To $aConfig[0][0]
            $butname = $aConfig[$i][0]
            $regkey = $aConfig[$i][1]
            $aButs[$i - 1] = GUICtrlCreateButton($butname, 10, ($i - 1) * 30 + 10, 180)
            GUICtrlSetOnEvent(-1, 'btnsfunc')
            $x = $i
        Next
        $x += 1
        $input = GUICtrlCreateInput("RegKey", 10, ($x - 1) * 30 + 10, 145)
        $go = GUICtrlCreateButton("go", 160, ($x - 1) * 30 + 10, 30)
        GUICtrlSetOnEvent(-1, 'btnsfunc')
        GUISetState(@SW_SHOW)
    Else
        MsgBox(0, "Error", "No ini File found!")
        Exit
    EndIf
EndFunc   ;==>_createGUI

Func btnsfunc()
    Local $aConfig = IniReadSection(@ScriptDir & "\regstart.ini", "config")
    $buttext = GUICtrlRead(@GUI_CtrlId)
    If _ArraySearch($aConfig, $buttext, 0, 0, 0, 1) <> -1 Then
        $index = _ArraySearch($aConfig, $buttext, 0, 0, 0, 1)
        $regkey = $aConfig[$index][1]
        RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", "REG_SZ", $regkey)
        Run("regedit")
    Else
        $regkey = "Computer\" & GUICtrlRead($input)
        $newbutname = InputBox("Neuer Knopf!", "Bitte Namen für den neuen Knopf angeben!")
        IniWrite(@ScriptDir & "\regstart.ini", "config", $newbutname, $regkey)
        GUIDelete($maingui)
        $Restart = True
    EndIf
EndFunc   ;==>btnsfunc

Func quit()
    Exit
EndFunc   ;==>quit

I didn't test this as I don't have the ini file needed, and I don't want it writing to the registry on my machine even if I did. :)

It should work, but let us know if it doesn't.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hey Brewman,

it worked indeed thank you very much :D Now im gonne try to understand why my approach didnt worked.

edit: I was wondering that it even worked without setting $restart back to true... then I found the part where you were setting it back to true... anyway it is working now I'm even more confused ;)

edit2: try it with setting $restart back to true and deleting _createGUI() from the btsfunc() still working ;)

Greets Tekkion

Edited by Tekkion
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...