Jump to content

Recommended Posts

Hello everyone,

this is my first post in the forum, so apologize if this might not be the right section.

 

I am using the GUIListViewEx UDF from @Melba23 (thx for that awesome UDF!)

I started creating a simple CRUD (Create-Update-Delete) Application, based on one of the provided examples, and I am struggling to create the "Create"

When pressing the Create button, I created another form that appears. I would like to use this form to add a new entry into the table.

image.png.db51867c3114157d3e7c7f2e38c23a32.png

However, I am struggling how to read the values and add them to the table.

Please find below my minimum viable example.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <StaticConstants.au3>
#include <UpDownConstants.au3>
#include <EditConstants.au3>

#include <File.au3>
#include <Misc.au3>
#include <String.au3>
#include <Array.au3>

#include "GUIListViewEx.au3"

Global $sRet

$hGUI = GUICreate("Task Table Helper", 750, 510)

; Create ListView
GUICtrlCreateLabel("Task Table", 10, 10, 400, 20)
$cLV_1 = GUICtrlCreateListView("#|Title|Image|Post|Schedule Date|Status", 10, 30, 720, 260, BitOR($LVS_SINGLESEL, $LVS_SHOWSELALWAYS))

_GUICtrlListView_SetExtendedListViewStyle($cLV_1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_HEADERDRAGDROP))
For $i = 0 To 3
    _GUICtrlListView_SetColumnWidth($cLV_1, $i, 100)
Next

; Create array and fill listview
Global $aLVArray_1[8][6]

; Initiate ListView = sort on column click - editable headers - header colours - user colours
$iLVIndex_1 = _GUIListViewEx_Init($cLV_1, $aLVArray_1, 0, 0, True, 1 + 8 + 32) ; + 16

; Set column edit status
_GUIListViewEx_SetEditStatus($iLVIndex_1, 1)                   ; Default = standard text edit
_GUIListViewEx_SetEditStatus($iLVIndex_1, 2, 2, "1|2|3", True) ; 2 = Read-only combo
_GUIListViewEx_SetEditStatus($iLVIndex_1, 3, 3)                ; 3 = DTP
_GUIListViewEx_SetEditStatus($iLVIndex_1, 4)                   ; Default = standard text edit

; Create buttons for LH ListView
GUICtrlCreateGroup("", 10, 300, 480, 160)

GUICtrlCreateLabel("Up/Down", 25, 320, 80, 20, $SS_CENTER)
$cUp = GUICtrlCreateButton("Up", 25, 340, 80, 30)
$cDown = GUICtrlCreateButton("Down", 25, 380, 80, 30)

GUICtrlCreateLabel("Row", 150, 320, 80, 20, $SS_CENTER)
$cIns = GUICtrlCreateButton("Insert", 150, 340, 80, 30)
$cDel = GUICtrlCreateButton("Delete", 150, 380, 80, 30)
$cCre = GUICtrlCreateButton("Create", 150, 420, 80, 30)

GUICtrlCreateLabel("Run/Stop", 275, 320, 80, 20, $SS_CENTER)
$cRun = GUICtrlCreateButton("Run", 275, 340, 80, 30)
$cStop = GUICtrlCreateButton("Stop", 275, 380, 80, 30)

GUICtrlCreateLabel("Read", 400, 320, 80, 20, $SS_CENTER)
$cContent = GUICtrlCreateButton("Content", 400, 340, 80, 30)
$cHeaders = GUICtrlCreateButton("Headers", 400, 380, 80, 30)

; Create additional buttons
$cSave = GUICtrlCreateButton("Save LH ListView", 10, 470, 150, 30)
$cLoad = GUICtrlCreateButton("Load RH ListView", 180, 470, 150, 30)
GUICtrlSetState($cLoad, $GUI_DISABLE)
$cExit = GUICtrlCreateButton("Exit", 360, 470, 110, 30)

; If colours used then this function must be run BEFORE GUISetState
_GUIListViewEx_MsgRegister()

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE, $cExit
            MsgBox(0, "Close", "SAVE BEFORE CLOSING!!!")
            Exit

        Case $cSave
            _GUIListViewEx_SaveListView($iLVIndex_1, "Save.lvs")
            GUICtrlSetState($cLoad, $GUI_ENABLE)
        Case $cLoad
            ; _GUIListViewEx_LoadListView($iLVIndex_2, "Save.lvs") ; But return now forced to 2D

        Case $cUp
            _GUIListViewEx_SetActive($iLVIndex_1)
            _GUIListViewEx_Up()

        Case $cDown
            _GUIListViewEx_SetActive($iLVIndex_1)
            _GUIListViewEx_Down()

        Case $cIns
            ; Insert row/col
            _GUIListViewEx_SetActive($iLVIndex_1)
            _GUIListViewEx_Insert("txt")
        Case $cCre
            ; MsgBox(0, "Dragging", "Test")
            _addToTable()

        Case $cDel
            ; Delete row/col
            _GUIListViewEx_SetActive($iLVIndex_1)
                _GUIListViewEx_Delete()
        Case $cHeaders
            $aRet = _GUIListViewEx_ReturnArray($iLVIndex_1, 4)
            _ArrayDisplay($aRet, "", Default, 8)

    EndSwitch

    $vRet = _GUIListViewEx_EventMonitor()
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Error", "Event error: " & @error)
    EndIf
    Switch @extended
        Case 0
            ; No event detected
        Case 1
            If $vRet = "" Then
                 MsgBox($MB_SYSTEMMODAL, "Edit", "Edit aborted" & @CRLF)
            Else
                _ArrayDisplay($vRet, "ListView " & _GUIListViewEx_GetActive() & " content edited", Default, 8)
            EndIf
        Case 2
            If $vRet = "" Then
                MsgBox($MB_SYSTEMMODAL, "Header edit", "Header edit aborted" & @CRLF)
            Else
                _ArrayDisplay($vRet, "ListView " & _GUIListViewEx_GetActive() & " header edited", Default, 8)
            EndIf
        Case 3
            MsgBox($MB_SYSTEMMODAL, "Sorted", "ListView: " & $vRet & @CRLF)
        Case 4
            Local $aRet = StringSplit($vRet, ":")
            MsgBox($MB_SYSTEMMODAL, "Dragged", "From ListView " & $aRet[1] & @CRLF & "To ListView " & $aRet[2])
    EndSwitch

WEnd

Func _addToTable()
    $Form1_1 = GUICreate("Add to Table", 389, 340, 253, 140)
    $Title = GUICtrlCreateLabel("Title", 16, 32, 24, 17)
    $Input1 = GUICtrlCreateInput("", 56, 32, 305, 21)
    $Image = GUICtrlCreateLabel("Image", 16, 72, 33, 17)
    $Input2 = GUICtrlCreateInput("", 56, 72, 305, 21)
    $Post = GUICtrlCreateLabel("Post", 16, 112, 25, 17)
    $Input3 = GUICtrlCreateInput("", 56, 112, 305, 70)
    $btnAddToTable = GUICtrlCreateButton("Add to Table", 216, 296, 137, 33)
    $btnCancel = GUICtrlCreateButton("Cancel", 40, 296, 145, 33)

    $Date = GUICtrlCreateLabel("Date", 16, 200, 27, 17)
    $idStart = GUICtrlCreateDate("2019/07/03 12:13:30", 56, 200, 305, 21)

    $stat = GUICtrlCreateLabel("Status", 16, 225, 25, 17)
    ; $iStat = GUICtrlCreateInput("", 56, 225, 305, 21)
    $iStat = GUICtrlCreateCombo("Closed", 56, 225, 305, 21)

    ; Add additional items to the combobox.
    GUICtrlSetData($iStat, "Active|Pending", "Active")

    ; $Input4 = GUICtrlCreateInput("", 56, 200, 305, 21)
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE, $btnCancel
                Exit

            Case $btnAddToTable
            ; add the values from the text field to the table
        EndSwitch
    WEnd

EndFunc

Any suggestions how to take the values and add them to my table?

I appreciate your replies!

 

Share this post


Link to post
Share on other sites

There is multiple ways to achieve what you want.  The easiest solution that I am thinking would be to pass a small array (by ref) to your function _addToTable ().  Something like :

Local $aTable[5] ; place that at the beginning of the script

_addToTable ($aTable) ; is how you will call your function

; now $aTable contains the 5 fields of your _addToTable GUI

Func _addToTable (ByRef $aField)  ; is how your function will be declared

Case $btnAddToTable
  $aField[0] = GUICtrlRead ($input1)
  $aField[1] = GUICtrlRead ($input2)
  $aField[2] = GUICtrlRead ($input3)
  $aField[3] = GUICtrlRead ($idStart)
  $aField[4] = GUICtrlRead ($iStat)

Untested, just wrote it fast cause I got to go in a sec....but you will get the idea

Share this post


Link to post
Share on other sites

Hi @algospider, and welcome to the AutoIt forums :welcome:

As @Nine suggested, you could use an array in which you store the fields' values, and then write them through _GUICtrlListView_AddArray(), for example.

If you need further information, feel free to ask (providing an example of what you tried and what you are trying to accomplish) :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Posted (edited)

algospider,

This is how you do it:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <StaticConstants.au3>
#include <UpDownConstants.au3>
#include <EditConstants.au3>

#include <File.au3>
#include <Misc.au3>
#include <String.au3>
#include <Array.au3>

#include "GUIListViewEx.au3"

Global $sRet

$hGUI = GUICreate("Task Table Helper", 750, 510)

; Create ListView
GUICtrlCreateLabel("Task Table", 10, 10, 400, 20)
$cLV_1 = GUICtrlCreateListView("#|Title|Image|Post|Schedule Date|Status", 10, 30, 720, 260, BitOR($LVS_SINGLESEL, $LVS_SHOWSELALWAYS))

_GUICtrlListView_SetExtendedListViewStyle($cLV_1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_HEADERDRAGDROP))
For $i = 0 To 3
    _GUICtrlListView_SetColumnWidth($cLV_1, $i, 100)
Next

; Create array and fill listview
;Global $aLVArray_1[8][6] ; You have no content at the start so you do not need an array <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; Initiate ListView = sort on column click - editable headers - header colours - user colours
$iLVIndex_1 = _GUIListViewEx_Init($cLV_1, "", 0, 0, True, 1 + 8 + 32) ; + 16 ; And you just use an empty string when intialising the ListView <<<<<<<<<<

; Set column edit status
_GUIListViewEx_SetEditStatus($iLVIndex_1, 1)                   ; Default = standard text edit
_GUIListViewEx_SetEditStatus($iLVIndex_1, 2, 2, "1|2|3", True) ; 2 = Read-only combo
_GUIListViewEx_SetEditStatus($iLVIndex_1, 3, 3)                ; 3 = DTP
_GUIListViewEx_SetEditStatus($iLVIndex_1, 4)                   ; Default = standard text edit

; Create buttons for LH ListView
GUICtrlCreateGroup("", 10, 300, 480, 160)

GUICtrlCreateLabel("Up/Down", 25, 320, 80, 20, $SS_CENTER)
$cUp = GUICtrlCreateButton("Up", 25, 340, 80, 30)
$cDown = GUICtrlCreateButton("Down", 25, 380, 80, 30)

GUICtrlCreateLabel("Row", 150, 320, 80, 20, $SS_CENTER)
$cIns = GUICtrlCreateButton("Insert", 150, 340, 80, 30)
$cDel = GUICtrlCreateButton("Delete", 150, 380, 80, 30)
$cCre = GUICtrlCreateButton("Create", 150, 420, 80, 30)

GUICtrlCreateLabel("Run/Stop", 275, 320, 80, 20, $SS_CENTER)
$cRun = GUICtrlCreateButton("Run", 275, 340, 80, 30)
$cStop = GUICtrlCreateButton("Stop", 275, 380, 80, 30)

GUICtrlCreateLabel("Read", 400, 320, 80, 20, $SS_CENTER)
$cContent = GUICtrlCreateButton("Content", 400, 340, 80, 30)
$cHeaders = GUICtrlCreateButton("Headers", 400, 380, 80, 30)

; Create additional buttons
$cSave = GUICtrlCreateButton("Save LH ListView", 10, 470, 150, 30)
$cLoad = GUICtrlCreateButton("Load RH ListView", 180, 470, 150, 30)
GUICtrlSetState($cLoad, $GUI_DISABLE)
$cExit = GUICtrlCreateButton("Exit", 360, 470, 110, 30)

; If colours used then this function must be run BEFORE GUISetState
_GUIListViewEx_MsgRegister()

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE, $cExit
            MsgBox(0, "Close", "SAVE BEFORE CLOSING!!!")
            Exit

        Case $cSave
            _GUIListViewEx_SaveListView($iLVIndex_1, "Save.lvs")
            GUICtrlSetState($cLoad, $GUI_ENABLE)
        Case $cLoad
            ; _GUIListViewEx_LoadListView($iLVIndex_2, "Save.lvs") ; But return now forced to 2D

        Case $cUp
            _GUIListViewEx_SetActive($iLVIndex_1)
            _GUIListViewEx_Up()

        Case $cDown
            _GUIListViewEx_SetActive($iLVIndex_1)
            _GUIListViewEx_Down()

        Case $cIns
            ; Insert row/col
            _GUIListViewEx_SetActive($iLVIndex_1)
            _GUIListViewEx_Insert("txt")

        Case $cCre
            ; MsgBox(0, "Dragging", "Test")
            _addToTable()

        Case $cDel
            ; Delete row/col
            _GUIListViewEx_SetActive($iLVIndex_1)
                _GUIListViewEx_Delete()
        Case $cHeaders
            $aRet = _GUIListViewEx_ReturnArray($iLVIndex_1, 4)
            _ArrayDisplay($aRet, "", Default, 8)

    EndSwitch

    $vRet = _GUIListViewEx_EventMonitor()
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Error", "Event error: " & @error)
    EndIf
    Switch @extended
        Case 0
            ; No event detected
        Case 1
            If $vRet = "" Then
                 MsgBox($MB_SYSTEMMODAL, "Edit", "Edit aborted" & @CRLF)
            Else
                _ArrayDisplay($vRet, "ListView " & _GUIListViewEx_GetActive() & " content edited", Default, 8)
            EndIf
        Case 2
            If $vRet = "" Then
                MsgBox($MB_SYSTEMMODAL, "Header edit", "Header edit aborted" & @CRLF)
            Else
                _ArrayDisplay($vRet, "ListView " & _GUIListViewEx_GetActive() & " header edited", Default, 8)
            EndIf
        Case 3
            MsgBox($MB_SYSTEMMODAL, "Sorted", "ListView: " & $vRet & @CRLF)
        Case 4
            Local $aRet = StringSplit($vRet, ":")
            MsgBox($MB_SYSTEMMODAL, "Dragged", "From ListView " & $aRet[1] & @CRLF & "To ListView " & $aRet[2])
    EndSwitch

WEnd

Func _addToTable()
    $Form1_1 = GUICreate("Add to Table", 389, 340, 253, 140)
    $Title = GUICtrlCreateLabel("Title", 16, 32, 24, 17)
    $Input1 = GUICtrlCreateInput("", 56, 32, 305, 21)
    $Image = GUICtrlCreateLabel("Image", 16, 72, 33, 17)
    $Input2 = GUICtrlCreateInput("", 56, 72, 305, 21)
    $Post = GUICtrlCreateLabel("Post", 16, 112, 25, 17)
    $Input3 = GUICtrlCreateInput("", 56, 112, 305, 70)
    $btnAddToTable = GUICtrlCreateButton("Add to Table", 216, 296, 137, 33)
    $btnCancel = GUICtrlCreateButton("Cancel", 40, 296, 145, 33)

    $Date = GUICtrlCreateLabel("Date", 16, 200, 27, 17)
    $idStart = GUICtrlCreateDate("2019/07/03 12:13:30", 56, 200, 305, 21)

    $stat = GUICtrlCreateLabel("Status", 16, 225, 25, 17)
    ; $iStat = GUICtrlCreateInput("", 56, 225, 305, 21)
    $iStat = GUICtrlCreateCombo("Closed", 56, 225, 305, 21)

    ; Add additional items to the combobox.
    GUICtrlSetData($iStat, "Active|Pending", "Active")

    ; $Input4 = GUICtrlCreateInput("", 56, 200, 305, 21)
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE, $btnCancel
                ExitLoop

            Case $btnAddToTable
                ; add the values from the text field to the table
                ; Get the data into a delimited string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                $sData = "#" & "|" & GUICtrlRead($Input1) & "|" & _
                            GUICtrlRead($Input2) & "|" & GUICtrlRead($Input3) & "|" & _
                            GUICtrlRead($idStart) &"|" & GUICtrlRead($iStat)
                ; And add it to the ListView <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                _GUIListViewEx_Insert($sData)
                ExitLoop
        EndSwitch
    WEnd

    GUIDelete($Form1_1)

EndFunc

With this UDF you MUST use the UDF functions to add/delete/modify the ListView content or it will not work. This is because the UDF uses a shadow array to reflect the ListView content and merely adding data to the ListView itself will mean the control and the array will no longer be synchronised leading to usually catastrophic results.

M23

Edited by Melba23
29k

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...