Sign in to follow this  
Followers 0
zvikush

get text from GUICtrlListView

4 posts in this topic

#1 ·  Posted (edited)

hey everyone...

i have created a gui that has a GUICtrlListView thet retrievs data from a sql database

and lets me add values to the database+GUI $delete values from the database+gui...

the thing is that i don't find how to delete a selected item and retrieving the text content of the item in order to delete it from the sql database!

please help... here's the code... (the problem part is marked with ?????)

CODE
#include <SQLite.au3>

#include <SQLite.dll.au3>

#include <GuiConstantsEx.au3>

#include <GuiListView.au3>

#RequireAdmin

global $system_log = @ScriptDir&"\logs\system.txt"

global $orders_log = @ScriptDir&"\logs\orders.txt"

global $customer_actions_log = @ScriptDir&"\logs\customer_actions.txt"

global $products_change_log = @ScriptDir&"\logs\products_change.txt"

global $main_db = @ScriptDir&"\db\main.db"

global $phone_num, $first_name, $last_name, $city, $street_name, $street_num, $house_number,$product,$product_price

global $print,$parent,$child1,$child2,$child3

global $product_array[1000],$product_item_arrey[1000],$listview,$delete_product_array[1000]

GUISwitch ( $parent )

GUISetState (@SW_DISABLE )

$child2=GUICreate (" ניהול מוצרים")

GUISetState (@SW_show)

GUISetState (@SW_MAXIMIZE)

$listview = _GUICtrlListView_Create ($child2,"",300,60,400,250 )

_GUICtrlListView_InsertColumn ($ListView, 0, "מוצר", 100)

_GUICtrlListView_InsertColumn ($ListView, 1, "מחיר", 100)

call ("display_products")

$product_array=StringSplit($print, @CRLF,1)

for $list_item_pointer=2 to $product_array[0]-1

$product_array_2=Stringsplit($product_array[$list_item_pointer], @tab)

_GUICtrlListView_AddItem ($ListView, $product_array_2[1], $list_item_pointer-2)

_GUICtrlListView_AddSubItem ($ListView, $list_item_pointer-2, $product_array_2[2],1 ,$list_item_pointer-1)

Next

$end_button = GuiCtrlCreateButton ("יציאה",300,500,100,30)

$del_button = GuiCtrlCreateButton ("מחיקה",450,500,100,30)

$add_button = GuiCtrlCreateButton ("הוספה",600,500,100,30)

GUICtrlCreateLabel (": מוצר",640, 25, 100, 20)

GUICtrlCreateLabel (": מחיר", 440, 25, 100, 20)

$product=""

$product_price=""

$product_price_pointer=GUICtrlCreateInput ("", 335, 21, 100, 20)

$product_pointer=GUICtrlCreateInput ("", 535,21, 100, 20)

$msg = 0

While 1

$child2_msg = GUIGetMsg()

Select

Case $child2_msg = $add_button

;msgbox (4096,"",GUICtrlRead($product) &" : "&GUICtrlRead($product_price))

if GUICtrlRead($product_pointer)<>"" And GUICtrlRead($product_price_pointer)<>"" Then

$product=GUICtrlRead($product_pointer)

$product_price=GUICtrlRead($product_price_pointer)

call ("display_one_product")

if $print <> "no product" Then

msgbox (4096," !!!!","המוצר קיים")

Else

_GUICtrlListView_AddItem ($ListView, $product, $list_item_pointer-2)

_GUICtrlListView_AddSubItem ($ListView, $list_item_pointer-2, $product_price,1 ,$list_item_pointer-1)

$list_item_pointer=$list_item_pointer+1

call ("add_product")

EndIf

Else

msgbox (4096," חסרים פרטים","אנא מלא פרטי מוצר ומחיר")

endif

Case $child2_msg = $del_button

???????????????????

case $child2_msg=$GUI_EVENT_CLOSE or $child2_msg = $end_button

call ("end_child_continue_parent",$child2)

Exit

EndSelect

Wend

Func add_product () ;adds a line to the database

_SQLite_Startup ()

If @error > 0 Then

MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")

_FileWriteLog ( $system_log, "SQLite.dll Can't be Loaded! exiting..." )

Exit - 1

EndIf

_SQLite_Open ($main_db)

If @error > 0 Then

MsgBox(16, "SQLite Error", "Can't Load Database!")

_FileWriteLog ( $system_log, "Can't Load Database! exiting..." )

Exit - 1

EndIf

;_SQLite_Exec(-1,"Create table producttab (product varchar ,price float);")

_SQLite_Exec(-1,"Insert into producttab values ('"&$product&"',"& $product_price&");")

if @error Then

_FileWriteLog ( $products_change_log, "failure: "&"Insert into producttab values ('"&$product&"','"& $product_price&");" )

msgbox (4096,@ScriptName,"אין אפשרות להכנסת מוצר לבסיס הנתונים")

Else

_FileWriteLog ( $products_change_log, "success: "&"Insert into producttab values ('"&$product&"','"& $product_price&");" )

EndIf

_SQLite_Close ()

_SQLite_Shutdown ()

EndFunc

Func del_product () ;adds a line to the database

_SQLite_Startup ()

If @error > 0 Then

MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")

_FileWriteLog ( $system_log, "SQLite.dll Can't be Loaded! exiting..." )

Exit - 1

EndIf

_SQLite_Open ($main_db)

If @error > 0 Then

MsgBox(16, "SQLite Error", "Can't Load Database!")

_FileWriteLog ( $system_log, "Can't Load Database! exiting..." )

Exit - 1

EndIf

;_SQLite_Exec(-1,"Create table producttab (product varchar ,price float);")

_SQLite_Exec(-1,"delete from producttab WHERE product='"&$product&"';")

if @error Then

_FileWriteLog ( $products_change_log, "failure: "&"delete from producttab WHERE product='"&$product&"';")

msgbox (4096,@ScriptName,"אין אפשרות להכנסת מוצר לבסיס הנתונים")

Else

_FileWriteLog ( $products_change_log, "success: "&"delete from producttab WHERE product='"&$product&"';" )

EndIf

_SQLite_Close ()

_SQLite_Shutdown ()

EndFunc

Func display_products () ;gives the product list in $print (can be seperated by @crlf)

_SQLite_Startup ()

If @error > 0 Then

MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")

_FileWriteLog ( $system_log, "SQLite.dll Can't be Loaded! exiting..." )

Exit - 1

EndIf

_SQLite_Open ($main_db)

If @error > 0 Then

MsgBox(16, "SQLite Error", "Can't Load Database!")

_FileWriteLog ( $system_log, "Can't Load Database! exiting..." )

Exit - 1

EndIf

FileDelete (@ScriptDir&"\tempfile.txt")

$print = ""

$d = _SQLite_Exec(-1,"SELECT * FROM producttab","_cb") ; _cb Will be called for each row

if $print = "" Then

$print = "no products"

EndIf

_SQLite_Close ()

_SQLite_Shutdown ()

Return $print

EndFunc

Func display_one_product () ;gives one product line in $print

_SQLite_Startup ()

If @error > 0 Then

MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")

_FileWriteLog ( $system_log, "SQLite.dll Can't be Loaded! exiting..." )

Exit - 1

EndIf

_SQLite_Open ($main_db)

If @error > 0 Then

MsgBox(16, "SQLite Error", "Can't Load Database!")

_FileWriteLog ( $system_log, "Can't Load Database! exiting..." )

Exit - 1

EndIf

FileDelete (@ScriptDir&"\tempfile.txt")

$print = ""

$d = _SQLite_Exec(-1,"SELECT * FROM producttab WHERE product='"&$product&"'","_cb") ; _cb Will be called for each row

if $print = "" Then

$print = "no product"

EndIf

_SQLite_Close ()

_SQLite_Shutdown ()

Return $print

EndFunc

func end_child_continue_parent ($gui_name)

GUIDelete ($gui_name)

GUISwitch ( $parent )

GUISetState (@SW_ENABLE )

EndFunc

Func _cb($aRow)

For $s In $aRow

$print=$print & $s & @TAB

Next

$print = $print & @CRLF

EndFunc

Edited by zvikush

Share this post


Link to post
Share on other sites



Examples:

#include <GuiConstants.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>

Opt("GuiOnEventMode", 1)

Global Enum $idMenuRead = 1000, $idMenuDelete

$hGui = GUICreate("Test GUI", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "MainEvent")

$ReadButton = GUICtrlCreateButton("Read", 10, 175, 70, 20)
GUICtrlSetOnEvent(-1, "MainEvent")

$DeleteButton = GUICtrlCreateButton("Delete", 220, 175, 70, 20)
GUICtrlSetOnEvent(-1, "MainEvent")

$hListView = _GUICtrlListView_Create($hGui, "Items|SubItems", 10, 10, 280, 160, -1, $WS_EX_CLIENTEDGE)

_GUICtrlListView_AddItem($hListView, "Item1")
_GUICtrlListView_AddItem($hListView, "Item2")
_GUICtrlListView_AddItem($hListView, "Item3")
_GUICtrlListView_AddItem($hListView, "Item4")

_GUICtrlListView_AddSubItem($hListView, 0, "SubItem1", 1)
_GUICtrlListView_AddSubItem($hListView, 1, "SubItem2", 1)
_GUICtrlListView_AddSubItem($hListView, 2, "SubItem3", 1)
_GUICtrlListView_AddSubItem($hListView, 3, "SubItem4", 1)

_GUICtrlListView_SetColumnWidth($hListView, 0, 150)
_GUICtrlListView_SetColumnWidth($hListView, 1, 120)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

While 1
    Sleep(100)
WEnd

Func MainEvent()
    Local $iIndice
    
    Switch @GUI_CtrlId
    Case $GUI_EVENT_CLOSE
        Exit
    Case $ReadButton
        $iIndice = _GUICtrlListView_GetSelectedIndices($hListView)
        If $iIndice = "" Then Return
        MsgBox(0, "", _GUICtrlListView_GetItemText($hListView, $iIndice))
    Case $DeleteButton
        $iIndice = _GUICtrlListView_GetSelectedIndices($hListView)
        If $iIndice = "" Then Return
        _GUICtrlListView_DeleteItem($hListView, $iIndice)
    EndSwitch
EndFunc ;==>MainEvent

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode, $tInfo, $iIndex
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $HwndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $HwndFrom
    Case $hListView
        Switch $iCode
        Case $NM_RCLICK
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
            $iIndex = DllStructGetData($tInfo, "Index")
            If $iIndex <> -1 Then _Menu($iIndex)
        EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
    
EndFunc ;==>WM_NOTIFY

Func _Menu($sIndex)
    Local $hMenu
    $hMenu = _GUICtrlMenu_CreatePopup()
    
    _GUICtrlMenu_AddMenuItem($hMenu, "Read", $idMenuRead)
    _GUICtrlMenu_AddMenuItem($hMenu, "Delete", $idMenuDelete)
    
    Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $hListView, -1, -1, 1, 1, 2)
    Case $idMenuRead
        MsgBox(0, "", _GUICtrlListView_GetItemText($hListView, $sIndex))
    Case $idMenuDelete
        _GUICtrlListView_DeleteItem($hListView, $sIndex)
    EndSwitch
    
    _GUICtrlMenu_DestroyMenu($hMenu)
EndFunc ;==>_Menu
:)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

rasim nice script but now my $DoubleClicked func dont work anymore how i can fix it that it works again with _GUICtrlListView_Create

it was GUICtrlCreateListView

?

Edited by yucatan

Share this post


Link to post
Share on other sites

rasim nice script but now my $DoubleClicked func dont work anymore how i can fix it that it works again with _GUICtrlListView_Create

it was GUICtrlCreateListView

?

Answer

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