Jump to content

Dynamic Button Creation


Recommended Posts

I have a script that I want to be able to tweak the menu buttons with an INI file. My problem is that I don't know how to set the variables to be read in the while loop. Any ideas?

#include <GuiConstants.au3>

$iniFile = @ScriptDir & "\menue.ini"
$iniSections = IniReadSectionNames($iniFile)

$GUI_W = "290"
$GUI_H = "285"
GUICreate("MaosnNet Admin", $GUI_W, $GUI_H, (@DesktopWidth-$GUI_W)/10, (@DesktopHeight-$GUI_H)/10)
;Local Admin group and positioning
$laHors = "8"     ;horizontal positioning of Local Admin group box
$laVert = "140"   ;vertical positioning of Local Admin group box
$laHorsBut = "88" ;horzontal button positioning compared to Local Admin group box & used as multable
$laVertBut = "30" ;vertical button positioning compared to Local Admin group box & used as multable
$laButSize = "75, 25" ;Size of buttons in Local Admin group box
GUICtrlCreateGroup("Local Administration", $laHors, $laVert, 265, 138)

If @error Then 
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    $h = 0
    $v = 0
    For $i = 1 To $iniSections[0]
        if $iniSections[$i] <> "Setup" then
            $buttonName = $iniSections[$i]
            ;$Button_$iniSections[$i] = GUICtrlCreateButton("Gateway", 8 + $laHors + $laHorsBut * 0, 16 + $laVert + $laVertBut * 0, $laButSize)
            GUICtrlCreateButton($buttonName, 8 + $laHors + $laHorsBut * $h, 16 + $laVert + $laVertBut * $v, $laButSize)
            $h = $h + 1
            if $h > 2 Then
                $h = 0
                $v = $v + 1
            EndIf
        EndIf
    Next
EndIf

GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
            
    EndSwitch
WEnd

a sample of the ini file:

[setup]

[Gateway]

cmd=c:\Program Files\Mozilla Firefox\firefox.exe

parm=https://gateway.domain.com:445

[L. Station]

cmd=c:\Program Files\Mozilla Firefox\firefox.exe

parm=http://drive.domain.com

[D-Link]

cmd=c:\Program Files\Mozilla Firefox\firefox.exe

parm=http://dlink.domain.com

[Webmin 1]

cmd=c:\Program Files\Mozilla Firefox\firefox.exe

parm=https://opn01.domain.com:10000"

[Webmin 2]

cmd=c:\Program Files\Mozilla Firefox\firefox.exe

parm=https://opn02.domain.com:10000"

[Map Network]

cmd=

parm=

[unmap Network]

cmd=

parm=

[Putty]

cmd=putty.exe

parm=

[Winscape]

cmd=winscp.exe

parm=

Edited by masonje
Link to comment
Share on other sites

What can I see is that your script is creating all buttons without any ID on them:

$buttonName = $iniSections[$i]
            ;$Button_$iniSections[$i] = GUICtrlCreateButton("Gateway", 8 + $laHors + $laHorsBut * 0, 16 + $laVert + $laVertBut * 0, $laButSize)
            GUICtrlCreateButton($buttonName, 8 + $laHors + $laHorsBut * $h, 16 + $laVert + $laVertBut * $v, $laButSize)
oÝ÷ Ù.Â)e綬²('¶¯z¼ºÛh©âuéíø®X¤y«­¢+ØÀÌØíÕÑѽ¹9µôÀÌØí¥¹¥MÑ¥½¹ÍlÀÌØí¥t(ÀÌØí  ÕÑѽ¹lÀÌØí¥tôU%
Ñɱ
ÉÑ    ÕÑѽ¸ ÀÌØíÕÑѽ¹9µ°à¬ÀÌØí±!½É̬ÀÌØí±!½ÉÍ  ÕШÀÌØí °ÄجÀÌØí±YÉЬÀÌØí±YÉÑ ÕШÀÌØíØ°ÀÌØí±    ÕÑM¥é¤(

If you do it this way then you will be able to use thr buttons ID ($Button[1], $Button[2] ...) into the While loop.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

If you do it this way then you will be able to use thr buttons ID ($Button[1], $Button[2] ...) into the While loop.

Cool, I get that, but how do I set up the while loop to check for variables that have not yet been created
Link to comment
Share on other sites

but how do I set up the while loop to check for variables that have not yet been created

I don't know exactly what do you mean - should I understand that your ini file will have new sections added during script run and it will need to create new buttons?

... I don't know ... could you explain "not yet been created" please?

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

#include <GUIConstants.au3>

;  Example 1

GUICreate('Example 1')

Dim $btns[5]
$btns[0] = GUICtrlCreateButton('Button 1', 10, 10, 60, 26)
For $i = 1 To 4
    $btns[$i] = GUICtrlCreateButton('Button ' & $i + 1, 10, $i * 36 + 10, 60, 26)
Next

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
        Case $btns[0] To $btns[4]
            MsgBox(0, '', GUICtrlRead($msg))
    EndSwitch
WEnd

;  Example 2

GUICreate('Example 2')

Dim $btns[5]
$btns[0] = GUICtrlCreateButton('Button 1', 10, 10, 60, 26)
For $i = 1 To 4
    $btns[$i] = GUICtrlCreateButton('Button ' & $i + 1, 10, $i * 36 + 10, 60, 26)
Next

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $btns[0]
            MsgBox(0, '', 'Button 1')
        Case $btns[1]
            MsgBox(0, '', 'Button 2')
        Case $btns[2]
            MsgBox(0, '', 'Button 3')
        Case $btns[3]
            MsgBox(0, '', 'Button 4')
        Case $btns[4]
            MsgBox(0, '', 'Button 5')
    EndSwitch
WEnd

Link to comment
Share on other sites

OK, getting warmer. I think something like example 1 works best, but 2 is too fixed. There has to be some way to get the msg read correct. I took example 1 concept and incorporated it into my existing script, but I keep getting GUICtrlRead($msg) = 0 and it's stuck throwing the MsgBox(0, '', 0) ever iteration of the while loop. Thoughts?...

#include <GuiConstants.au3>
#include <Array.au3>

$iniFile = @ScriptDir & "\menue.ini"
$iniSections = IniReadSectionNames($iniFile)
Dim $buttonID[20]

$GUI_W = "290"
$GUI_H = "285"
GUICreate("MaosnNet Admin", $GUI_W, $GUI_H, (@DesktopWidth-$GUI_W)/10, (@DesktopHeight-$GUI_H)/10)
;Local Admin group and positioning
$laHors = "8"     ;horizontal positioning of Local Admin group box
$laVert = "140"   ;vertical positioning of Local Admin group box
$laHorsBut = "88" ;horzontal button positioning compared to Local Admin group box & used as multable
$laVertBut = "30" ;vertical button positioning compared to Local Admin group box & used as multable
$laButSize = "75, 25" ;Size of buttons in Local Admin group box
GUICtrlCreateGroup("Local Administration", $laHors, $laVert, 265, 138)

If @error Then 
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    $h = 0
    $v = 0
    $x = 1
    For $i = 1 To $iniSections[0]
        if $iniSections[$i] <> "Setup" then
            $buttonID[$i] = GUICtrlCreateButton($iniSections[$i], 8 + $laHors + $laHorsBut * $h, 16 + $laVert + $laVertBut * $v, $laButSize)
            $h = $h + 1
            if $h > 2 Then
                $h = 0
                $v = $v + 1
            EndIf
            ;MsgBox(0, $i,$iniSections[$i])
        EndIf
    Next
EndIf

GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
            
        Case $buttonID[1] To $buttonID[$iniSections[0]]
            MsgBox(0, '', GUICtrlRead($msg))
            
        Case Else
            ;;;
            
    EndSwitch
WEnd

Edit...Found it!!!

Case $buttonID[2] To $buttonID[$iniSections[0]]

Array 0 & 1 are not used. I tried to change $i to 0 and the case statement to Case $buttonID[1] To $buttonID[$iniSections[0]] but that ended up with the same thing. Ultimately it's because I'm skipping section "setup" that was jacking with me. Thanks for your help guys (or gals).

Edited by masonje
Link to comment
Share on other sites

You could also use oneventmode...

#include <GUIConstants.au3>

Opt('GuiOnEventMode', 1)

GUICreate('Example 1')
GUISetOnEvent($GUI_EVENT_CLOSE, 'quit')

For $i = 1 To 5
    GUICtrlCreateButton('Button ' & $i, 10, $i * 36 + 10, 60, 26)
    GUICtrlSetOnEvent(-1, 'btnsfunc')
Next

GUISetState()

While 1
    Sleep(100)
WEnd

Func btnsfunc()
    MsgBox(0, '', GUICtrlRead(@GUI_CtrlId))
EndFunc

Func quit()
    Exit
EndFunc
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...