# Simple grid of input controls in au3

## Recommended Posts

Just a quick sample. I know there are other similar solutions out there in the forum.

EDIT: Update later in th thread

Edited by Uten

##### Share on other sites

Placeholder for first version.

CODE
#include-once
#include <GuiConstants.au3>
; NOTE: I have forgotten how cells are addressed in excel.
; It would probably be easier to use the functions if ther match Excel addressing.
Func au3GridCreate($cols,$rows, $left=10,$top=10)
; Using 1 dimentional array to hold the grid
; NOTE: The first two array positons are used to hold the size of the grid in $cols and$rows
; NOTE: We should probably keep track of positions and sizes to.
Local $grid[$cols*$rows + 2] Local$i
Local  $cLeft =$left, $cTop =$top, $cWidth = 50,$cHeight = 18
Local $hspace = 5,$vspace = 5
Local $offset = 2 ;Using offset to be able to use part of the array for maintenance data.$grid[0] = $cols$grid[1] = $rows Local$col, $row For$i = $offset to$cols*$rows - 1 + 2 ;NOTE how to calculate where you are in the twodimentional grid$col = 1 + ($i -$offset+0 - mod($i -$offset + 0,$rows))/$rows
$row = mod($i - $offset + 1,$rows)
If $row = 0 Then$row = $rows$grid[$i] = GUICtrlCreateInput("C" &$col & "R" & $row ,$cLeft, $cTop,$cWidth, $cHeight) ; Move cels to the right until we reach mod(...) = 0 Then move 1 down and to the Left If mod($i - $offset + 1,$rows) = 0 then
$cLeft =$left
$cTop +=$cHeight + $vspace Else$cLeft += $cWidth +$hspace
EndIf
Next
Return $grid EndFunc Func au3GridCreate2d($cols, $rows,$left, $top) ;NOTE: This is just a provided as a sample, No other support functions for 2d ;Using 2 dimentional array to hold the grid ;NOTE: When we use a 2d array we can get the grid size back with Ubound($grid, 1) and UBound($grid, 2) Local$grid[$rows][$cols]
Local $i,$j
Local  $cLeft =$left, $cTop =$top, $cWidth = 50,$cHeight = 18
Local $hspace = 5,$vspace = 5
For $i = 0 to$cols - 1
For $j = 0 to$rows - 1
$grid[$i][$j] = GUICtrlCreateInput("C" &$i + 1 & "R" & $j,$cLeft + ( $cWidth +$hspace)*$j,$cTop + ($cHeight +$vspace)*$i ,$cWidth, $cHeight) ; Move cels to the right until we reach mod(...) = 0 Then move 1 down and to the Left Next Next Return$grid
EndFunc

Func au3GridToIni( ByRef $grid,$pathname, $page="Page1") Local$i
IniWrite($pathname,$page, "Cols", $grid[0]) IniWrite($pathname, $page, "Rows",$grid[1])
For $i = 0 to UBound($grid) - 1
IniWrite($pathname,$page, "C" & $i & "R" &$i, GUICtrlRead($grid[$i]))
Next
EndFunc
Func au3GridFromIni($pathname,$page="Page1", $left = 10,$top = 10)
Local $cols = IniRead($pathname, $page, "Cols",5) Local$rows = IniRead($pathname,$page, "Rows",5)
;TODO: Handel errors if there is no gui
Local $grid = au3GridCreate($cols, $rows,$left, $top) Local$i, $data,$default
Local $offset = 2 ;TODO: This variable has to change in several functions Local$col, $row For$i = 2 to UBound($grid) -1 ;NOTE how to calculate where you are in the twodimentional grid$col = 1 + ($i -$offset+0 - mod($i -$offset + 0,$rows))/$rows
$row = mod($i - $offset + 1,$rows)
If $row = 0 Then$row = $rows$default = "~C" & $col & "R" &$row
$data = IniRead($pathname, $page, "C" &$i & "R" & $i,$default)
GUICtrlSetData($grid[$i], $data) Next Return$grid
EndFunc

Func Main()
Local $inifile = "au3grid.ini" ;If FileExists($inifile) Then FileDelete($inifile) Local$gui = GUICreate("My grid")
Local $grid = au3GridFromIni($inifile)
;Local $grid = au3GridCreate(5,5) GUISetState(@SW_SHOW) While GuiGetMsg() <> -3 Sleep(100) WEnd au3GridToIni($grid, $inifile) EndFunc Main() Exit EDIT: Moved code from first post to here. Edited by Uten #### Share this post ##### Link to post ##### Share on other sites Second version. Added tab control and data reflection to show how to reuse the grid. Creating all those controls are expencive. So rather than creating controls we maintain data in arrays and updates the gui part as needed. This is the simplest possible approach. A later version should change data in the data in the assoisated array as it is changed in the gui. CODE #include-once #include <GuiConstants.au3> ; NOTE: I have forgotten how cells are addressed in excel. ; It would probably be easier to use the functions if ther match Excel addressing. Func au3GridCreate($cols, $rows,$left=10, $top=10) ; Using 1 dimentional array to hold the grid ; NOTE: The first two array positons are used to hold the size of the grid in$cols and $rows ; NOTE: We should probably keep track of positions and sizes to. Local$grid[$cols*$rows + 2]
Local $i Local$cLeft = $left,$cTop = $top,$cWidth = 50, $cHeight = 18 Local$hspace = 1, $vspace = 1 Local$offset = 2 ;Using offset to be able to use part of the array for maintenance data.
$grid[0] =$cols
$grid[1] =$rows
Local $col,$row
For $i =$offset to $cols*$rows - 1 + 2
;NOTE how to calculate where you are in the twodimentional grid
$col = 1 + ($i - $offset+0 - mod($i - $offset + 0,$rows))/$rows$row = mod($i -$offset + 1,$rows) If$row = 0 Then $row =$rows
$grid[$i] = GUICtrlCreateInput("C" & $col & "R" &$row ,$cLeft,$cTop,$cWidth,$cHeight)
; Move cels to the right until we reach mod(...) = 0 Then move 1 down and to the Left
If mod($i -$offset + 1,$rows) = 0 then$cLeft = $left$cTop += $cHeight +$vspace
Else
$cLeft +=$cWidth + $hspace EndIf Next Return$grid
EndFunc

Func au3GridCreate2d($cols,$rows, $left,$top)
;NOTE: This is just a provided as a sample, No other support functions for 2d
;Using 2 dimentional array to hold the grid
;NOTE: When we use a 2d array we can get the grid size back with Ubound($grid, 1) and UBound($grid, 2)
Local $grid[$rows][$cols] Local$i, $j Local$cLeft = $left,$cTop = $top,$cWidth = 50, $cHeight = 18 Local$hspace = 5, $vspace = 5 For$i = 0 to $cols - 1 For$j = 0 to $rows - 1$grid[$i][$j] = GUICtrlCreateInput("C" & $i + 1 & "R" &$j,$cLeft + ($cWidth + $hspace)*$j, $cTop + ($cHeight + $vspace)*$i ,$cWidth,$cHeight)
; Move cels to the right until we reach mod(...) = 0 Then move 1 down and to the Left
Next
Next
Return $grid EndFunc Func au3GridToIni( ByRef$grid,$pathname,$page="Page1")
Local $i IniWrite($pathname, $page, "Cols",$grid[0])
IniWrite($pathname,$page, "Rows", $grid[1]) For$i = 0 to UBound($grid) - 1 IniWrite($pathname, $page, "C" &$i & "R" & $i, GUICtrlRead($grid[$i])) Next EndFunc Func au3GridFromIni($pathname, $page="Page1",$left = 10, $top = 10) Local$cols = IniRead($pathname,$page, "Cols",5)
Local $rows = IniRead($pathname, $page, "Rows",5) ;TODO: Handel errors if there is no gui Local$grid = au3GridCreate($cols,$rows, $left,$top)
Local $i,$data, $default Local$offset = 2 ;TODO: This variable has to change in several functions
Local $col,$row
For $i = 2 to UBound($grid) -1
;NOTE how to calculate where you are in the twodimentional grid
$col = 1 + ($i - $offset+0 - mod($i - $offset + 0,$rows))/$rows$row = mod($i -$offset + 1,$rows) If$row = 0 Then $row =$rows
$default = "~C" &$col & "R" & $row$data = IniRead($pathname,$page, "C" & $i & "R" &$i, $default) GUICtrlSetData($grid[$i],$data)
Next
Return $grid EndFunc Func au3GridArrayToIni( ByRef$arr, $pathname,$page="Page1")
;TODO:
EndFunc
Func au3GridArrayFromIni($pathname,$page="Page1")
;TODO:
EndFunc

Func au3GridArrayToGrid(ByRef $grid, ByRef$data)

Local $i,$gridMax = UBound($grid) - 1 For$i = 2 to UBound($data) -1 + 2 If$i > $gridMax Then ConsoleWrite("(" & @ScriptLineNumber & ") := !ERROR: data collection biger than grid size.$i:=" & $i & " Ubound($data):=" & UBound($data) & @CRLF ) SetError(1, 1, 0) ExitLoop EndIf GUICtrlSetData($grid[$i],$data[$i - 2]) Next EndFunc Func au3GridGridToArray(ByRef$grid, ByRef $data) Local$i, $dataMax = Ubound($data) - 1
For $i = 2 to UBound($grid) -1
If $i >$dataMax Then ReDim $data[UBound($grid) - 2] ;TODO: Save space if not all grid elements are used
$data[$i - 2] = GuiCtrlRead($grid[$i])
Next
EndFunc
Func Main()
Local $data0[6] = [1, 2, 3, 4, 5, 6] Local$data1[6] = [10, 20, 30, 40, 50, 60]
Local $inifile = "au3grid.ini" If FileExists($inifile) Then FileDelete($inifile) Local$gui = GUICreate("My grid")

$Tab1 = GUICtrlCreateTab(32, 40, 313, 273)$TabSheet1 = GUICtrlCreateTabItem("TabSheet1")
$TabSheet2 = GUICtrlCreateTabItem("TabSheet2") GUICtrlCreateTabItem("") ;Local$grid = au3GridFromIni($inifile) Local$grid = au3GridCreate(5,5,33,65)
GUISetState(@SW_SHOW)
Local $currentTab = 0 au3GridArrayToGrid($grid, $data0) While 1$nMsg = GUIGetMsg()
Switch $nMsg Case$GUI_EVENT_CLOSE
ExitLoop

Case $Tab1 Switch GUICtrlread ($Tab1)
Case 0 ;$TabSheet1 ;TODO: Easier reflection handeling If$currentTab <> 0 then
au3GridGridToArray($grid,$data1)
au3GridArrayToGrid($grid,$data0)
ConsoleWrite("TabSheet1" & @CRLF )
$currentTab = 0 EndIf Case 1;$TabSheet2
If $currentTab <> 1 then au3GridGridToArray($grid, $data0) au3GridArrayToGrid($grid, $data1) ConsoleWrite("TabSheet2" & @CRLF )$currentTab = 1
EndIf
EndSwitch
EndSwitch
WEnd
GUISetState(@SW_HIDE)
;TODO: Manage data for all tabs: au3GridToIni($grid,$inifile)
GUIDelete(\$gui)
EndFunc
Main()
Exit

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...