Jump to content

Grid UDF


Kohr
 Share

Recommended Posts

I seam to find myself lately trying to use a listview as a grid so I just made this function to simulate one. Since I am using inputs as the grid it will not sort or resize yet.

I thought others might find it useful. Hopefully this hasn't been done before because I can never find an easy one using search.

Kohr

EDIT::

Parts of this coded by Valuater.

This will now sort on columns. I removed the need for GUIButton.au3 and just made them normal buttons.

#include <GUIConstants.au3>
#include <array.au3>

Opt("GUIOnEventMode", 1)

Global $File = @ScriptDir & "\Grid.txt"
Global $Cell[1][1]
Global $rows = 19
Global $columns = 12
Global $color = 0x00ff00
$width = 40
$height = 20
Dim $aSortButton[$columns + 1]

$GUImain = GUICreate("Grid Example", 500, 500, -1, -1, $WS_SIZEBOX)

$ret = 10
For $i = 1 To $columns
    $aSortButton[$i] = GUICtrlCreateButton("Sort", $ret, 10, $width, $height)
    GUICtrlSetResizing($aSortButton[$i], 1)
    GUICtrlSetOnEvent($aSortButton[$i], "buttonPressed")
    $ret += $width
Next

$aGrid = _Grid(10, 30, $width, $height, $rows, $columns, $color)

For $i = 1 To $rows
    For $j = 1 To $columns
        GUICtrlSetResizing($aGrid[$i][$j], 1)
    Next
Next

_ColumnHeader("#")
_RowHeader("#")

GUICtrlSetBkColor($aGrid[1][1], 0xffffff)
GUICtrlSetBkColor($aGrid[2][2], 0xffff00)
GUICtrlSetBkColor($aGrid[2][3], 0xff00ff)
GUICtrlSetBkColor($aGrid[3][4], 0xff0000)
GUICtrlSetBkColor($aGrid[3][5], 0x00ffff)
GUICtrlSetBkColor($aGrid[4][6], 0x111111)
GUICtrlSetBkColor($aGrid[4][7], 0x12ffe0)
GUICtrlSetBkColor($aGrid[5][8], 0x6eff0e)

GUICtrlSetData($aGrid[7][4], "text")

$Save_btn = GUICtrlCreateButton("&Save Grid", 20, 420, 150, 30)
GUICtrlSetOnEvent($Save_btn, "_SaveGrid")
$Load_btn = GUICtrlCreateButton("&Load Grid", 175, 420, 150, 30)
GUICtrlSetOnEvent($Load_btn, "_LoadGrid")
$Exit_btn = GUICtrlCreateButton("&Exit Grid", 330, 420, 150, 30)
GUICtrlSetOnEvent($Exit_btn, "CloseClicked")

$k = 0
For $i = 2 To $rows
    For $j = 2 To $columns
        $k += 1
        GUICtrlSetData($aGrid[$i][$j], Chr(63 + $i) & $k)
    Next
Next

GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseClicked")

While 1
    Sleep(200)
WEnd

#cs
    _Grid ( $StartX, $StartY,  $cellWidth, $cellHeight, $rows, $columns )
    
    Parameters:
    $StartX - left coordinate to start grid
    $StartY - top coordinate to start grid.
    $cellWidth - Width of each cell.
    $cellHeight - Height of each cell.
    $rows - Total rows to create.
    $columns - Total columns to create.
    
    Return Value:
    Returns the identifier (controlID) of each new control in the grid.
    Array[0][0] = total controls
    Array[1][1] = 1st control (count going from left to right, by row)
#ce

Func _Grid($StartX, $StartY, $cellWidth, $cellHeight, $rows, $columns, $color = "")
    Local $tempX, $tempY
    ReDim $Cell[$rows + 1][$columns + 1]
    $tempX += $StartX
    $tempY += $StartY
    For $i = 1 To $rows
        For $j = 1 To $columns
            $Cell[$i][$j] = GUICtrlCreateInput("", $tempX, $tempY, $cellWidth, $cellHeight)
            If $color > "" And $i = 1 Or $j = 1 Then
                GUICtrlSetBkColor($Cell[$i][$j], $color)
            Else
                GUICtrlSetBkColor($Cell[$i][$j], 0xffffff)
            EndIf
            $tempX += $cellWidth
        Next
        $tempX = $StartX
        $tempY += $cellHeight
    Next
    $Cell[0][0] = UBound($Cell) - 1
    Return $Cell
EndFunc   ;==>_Grid

Func _ColumnHeader($GHeader, $GLock = 0)
    If $GHeader = "#" Then
        For $i = 2 To $columns
            GUICtrlSetData($aGrid[1][$i], "Col " & $i - 1)
            If $GLock Then GUICtrlSetStyle($Cell[1][$i], $WS_DISABLED)
        Next
    Else
        $GHeader = StringSplit($GHeader, ",")
        For $i = 1 To $GHeader[0]
            GUICtrlSetData($aGrid[1][$i], $GHeader[$i])
            If $GLock Then GUICtrlSetStyle($Cell[1][$i], $WS_DISABLED)
        Next
    EndIf
EndFunc   ;==>_ColumnHeader

Func _RowHeader($RHeader, $RLock = 0)
    If $RHeader = "#" Then
        For $i = 2 To $rows
            GUICtrlSetData($aGrid[$i][1], "R " & $i - 1)
            If $RLock Then GUICtrlSetStyle($Cell[$i][1], $WS_DISABLED)
        Next
    Else
        $RHeader = StringSplit($RHeader, ",")
        For $i = 1 To $RHeader[0]
            GUICtrlSetData($aGrid[$i][1], $RHeader[$i])
            If $RLock Then GUICtrlSetStyle($Cell[$i][1], $WS_DISABLED)
        Next
    EndIf
EndFunc   ;==>_RowHeader

Func _SaveGrid()
    FileDelete($File)
    For $i = 1 To $rows
        For $j = 1 To $columns
            FileWriteLine($File, GUICtrlRead($Cell[$i][$j]))
        Next
    Next
EndFunc   ;==>_SaveGrid

Func _LoadGrid()
    Local $x
    For $i = 1 To $rows
        For $j = 1 To $columns
            $x = $x + 1
            GUICtrlSetData($Cell[$i][$j], FileReadLine($File, $x))
        Next
    Next
EndFunc   ;==>_LoadGrid

Func _GridSort($SortColumn, $rows, $columns, $sortType)
    Dim $Data[$rows][$columns + 1]
    For $i = 2 To $rows
        For $j = 1 To $columns
            $Data[$i - 1][$j] = GUICtrlRead($aGrid[$i][$j])
        Next
    Next
;~      Console_Array2dDisplay($Data, "$output b4 sort")
    If $sortType = 1 Then
        _ArraySort($Data, 1, 1, -1, $columns + 1, $SortColumn)
    Else
        _ArraySort($Data, "", 1, -1, $columns + 1, $SortColumn)
    EndIf
;~      Console_Array2dDisplay($Data, "$output after sort")
    For $i = 2 To $rows
        For $j = 1 To $columns
            GUICtrlSetData($aGrid[$i][$j], $Data[$i - 1][$j])
        Next
    Next
    Dim $Data = 1
EndFunc   ;==>_GridSort

Func buttonPressed()
    Local $hwnd = ControlGetHandle($GUImain, "", ControlGetFocus($GUImain))
    Local $result = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hwnd)
    $SortColumn = $result[0] - $aSortButton[1] + 1
    $ret = GUICtrlRead($result[0])
    If $ret = "Sort" Then
        GUICtrlSetData($result[0], "\/")
        _GridSort($SortColumn, $rows, $columns, 1)
    EndIf
    ;descend
    If $ret = "/\" Then
        GUICtrlSetData($result[0], "\/")
        _GridSort($SortColumn, $rows, $columns, 1)
    EndIf
    If $ret = "\/" Then
        ;ascend
        GUICtrlSetData($result[0], "/\")
        _GridSort($SortColumn, $rows, $columns, 0)
    EndIf
    $ret = $result[0] - $aSortButton[1] + 1
    ConsoleWrite($ret)
    For $i = 1 To $columns
        If $i <> $ret Then
            GUICtrlSetData($aSortButton[$i], "Sort")
        EndIf
    Next
EndFunc   ;==>buttonPressed

Func CloseClicked()
    Exit
EndFunc   ;==>CloseClicked

Func Console_Array2dDisplay(ByRef $ar_Array, $s_Title = "Array contents", $n_Index = 1, $i_Message = 0)
    ; Display 2 dimensional array
    Local $output = ""
    Local $r, $c
    If Not IsArray($ar_Array) Then Return -1
    For $r = $n_Index To UBound($ar_Array, 1) - 1
        $output = $output & @LF
        For $c = 0 To UBound($ar_Array, 2) - 1
            $output = $output & $ar_Array[$r][$c] & " "
        Next
    Next
    ConsoleWrite($s_Title & "=" & @LF & $output & @LF)
    If $i_Message Then MsgBox(4096, $s_Title, $output)
    Return $output
EndFunc   ;==>Console_Array2dDisplay
Edited by Kohr
Link to comment
Share on other sites

Nice..!!!

an idea i would use is a two dimension array... where

Array[0][0] = total controls

Array[1][1] = 1st control (count going from left to right, by row)

Array [2][5] = row #2, column #5

...

Good Job

8)

Thanks. I will change it to that on the next version. I am attempting to make it where you can sort data in the columns right now.

Kohr

Link to comment
Share on other sites

I like your UDF i agree with Valuater though.

Checkout rakudave's UDF http://www.autoitscript.com/forum/index.php?showtopic=25374

Edited by RazerM
My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

maybe...

#include <GUIConstants.au3>
#include <array.au3>

$rows = 19
$columns = 12
$color = 0x00ff00


$GUImain = GUICreate("Grid Example", 500, 400)

$aGrid = _Grid(10, 10, 40, 20, $rows, $columns, $color)

For $i = 2 To $columns
    GUICtrlSetData($aGrid[1][$i], "C" & $i - 1)
Next

For $i = 2 To $rows
    GUICtrlSetData($aGrid[$i][1], "R" & $i - 1)
Next
GUICtrlSetBkColor($aGrid[10][6], 0xffffff)
GUICtrlSetBkColor($aGrid[12][5], 0xffff00)
GUICtrlSetBkColor($aGrid[2][7], 0xff00ff)
GUICtrlSetBkColor($aGrid[3][4], 0xff0000)
GUICtrlSetBkColor($aGrid[4][10], 0x00ffff)
GUICtrlSetBkColor($aGrid[5][6], 0x111111)
GUICtrlSetBkColor($aGrid[6][9], 0x12ffe0)
GUICtrlSetBkColor($aGrid[7][5], 0x6eff0e)

GUICtrlSetData($aGrid[7][4], "text")

GUISetState()

While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
    Sleep(20)
WEnd

#cs
    _Grid ( $StartX, $StartY,  $cellWidth, $cellHeight, $rows, $columns )
    
    Parameters:
    $StartX - left coordinate to start grid
    $StartY - top coordinate to start grid.
    $cellWidth - Width of each cell.
    $cellHeight - Height of each cell.
    $rows - Total rows to create.
    $columns - Total columns to create.
    
    Return Value:
    Returns the identifier (controlID) of each new control in the grid.
    Array[0][0] = total controls
    Array[1][1] = 1st control (count going from left to right, by row)
#ce

Func _Grid($StartX, $StartY, $cellWidth, $cellHeight, $rows, $columns, $color = "")
    Local $tempX, $tempY, $Cell[$rows + 1][$columns + 1]
    $tempX += $StartX
    $tempY += $StartY
    For $i = 1 To $rows
        For $j = 1 To $columns
            $Cell[$i][$j] = GUICtrlCreateInput("", $tempX, $tempY, $cellWidth, $cellHeight)
            If $color > "" And $i = 1 Or $j = 1 Then
                GUICtrlSetStyle($Cell[$i][$j], $WS_DISABLED)
                GUICtrlSetBkColor($Cell[$i][$j], $color)
            Else
                GUICtrlSetBkColor($Cell[$i][$j], 0xffffff)
            EndIf
            $tempX += $cellWidth
        Next
        $tempX = $StartX
        $tempY += $cellHeight
    Next
    $Cell[0][0] = UBound($Cell) - 1
    Return $Cell
EndFunc   ;==>_Grid

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

That is much better Valuater.

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

I have not tried your yet. Just thought I should give a few pointers based on experience.

  • A grid like this should be virtual (to speed things up). That is you should only load as many gui elements as necessary to fill the visible part of the screen. An array (or better linked list) should be hold the data to be reflected in the grid.
  • Anny grid should be scrollable. This is where you have to add logic to make your grid keep track of which element is visual and which is not. You will have to add a scrollbar at the bottom/top and one the left/right side.
  • You should think speed and screen updates all the time. Or you will have to do major rewrites now and then.
Link to comment
Share on other sites

I have not tried your yet. Just thought I should give a few pointers based on experience.

  • A grid like this should be virtual (to speed things up). That is you should only load as many gui elements as necessary to fill the visible part of the screen. An array (or better linked list) should be hold the data to be reflected in the grid.
  • Anny grid should be scrollable. This is where you have to add logic to make your grid keep track of which element is visual and which is not. You will have to add a scrollbar at the bottom/top and one the left/right side.
  • You should think speed and screen updates all the time. Or you will have to do major rewrites now and then.
I was not trying to achieve an excel type grid. I usually just wanted a grid on screen so I could enter data or use as option settings. Nearly every time I wanted this I would try to use a listview(obvious choice to me) but it was never simple.

Kohr

Link to comment
Share on other sites

Thanks Valuater. I started writing a sorting type code but realized I don't really need that functionality for the game I am building so that part is on hold until I need it.

The only other thing I added that you didn't is the following code to allow resizing.

Anyway this should be a good example for others to use ;).

$GUImain = GUICreate("Grid Example", 500, 450, -1, -1,$WS_SIZEBOX)

For $i = 1 To $rows
    For $j = 1 To $columns
        GUICtrlSetResizing ($aGrid[$i][$j], 1)
    Next
Next
Link to comment
Share on other sites

just playing...

_ColumnHeader(",Sales,Date,Office,,City,File#")

;_ColumnHeader("#")

_RowHeader("#")

#include <GUIConstants.au3>
#include <array.au3>

Global $Cell[1][1]
Global $rows = 19
Global $columns = 12
Global $color = 0x00ff00



$GUImain = GUICreate("Grid Example", 500, 450)

$aGrid = _Grid(10, 10, 40, 20, $rows, $columns, $color)

_ColumnHeader(",Sales,Date,Office,,City,File#")

;_ColumnHeader("#")
_RowHeader("#")

GUICtrlSetBkColor($aGrid[10][6], 0xffffff)
GUICtrlSetBkColor($aGrid[12][5], 0xffff00)
GUICtrlSetBkColor($aGrid[2][7], 0xff00ff)
GUICtrlSetBkColor($aGrid[3][4], 0xff0000)
GUICtrlSetBkColor($aGrid[4][10], 0x00ffff)
GUICtrlSetBkColor($aGrid[5][6], 0x111111)
GUICtrlSetBkColor($aGrid[6][9], 0x12ffe0)
GUICtrlSetBkColor($aGrid[7][5], 0x6eff0e)

GUICtrlSetData($aGrid[7][4], "text")

GUISetState()

While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
    Sleep(20)
WEnd

#cs
    _Grid ( $StartX, $StartY,  $cellWidth, $cellHeight, $rows, $columns )
    
    Parameters:
    $StartX - left coordinate to start grid
    $StartY - top coordinate to start grid.
    $cellWidth - Width of each cell.
    $cellHeight - Height of each cell.
    $rows - Total rows to create.
    $columns - Total columns to create.
    
    Return Value:
    Returns the identifier (controlID) of each new control in the grid.
    Array[0][0] = total controls
    Array[1][1] = 1st control (count going from left to right, by row)
#ce

Func _Grid($StartX, $StartY, $cellWidth, $cellHeight, $rows, $columns, $color = "")
    Local $tempX, $tempY
    ReDim $Cell[$rows + 1][$columns + 1]
    $tempX += $StartX
    $tempY += $StartY
    For $i = 1 To $rows
        For $j = 1 To $columns
            $Cell[$i][$j] = GUICtrlCreateInput("", $tempX, $tempY, $cellWidth, $cellHeight)
            If $color > "" And $i = 1 Or $j = 1 Then
                GUICtrlSetStyle($Cell[$i][$j], $WS_DISABLED)
                GUICtrlSetBkColor($Cell[$i][$j], $color)
            Else
                GUICtrlSetBkColor($Cell[$i][$j], 0xffffff)
            EndIf
            $tempX += $cellWidth
        Next
        $tempX = $StartX
        $tempY += $cellHeight
    Next
    $Cell[0][0] = UBound($Cell) - 1
    Return $Cell
EndFunc   ;==>_Grid

Func _ColumnHeader($GHeader)
    If $GHeader = "#" Then
        For $i = 2 to $columns
            GUICtrlSetData($aGrid[1][$i], "Col " & $i - 1)
        Next
    Else
        $GHeader = StringSplit($GHeader, ",")
        For $i = 1 To $GHeader[0]
            GUICtrlSetData($aGrid[1][$i], $GHeader[$i])
        Next
    EndIf
EndFunc

Func _RowHeader($RHeader)
    If $RHeader = "#" Then
        For $i = 2 to $rows
            GUICtrlSetData($aGrid[$i][1], "R " & $i - 1)
        Next
    Else
        $RHeader = StringSplit($RHeader, ",")
        For $i = 1 To $RHeader[0]
            GUICtrlSetData($aGrid[$i][1], $RHeader[$i])
        Next
    EndIf
EndFunc

i couldn't get resizing to work, especially with $WS_DISABLE

8)

NEWHeader1.png

Link to comment
Share on other sites

i couldn't get resizing to work, especially with $WS_DISABLE

This is what I had done that did resizing (resizing is mean by the whole gui, not columns) and the start of sorting abilities. This needs the GuiButton.au3 from gafrost Colored Buttons to work. More ideas if you want to improve it.

CODE

;GUIButton written by gafrost
#region --- include, Opt, Globals, HotKeySet ---
#include <GUIConstants.au3>
#include <array.au3>
#include "GuiButton.au3"

Opt("GUIOnEventMode", 1)

HotKeySet("{END}", "CloseClicked")
#endregion --- include, Opt, Globals, HotKeySet ---

#region --- GUI ---
$GUImain = GUICreate("Grid Example", 500, 400, -1, -1, $WS_SIZEBOX)


$rows = 12
$columns = 12
$width = 30
$height = 20
Dim $aSortButton[$columns+1]

$aGrid = _Grid(10, 30, $width, $height, $rows, $columns)
For $i = 1 To $rows
    For $j = 1 To $columns
        GUICtrlSetResizing ($aGrid[$i][$j], 1)
    Next
Next

$ret = $width+10
For $i = 2 To $columns
    $aSortButton[$i] = _GuiCtrlCreateButton("Sort", $ret, $height-10, $width, $height, -1, 0x0ffff0)
    GUICtrlSetResizing ($aSortButton[$i], 1)
    $ret += $width
Next


For $i = 1 To $columns
    GUICtrlSetBkColor($aGrid[1][$i], 0x00ff00)
    GUICtrlSetStyle($aGrid[1][$i], $WS_DISABLED)
Next
$j = 0
For $i = 2 To $columns
    $j += 1
    GUICtrlSetData($aGrid[1][$i], "C" & $j)
Next
$j = 0
For $i = 2 To $rows
    GUICtrlSetBkColor($aGrid[$i][1], 0x00ff00)
    GUICtrlSetStyle($aGrid[$i][1], $WS_DISABLED)
    GUICtrlSetData($aGrid[$i][1], "R" & $i)
Next
For $i = 2 To $rows
    $j += 1
    GUICtrlSetData($aGrid[$i][1], "R" & $j)
Next
GUICtrlSetBkColor($aGrid[1][1], 0xffffff)
GUICtrlSetBkColor($aGrid[2][2], 0xffff00)
GUICtrlSetBkColor($aGrid[2][3], 0xff00ff)
GUICtrlSetBkColor($aGrid[3][4], 0xff0000)
GUICtrlSetBkColor($aGrid[3][5], 0x00ffff)
GUICtrlSetBkColor($aGrid[4][6], 0x111111)
GUICtrlSetBkColor($aGrid[4][7], 0x12ffe0)
GUICtrlSetBkColor($aGrid[5][8], 0x6eff0e)

GUICtrlSetData($aGrid[4][4], "text")

;needed for color buttons
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
; WM_DRAWITEM has to registered before showing GUI otherwise the initial drawing isn't done
GUIRegisterMsg($WM_DRAWITEM, "MY_WM_DRAWITEM")

GUISetState(@SW_SHOW, $GUImain)
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseClicked")

#endregion --- GUI ---
While 1
    Sleep(200)
WEnd

#cs
    _Grid ( $StartX, $StartY,  $cellWidth, $cellHeight, $rows, $columns )
    
    Parameters:
    $StartX - left coordinate to start grid
    $StartY - top coordinate to start grid.
    $cellWidth - Width of each cell.
    $cellHeight - Height of each cell.
    $rows - Total rows to create.
    $columns - Total columns to create.
    
    Return Value:
    Returns the identifier (controlID) of each new control in the grid.
    Array[0] = total controls
    Array[1] = 1st control (count going from left to right, top to bottom)
#ce

Func _Grid($StartX, $StartY, $cellWidth, $cellHeight, $rows, $columns)
    Dim $tempX, $tempY, $index
    $ret = $rows * $columns + 1
    Dim $Cell[$rows+1][$columns+1]
    $tempX += $StartX
    $tempY += $StartY
    For $i = 1 To $rows
        For $j = 1 To $columns
            ;$index += 1
            $Cell[$i][$j] = GUICtrlCreateInput("", $tempX, $tempY, $cellWidth, $cellHeight)
            $tempX += $cellWidth
        Next
        $tempX = $StartX
        $tempY += $cellHeight
    Next
    $Cell[0][0] = UBound($Cell) - 1
    Return $Cell
EndFunc   ;==>_Grid

Func _GridSort()
    ConsoleWrite("a")
EndFunc


#region --- Functions for closing ---
Func CloseClicked()
    Exit
EndFunc   ;==>CloseClicked
#endregion --- Functions for closing ---

Func Button_Click(ByRef $ctrl_id)
    Switch $ctrl_id
        Case $aSortButton
;~          _DebugPrint(_GuiCtrlButtonGetText($nButton1))
    EndSwitch
EndFunc
Kohr Edited by Kohr
Link to comment
Share on other sites

I was busy playing some more

Added....

_SaveGrid($SFile)

_LoadGrid($LFile)

#include <GUIConstants.au3>
#include <array.au3>

$File = @ScriptDir & "Grid.txt"

Global $Cell[1][1]
Global $rows = 19
Global $columns = 12
Global $color = 0x00ff00


$GUImain = GUICreate("Grid Example", 500, 450)

$aGrid = _Grid(10, 10, 40, 20, $rows, $columns, $color)

_ColumnHeader(",Sales,Date,Office,,City,File#")

;_ColumnHeader("#")
_RowHeader("#")

GUICtrlSetBkColor($aGrid[1][1], 0xffffff)
GUICtrlSetBkColor($aGrid[2][2], 0xffff00)
GUICtrlSetBkColor($aGrid[2][3], 0xff00ff)
GUICtrlSetBkColor($aGrid[3][4], 0xff0000)
GUICtrlSetBkColor($aGrid[3][5], 0x00ffff)
GUICtrlSetBkColor($aGrid[4][6], 0x111111)
GUICtrlSetBkColor($aGrid[4][7], 0x12ffe0)
GUICtrlSetBkColor($aGrid[5][8], 0x6eff0e)

GUICtrlSetData($aGrid[7][4], "text")

$Save_btn = GUICtrlCreateButton("&Save Grid", 20, 410, 150, 30)
$Load_btn = GUICtrlCreateButton("&Load Grid", 175, 410, 150, 30)
$Exit_btn = GUICtrlCreateButton("&Exit Grid", 330, 410, 150, 30)

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Or $msg = $Exit_btn Then Exit
    
    If $msg = $Save_btn Then _SaveGrid($File)
    
    If $msg = $Load_btn Then _LoadGrid($File)
    
WEnd

#cs
    _Grid ( $StartX, $StartY,  $cellWidth, $cellHeight, $rows, $columns )
    
    Parameters:
    $StartX - left coordinate to start grid
    $StartY - top coordinate to start grid.
    $cellWidth - Width of each cell.
    $cellHeight - Height of each cell.
    $rows - Total rows to create.
    $columns - Total columns to create.
    
    Return Value:
    Returns the identifier (controlID) of each new control in the grid.
    Array[0][0] = total controls
    Array[1][1] = 1st control (count going from left to right, by row)
#ce

Func _Grid($StartX, $StartY, $cellWidth, $cellHeight, $rows, $columns, $color = "")
    Local $tempX, $tempY
    ReDim $Cell[$rows + 1][$columns + 1]
    $tempX += $StartX
    $tempY += $StartY
    For $i = 1 To $rows
        For $j = 1 To $columns
            $Cell[$i][$j] = GUICtrlCreateInput("", $tempX, $tempY, $cellWidth, $cellHeight)
            If $color > "" And $i = 1 Or $j = 1 Then
                GUICtrlSetStyle($Cell[$i][$j], $WS_DISABLED)
                GUICtrlSetBkColor($Cell[$i][$j], $color)
            Else
                GUICtrlSetBkColor($Cell[$i][$j], 0xffffff)
            EndIf
            $tempX += $cellWidth
        Next
        $tempX = $StartX
        $tempY += $cellHeight
    Next
    $Cell[0][0] = UBound($Cell) - 1
    Return $Cell
EndFunc   ;==>_Grid

Func _ColumnHeader($GHeader)
    If $GHeader = "#" Then
        For $i = 2 To $columns
            GUICtrlSetData($aGrid[1][$i], "Col " & $i - 1)
        Next
    Else
        $GHeader = StringSplit($GHeader, ",")
        For $i = 1 To $GHeader[0]
            GUICtrlSetData($aGrid[1][$i], $GHeader[$i])
        Next
    EndIf
EndFunc   ;==>_ColumnHeader

Func _RowHeader($RHeader)
    If $RHeader = "#" Then
        For $i = 2 To $rows
            GUICtrlSetData($aGrid[$i][1], "R " & $i - 1)
        Next
    Else
        $RHeader = StringSplit($RHeader, ",")
        For $i = 1 To $RHeader[0]
            GUICtrlSetData($aGrid[$i][1], $RHeader[$i])
        Next
    EndIf
EndFunc   ;==>_RowHeader

Func _SaveGrid($SFile)
    FileDelete($SFile)
    For $i = 1 To $rows
        For $j = 1 To $columns
            FileWriteLine($SFile, GUICtrlRead($Cell[$i][$j]))
        Next
    Next
EndFunc   ;==>_SaveGrid

Func _LoadGrid($LFile)
    Local $x
    For $i = 1 To $rows
        For $j = 1 To $columns
            $x = $x + 1
            GUICtrlSetData($Cell[$i][$j], FileReadLine($LFile, $x))
        Next
    Next
EndFunc   ;==>_LoadGrid

I will check out your last post now too!!

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Added...

Header lock ( edit or non-edit field )

_ColumnHeader($GHeader, $GLock = 0)

_RowHeader($RHeader, $RLock = 0)

#include <GUIConstants.au3>
#include <array.au3>

$File = @ScriptDir & "\Grid.txt"

Global $Cell[1][1]
Global $rows = 19
Global $columns = 12
Global $color = 0x00ff00


$GUImain = GUICreate("Grid Example", 500, 450)

$aGrid = _Grid(10, 10, 40, 20, $rows, $columns, $color)

_ColumnHeader(",Sales,Date,Office,,City,File#", 1)

;_ColumnHeader("#")
_RowHeader("#")

GUICtrlSetBkColor($aGrid[1][1], 0xffffff)
GUICtrlSetBkColor($aGrid[2][2], 0xffff00)
GUICtrlSetBkColor($aGrid[2][3], 0xff00ff)
GUICtrlSetBkColor($aGrid[3][4], 0xff0000)
GUICtrlSetBkColor($aGrid[3][5], 0x00ffff)
GUICtrlSetBkColor($aGrid[4][6], 0x111111)
GUICtrlSetBkColor($aGrid[4][7], 0x12ffe0)
GUICtrlSetBkColor($aGrid[5][8], 0x6eff0e)

GUICtrlSetData($aGrid[7][4], "text")

$Save_btn = GUICtrlCreateButton("&Save Grid", 20, 410, 150, 30)
$Load_btn = GUICtrlCreateButton("&Load Grid", 175, 410, 150, 30)
$Exit_btn = GUICtrlCreateButton("&Exit Grid", 330, 410, 150, 30)

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Or $msg = $Exit_btn Then Exit
    
    If $msg = $Save_btn Then _SaveGrid($File)
    
    If $msg = $Load_btn Then _LoadGrid($File)
    
WEnd

#cs
    _Grid ( $StartX, $StartY,  $cellWidth, $cellHeight, $rows, $columns )
    
    Parameters:
    $StartX - left coordinate to start grid
    $StartY - top coordinate to start grid.
    $cellWidth - Width of each cell.
    $cellHeight - Height of each cell.
    $rows - Total rows to create.
    $columns - Total columns to create.
    
    Return Value:
    Returns the identifier (controlID) of each new control in the grid.
    Array[0][0] = total controls
    Array[1][1] = 1st control (count going from left to right, by row)
#ce

Func _Grid($StartX, $StartY, $cellWidth, $cellHeight, $rows, $columns, $color = "")
    Local $tempX, $tempY
    ReDim $Cell[$rows + 1][$columns + 1]
    $tempX += $StartX
    $tempY += $StartY
    For $i = 1 To $rows
        For $j = 1 To $columns
            $Cell[$i][$j] = GUICtrlCreateInput("", $tempX, $tempY, $cellWidth, $cellHeight)
            If $color > "" And $i = 1 Or $j = 1 Then
                GUICtrlSetBkColor($Cell[$i][$j], $color)
            Else
                GUICtrlSetBkColor($Cell[$i][$j], 0xffffff)
            EndIf
            $tempX += $cellWidth
        Next
        $tempX = $StartX
        $tempY += $cellHeight
    Next
    $Cell[0][0] = UBound($Cell) - 1
    Return $Cell
EndFunc   ;==>_Grid

Func _ColumnHeader($GHeader, $GLock = 0)
    If $GHeader = "#" Then
        For $i = 2 To $columns
            GUICtrlSetData($aGrid[1][$i], "Col " & $i - 1)
            If $GLock Then GUICtrlSetStyle($Cell[1][$i], $WS_DISABLED)
        Next
    Else
        $GHeader = StringSplit($GHeader, ",")
        For $i = 1 To $GHeader[0]
            GUICtrlSetData($aGrid[1][$i], $GHeader[$i])
            If $GLock Then GUICtrlSetStyle($Cell[1][$i], $WS_DISABLED)
        Next
    EndIf
EndFunc   ;==>_ColumnHeader

Func _RowHeader($RHeader, $RLock = 0)
    If $RHeader = "#" Then
        For $i = 2 To $rows
            GUICtrlSetData($aGrid[$i][1], "R " & $i - 1)
            If $RLock Then GUICtrlSetStyle($Cell[$i][1], $WS_DISABLED)
        Next
    Else
        $RHeader = StringSplit($RHeader, ",")
        For $i = 1 To $RHeader[0]
            GUICtrlSetData($aGrid[$i][1], $RHeader[$i])
            If $RLock Then GUICtrlSetStyle($Cell[$i][1], $WS_DISABLED)
        Next
    EndIf
EndFunc   ;==>_RowHeader

Func _SaveGrid($SFile)
    FileDelete($SFile)
    For $i = 1 To $rows
        For $j = 1 To $columns
            FileWriteLine($SFile, GUICtrlRead($Cell[$i][$j]))
        Next
    Next
EndFunc   ;==>_SaveGrid

Func _LoadGrid($LFile)
    Local $x
    For $i = 1 To $rows
        For $j = 1 To $columns
            $x = $x + 1
            GUICtrlSetData($Cell[$i][$j], FileReadLine($LFile, $x))
        Next
    Next
EndFunc   ;==>_LoadGrid

that sort function is all yours.... lol

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

@khor, just remember my words when your statement comes back and bit you. ;) Been there done that. I did neither want to write a spreadsheet application.:lmao:

@Valuater, I might be wrong here but dont you have to get the brush information used by the system to draw the control to figure out it's current background color? Did a quick on at msdn but nothing familiar turnd up. So consider it a long, long shot.

Link to comment
Share on other sites

@khor, just remember my words when your statement comes back and bit you. ;) Been there done that. I did neither want to write a spreadsheet application. :lmao:

@Valuater, I might be wrong here but dont you have to get the brush information used by the system to draw the control to figure out it's current background color? Did a quick on at msdn but nothing familiar turnd up. So consider it a long, long shot.

thanks... I've got a couple of ideas I am working on now!!

thx

8)

NEWHeader1.png

Link to comment
Share on other sites

Here it is with sorting abilities. I just did this to see if it was possible. The main thing I don't like is how I did the part in func Button_Click because I believe it will not work right if another control is above it (not positive though). This needs GuiButton.au3 Colored Buttons by gafrost to work correctly.

I added all of your good suggestions as well Valuater.

Enjoy.

Kohr

CODE
;GUIButton written by gafrost
#include <GUIConstants.au3>
#include <array.au3>
#include "GuiButton.au3"

$File = @ScriptDir & "\Grid.txt"

Global $Cell[1][1]
Global $rows = 19
Global $columns = 12
Global $color = 0x00ff00
$width = 40
$height = 20
Dim $aSortButton[$columns + 1]

;~ $GUImain = GUICreate("Grid Example", 500, 450)
$GUImain = GUICreate("Grid Example", 500, 500, -1, -1,$WS_SIZEBOX)

$ret = 10
For $i = 1 To $columns
    $aSortButton[$i] = _GuiCtrlCreateButton ("Sort", $ret, 10, $width, $height, -1, $color)
    GUICtrlSetResizing($aSortButton[$i], 1)
    $ret += $width
Next

$aGrid = _Grid(10, 30, $width, $height, $rows, $columns, $color)

For $i = 1 To $rows
    For $j = 1 To $columns
        GUICtrlSetResizing ($aGrid[$i][$j], 1)
    Next
Next

_ColumnHeader("#")
_RowHeader("#")

GUICtrlSetBkColor($aGrid[1][1], 0xffffff)
GUICtrlSetBkColor($aGrid[2][2], 0xffff00)
GUICtrlSetBkColor($aGrid[2][3], 0xff00ff)
GUICtrlSetBkColor($aGrid[3][4], 0xff0000)
GUICtrlSetBkColor($aGrid[3][5], 0x00ffff)
GUICtrlSetBkColor($aGrid[4][6], 0x111111)
GUICtrlSetBkColor($aGrid[4][7], 0x12ffe0)
GUICtrlSetBkColor($aGrid[5][8], 0x6eff0e)

GUICtrlSetData($aGrid[7][4], "text")

$Save_btn = GUICtrlCreateButton("&Save Grid", 20, 420, 150, 30)
$Load_btn = GUICtrlCreateButton("&Load Grid", 175, 420, 150, 30)
$Exit_btn = GUICtrlCreateButton("&Exit Grid", 330, 420, 150, 30)

$k = 0
For $i = 2 To $rows
    For $j = 2 To $columns
        $k += 1
        GUICtrlSetData($aGrid[$i][$j],Chr(63 + $i) & $k)
    Next
Next

;needed for color buttons
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
; WM_DRAWITEM has to registered before showing GUI otherwise the initial drawing isn't done
GUIRegisterMsg($WM_DRAWITEM, "MY_WM_DRAWITEM")

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Or $msg = $Exit_btn Then Exit
   
    If $msg = $Save_btn Then _SaveGrid($File)
   
    If $msg = $Load_btn Then _LoadGrid($File)
   
WEnd

#cs
    _Grid ( $StartX, $StartY,  $cellWidth, $cellHeight, $rows, $columns )
   
    Parameters:
    $StartX - left coordinate to start grid
    $StartY - top coordinate to start grid.
    $cellWidth - Width of each cell.
    $cellHeight - Height of each cell.
    $rows - Total rows to create.
    $columns - Total columns to create.
   
    Return Value:
    Returns the identifier (controlID) of each new control in the grid.
    Array[0][0] = total controls
    Array[1][1] = 1st control (count going from left to right, by row)
#ce

Func _Grid($StartX, $StartY, $cellWidth, $cellHeight, $rows, $columns, $color = "")
    Local $tempX, $tempY
    ReDim $Cell[$rows + 1][$columns + 1]
    $tempX += $StartX
    $tempY += $StartY
    For $i = 1 To $rows
        For $j = 1 To $columns
            $Cell[$i][$j] = GUICtrlCreateInput("", $tempX, $tempY, $cellWidth, $cellHeight)
            If $color > "" And $i = 1 Or $j = 1 Then
                GUICtrlSetBkColor($Cell[$i][$j], $color)
            Else
                GUICtrlSetBkColor($Cell[$i][$j], 0xffffff)
            EndIf
            $tempX += $cellWidth
        Next
        $tempX = $StartX
        $tempY += $cellHeight
    Next
    $Cell[0][0] = UBound($Cell) - 1
    Return $Cell
EndFunc   ;==>_Grid

Func _ColumnHeader($GHeader, $GLock = 0)
    If $GHeader = "#" Then
        For $i = 2 To $columns
            GUICtrlSetData($aGrid[1][$i], "Col " & $i - 1)
            If $GLock Then GUICtrlSetStyle($Cell[1][$i], $WS_DISABLED)
        Next
    Else
        $GHeader = StringSplit($GHeader, ",")
        For $i = 1 To $GHeader[0]
            GUICtrlSetData($aGrid[1][$i], $GHeader[$i])
            If $GLock Then GUICtrlSetStyle($Cell[1][$i], $WS_DISABLED)
        Next
    EndIf
EndFunc   ;==>_ColumnHeader

Func _RowHeader($RHeader, $RLock = 0)
    If $RHeader = "#" Then
        For $i = 2 To $rows
            GUICtrlSetData($aGrid[$i][1], "R " & $i - 1)
            If $RLock Then GUICtrlSetStyle($Cell[$i][1], $WS_DISABLED)
        Next
    Else
        $RHeader = StringSplit($RHeader, ",")
        For $i = 1 To $RHeader[0]
            GUICtrlSetData($aGrid[$i][1], $RHeader[$i])
            If $RLock Then GUICtrlSetStyle($Cell[$i][1], $WS_DISABLED)
        Next
    EndIf
EndFunc   ;==>_RowHeader

Func _SaveGrid($SFile)
    FileDelete($SFile)
    For $i = 1 To $rows
        For $j = 1 To $columns
            FileWriteLine($SFile, GUICtrlRead($Cell[$i][$j]))
        Next
    Next
EndFunc   ;==>_SaveGrid

Func _LoadGrid($LFile)
    Local $x
    For $i = 1 To $rows
        For $j = 1 To $columns
            $x = $x + 1
            GUICtrlSetData($Cell[$i][$j], FileReadLine($LFile, $x))
        Next
    Next
EndFunc   ;==>_LoadGrid

Func _GridSort($SortColumn, $rows, $columns, $sortType)
    Dim $Data[$rows][$columns+1]
    For $i = 2 To $rows
        For $j = 1 To $columns
            $Data[$i-1][$j] = GUICtrlRead($aGrid[$i][$j])
        Next
    Next
;~      Console_Array2dDisplay($Data, "$output b4 sort")
    If $sortType = 1 Then
        _ArraySort($Data, 1, 1, -1, $columns+1, $SortColumn)
    Else
        _ArraySort($Data, "", 0, -1, $columns+1, $SortColumn)
    EndIf
;~      Console_Array2dDisplay($Data, "$output after sort")
    For $i = 2 To $rows
        For $j = 1 To $columns
            GUICtrlSetData($aGrid[$i][$j], $Data[$i-1][$j])
        Next
    Next
    Dim $Data = 1
EndFunc   ;==>_GridSort

Func Console_Array2dDisplay(ByRef $ar_Array, $s_Title = "Array contents", $n_Index = 1, $i_Message = 0)
    ; Display 2 dimensional array
    Local $output = ""
    Local $r, $c
    If Not IsArray($ar_Array) Then Return -1
    For $r = $n_Index To UBound($ar_Array, 1) - 1
        $output = $output & @LF
        For $c = 0 To UBound($ar_Array, 2) - 1
            $output = $output & $ar_Array[$r][$c] & " "
        Next
    Next
    ConsoleWrite($s_Title & "=" & @LF & $output & @LF)
    If $i_Message Then MsgBox(4096, $s_Title, $output)
    Return $output
EndFunc   ;==>Console_Array2dDisplay

Func Button_Click(ByRef $ctrl_id)
    $ret = _GuiCtrlButtonGetText($ctrl_id)
    If $ret = "Sort" Then
        _GuiCtrlButtonSetText($ctrl_id, "\/")
        _GridSort($ctrl_id - 2, $rows, $columns, 1)
    EndIf
    If $ret = "/\" Then
        _GuiCtrlButtonSetText($ctrl_id, "\/")
        _GridSort($ctrl_id - 2, $rows, $columns, 1)
    EndIf
    If $ret = "\/" Then
    ;ascend
        _GuiCtrlButtonSetText($ctrl_id, "/\")
        _GridSort($ctrl_id - 2, $rows, $columns, 0)
    EndIf
    For $i = 3 to $columns + 2
        If $i <> $ctrl_id Then
            _GuiCtrlButtonSetText($i, "Sort")
        EndIf
    Next
EndFunc   ;==>Button_Click
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...