Sign in to follow this  
Followers 0
Topher

Help with arrayed buttons

15 posts in this topic

Well, I'm right at the beginning of a project and already missing the obvious (I hope)

This creates a square of 9 x 9 pushbuttons.

The routine for registering whether one of the buttons has been pressed is not working.

(I've left out a couple of lengthy irrelevant routines but the idea is here)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#Include <Misc.au3>
#include <WindowsConstants.au3>

Global Const $Tribe = 1
Global Const $Clan   = 2
Global Const $HunterN  = 3
Global Const $HunterS  = 4
Global Const $HunterE  = 5
Global Const $HunterW  = 6
Global Const $HunterNE = 7
Global Const $HunterNW = 8
Global Const $HunterSE = 9
Global Const $HunterSW = 10
Global Const $Red      = 0
Global Const $Green = 100

Global Const $Black  = 0  ; no action
Global Const $Yellow = 1  ; selected
Global Const $Cyan   = 2  ; option

Dim $Board[9][9]
Dim $hBoard[9][9]
Dim $Color[9][9]
Dim $hColor[9][9]

$hGUI = GUICreate("IX ver 0.01", 68 * 9 + 300, 68 * 9)
InitializeBoard()
GUISetState()
DrawBoard()

; the main loop
While True
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then
  OnExit()
EndIf
For $side = 0 to 8
  For $fore = 0 to 8
   If $msg = $hBoard[$fore][$side] Then
    $Color[$fore][$side] = $Yellow
    DrawBoard()
   EndIf
  Next
Next
Sleep(25)
WEnd

Func OnExit()
GUIDelete($hGUI)
Exit
EndFunc

Func WipeColor()
For $x = 0 to 8
  For $y = 0 to 8
   $Color[$x][$y] = 0
  Next
Next
DrawBoard()
EndFunc

Func DrawBoard()
GUISetState(@SW_LOCK)
For $x = 0 to 8
  For $y = 0 to 8
   GUICtrlSetImage ( $hColor[$x][$y], ColorName($Color[$x][$y]))
   GUICtrlSetImage ( $hBoard[$x][$y], PieceName($Board[$x][$y]))
  Next
Next
GUISetState(@SW_UNLOCK)
EndFunc

; create the board picture handles
; and set all pieces to opening positions
Func InitializeBoard()
For $x = 0 To 8
  For $y = 0 To 8
   $Color[$x][$y]  = $Black
   $hColor[$x][$y] = GUICtrlCreatePic(".\Images\Black.bmp", $x * 68 + 150, $y * 68, 68, 68)
   $Board[$x][$y] = 0  ; empty squares
   $hBoard[$x][$y] = GUICtrlCreateButton("", $x * 68 + 2 + 150, $y * 68 + 2, 64, 64, BitOR($BS_BITMAP, $BS_FLAT))
  Next
Next
For $x = 1 to 7 Step 2
  $Board[0][$x] = $Tribe + $Red
  $Board[8][$x] = $Tribe + $Green
Next
EndFunc

[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

Share this post


Link to post
Share on other sites



"is not working"? Could you please be a bit more precise?

Does your script crash? Does it act unexpected? Do you get an error message?

More information is needed ...


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Sorry, it does not register the button click at all. just keeps puttering along as if I had done nothing.


[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

try replacing dim with global on declaration of $hboard[][] or pass the array byref by changing the func InitializeBoard() to func InitializeBoard(byref $hboard) then pass $hboard in the call to InitializeBoard

edit: make all of the arrays you're using in InitializeBoard global or pass them all byref to the function, if your changing the values of the arrays and expect to keep the results.

Looks good by the way. Let me know if that's not the answer, but it looks like the scope is wrong for the $hboard to be usable. Other than that it looks good.

The control graphic will likely be displayed but the control_id or handle would be lost. So you wouldn't be able to click the button.

Edited by Xandy

I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

You have stripped down your script a bit too much. Some functions are missing.

What we need is a script as small as possible which lets us reproduce the problem you have.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You have stripped down your script a bit too much. Some functions are missing.

What we need is a script as small as possible which lets us reproduce the problem you have.

All I took out was:

; return the file name for a given shape
Func PieceName($piece)
Switch $piece
  Case 0
   $FileName = ".ImagesSquare.bmp"
  Case 1
   $FileName = ".ImagesRed Tribe.bmp"
  Case 101
   $FileName = ".ImagesGreen Tribe.bmp"
  Case 2
   $FileName = ".ImagesRed Clan.bmp"
  Case 102
   $FileName = ".ImagesGreen Clan.bmp"
  Case 3
   $FileName = ".ImagesRed HunterN.bmp"
  Case 103
   $FileName = ".ImagesGreen HunterN.bmp"
  Case 4
   $FileName = ".ImagesRed HunterS.bmp"
  Case 104
   $FileName = ".ImagesGreen HunterS.bmp"
  Case 5
   $FileName = ".ImagesRed HunterE.bmp"
  Case 105
   $FileName = ".ImagesGreen HunterE.bmp"
  Case 6
   $FileName = ".ImagesRed HunterW.bmp"
  Case 106
   $FileName = ".ImagesGreen HunterW.bmp"
  Case 7
   $FileName = ".ImagesRed HunterNE.bmp"
  Case 107
   $FileName = ".ImagesGreen HunterNE.bmp"
  Case 8
   $FileName = ".ImagesRed HunterNW.bmp"
  Case 108
   $FileName = ".ImagesGreen HunterNW.bmp"
  Case 9
   $FileName = ".ImagesRed HunterSE.bmp"
  Case 109
   $FileName = ".ImagesGreen HunterSE.bmp"
  Case 10
   $FileName = ".ImagesRed HunterSW.bmp"
  Case 110
   $FileName = ".ImagesGreen HunterSW.bmp"
  Case Else
   MsgBox(0, "Fatal Error","Invalid Piece Number: " & $piece)
   Exit
EndSwitch
Return $FileName
EndFunc
; return the name of the square color for a given color
Func ColorName($setting)
Switch $setting
  Case $Black
   $FileName =".ImagesBlack.bmp"
  Case $Yellow
   $FileName = ".IamgesYellow.bmp"
  Case $Cyan
   $FileName = ".IamgesCyan.bmp"
  Case Else
   MsgBox(0, "Fatal Error", "Invalid Square Color:" & $setting)
   Exit
EndSwitch
Return $FileName
EndFunc

which relied so much on a bunch of bmps that I figured it to be unusable.

Edited by Topher

[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

Share this post


Link to post
Share on other sites

Insert

MsgBox(0,"", "Side: " & $side & @CRLF & "Fore: " & $fore)
after
If $msg = $hBoard[$fore][$side] Then
and you will see that your script handles clicks on a button.

But the pictures don't get changed.

Can't test because I don't have the images. But you could try to specify absolute path names.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I had put a beep in there as a test; it never rang.

images are loading fine (it initializes just fine) - if I understand what you said.

I can zip up the images if that will help.

Edited by Topher

[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

Share this post


Link to post
Share on other sites

If you like you can post them. Unfortunately I won't be able to test your script today because it's time to go to bed now ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Image Zip

Sleep well, thank you for the fast attention.


[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

Share this post


Link to post
Share on other sites

".ImagesBlack.bmp"

I don't think that forces the root of your script either. The working dir or whatever can change.

If you want to force the root script folder, I had to use @scriptdir&"ImagesBlack.bmp".

Please provide the pictures.


I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

".ImagesBlack.bmp"

I don't think that forces the root of your script either. The working dir or whatever can change.

If you want to force the root script folder, I had to use @scriptdir&"ImagesBlack.bmp".

Please provide the pictures.

I've had success (at least no one complained) with "." but I'll make the more professional change.

image zip provided one post back.


[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

solution!

add $gui_disable to the creation of $hColor

$hColor[$x][$y] = GUICtrlCreatePic(@scriptdir&"ImagesBlack.bmp", $x * 68 + 150, $y * 68, 68, 68, $gui_disable)

It was stealing the focus clicks from the buttons. $gui_disable will stop that from happening and now it beeps.

Edited by Xandy

I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

That did it.

Thank you.


[left][hr]
$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]

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
Sign in to follow this  
Followers 0