Jump to content

How to delete item from list by pressing DEL key?


VAN0
 Share

Recommended Posts

Hello.

I must be missing something obvious, but I can't figure out how to "capture" when an item in list selected and DELETE key pressed.

The whole routine deleting the selected item I'll figure out myself, just need help with keyboard part.

Thank you.

P.S.

Something wrong with search engine on this forum? getting white page when trying search.

Link to comment
Share on other sites

  • Moderators

VAN0,

You could do it this way: :idea:

#include <GUIConstantsEx.au3>
#include <Misc.au3>

$dll = DllOpen("user32.dll")

$hGUI = GUICreate("Test", 500, 500)

$hList = GUICtrlCreateList("", 10, 10, 480, 480)
GUICtrlSetData($hList, "Line 1|Line 2|Line 3")

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($dll)
            Exit
    EndSwitch

    If _IsPressed("2E", $dll) Then
        ; Wait for key to be released to avoid multiple firing the code below
        While _IsPressed("2E", $dll)
            Sleep(10)
        WEnd
        If GUICtrlRead($hList) <> "" Then
            MsgBox(0, "", "DELETE pressed with an item selected")
        EndIf
    EndIf

WEnd

M23

Edit: Added code to wait for key release - it caused problems later in the script if not.

Edited by Melba23

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

 

Link to comment
Share on other sites

I've added something at Melba's code:

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <GUIListBox.au3>

$dll = DllOpen("user32.dll")

$hGUI = GUICreate("Test", 500, 500)

$hList = GUICtrlCreateList("", 10, 10, 480, 480)
GUICtrlSetData($hList, "Line 1|Line 2|Line 3")

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($dll)
            Exit
    EndSwitch

    If _IsPressed("2E", $dll) Then
        Local $indice = _GUICtrlListBox_GetCurSel($hList)
        Local $iMsgBoxAnswer = MsgBox(36, "", "Are you sure?")
        Select
            Case $iMsgBoxAnswer = 6 ;Yes
                _GUICtrlListBox_DeleteString($hList, $indice)
            Case $iMsgBoxAnswer = 7 ;No
                ;Exit
        EndSelect
    EndIf

WEnd

[edit] I've used this in a right-click menu, because I didn't know how to use DELETE. Thank's Melba!

Edited by taietel
Link to comment
Share on other sites

Thank you very much guys, this is exactly what I needed!

Works perfectly.

P.S. now discovered another issue with _ArrayDelete(), I have to pause the script after using it, otherwise it corrupts array for some reason..

Link to comment
Share on other sites

Actually, I take it back, there is an issue with this method, it "capture" key press even when another program is opened, and even if list is not in currently in focus..

@taietel:

would you share you right click menu code? I think both method would be more useful in this case.

Thank you.

Edited by VAN0
Link to comment
Share on other sites

  • Moderators

VAN0,

Can you post some code which shows this array corruption because I have not heard of it happening before. :idea:

M23

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

 

Link to comment
Share on other sites

VAN0, of course! Here is a sample:

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 177, 267, 371, 201)
$lstDiscipline = GUICtrlCreateList("", 8, 8, 161, 214)
GUICtrlSetData($lstDiscipline, "Biologie|Chimie|Educatie Fizica si Sport|Fizica|Geografie|Istorie|Limba Engleza|Limba Franceza|Limba Romana|Matematica|Purtare|TIC")
Global $lstDisciplinecontext = GUICtrlCreateContextMenu($lstDiscipline)
Global $cmnuAdaugaDisciplina = GUICtrlCreateMenuItem("&Add discipline", $lstDisciplinecontext)
Global $cmnuStergeDisciplina = GUICtrlCreateMenuItem("&Delete discipline", $lstDisciplinecontext)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
 $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cmnuAdaugaDisciplina
            AdaugaDisciplina()
        Case $cmnuStergeDisciplina
            StergeDisciplina()

    EndSwitch
WEnd
Func StergeDisciplina()
    Local $indice = _GUICtrlListBox_GetCurSel($lstDiscipline)
    Local $iMsgBoxAnswer = MsgBox(36, "Atentie!", "Esti sigur(a) ca vrei sa stergi?")
    Select
        Case $iMsgBoxAnswer = 6 ;Da
            _GUICtrlListBox_DeleteString($lstDiscipline, $indice)
        Case $iMsgBoxAnswer = 7 ;Nu
            Exit
    EndSelect
EndFunc   ;==>StergeDisciplina

Func AdaugaDisciplina()
    Local $pozitie = MouseGetPos()
    Local $AdaugaDisciplina = GUICreate("Adauga Disciplina", 340, 40, $pozitie[0] + 5, $pozitie[1] + 5, BitOR($WS_SYSMENU, $WS_POPUP, $WS_POPUPWINDOW, $WS_BORDER, $WS_CLIPSIBLINGS))
    Local $txtAdaugaDisciplina = GUICtrlCreateInput("", 8, 8, 217, 21)
    Local $btnAdaugaDisciplina = GUICtrlCreateButton("Adauga...", 232, 7, 97, 25, $WS_GROUP)
    WinSetTrans("Adauga Disciplina", "", 240)
    GUISetState(@SW_SHOW, $AdaugaDisciplina)
    Do
        Local $actiune = GUIGetMsg()
        If $actiune = $btnAdaugaDisciplina Then
            Local $listaDiscipline = ""
            For $i = 0 To _GUICtrlListBox_GetCount($lstDiscipline) -1
                $listaDiscipline &= _GUICtrlListBox_GetText($lstDiscipline, $i) & "|"
            Next
            Local $comparaLista = StringSplit($listaDiscipline,"|")
            If StringInStr(StringUpper($listaDiscipline), StringUpper(GUICtrlRead($txtAdaugaDisciplina))) Then
                MsgBox(48, "Situatii Statistice © 2010", "Disciplina " & StringUpper(GUICtrlRead($txtAdaugaDisciplina)) & " exista deja!")
            Else
                _GUICtrlListBox_AddString($lstDiscipline, StringUpper(GUICtrlRead($txtAdaugaDisciplina)))
            EndIf
            GUIDelete($AdaugaDisciplina)
        EndIf
    Until $actiune = $btnAdaugaDisciplina
EndFunc   ;==>AdaugaDisciplina

The variables are in my language. I hope it's not an issue!

Link to comment
Share on other sites

  • Moderators

Yashied,

Why not? :)

VAN0,

Here is the example reworked: :idea:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$hList = GUICtrlCreateList("", 10, 10, 480, 480)
GUICtrlSetData($hList, "Line 1|Line 2|Line 3")

; Set accelerator for Del
$hDelKey = GUICtrlCreateDummy()
Dim $AccelKeys[1][2]=[["{DELETE}", $hDelKey]]
GUISetAccelerators($AccelKeys)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDelKey
            If GUICtrlRead($hList) <> "" Then
                MsgBox(0, "", "DELETE pressed with an item selected")
            EndIf
    EndSwitch

WEnd

You choose! :(

M23

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

 

Link to comment
Share on other sites

It is better that DEL will be triggered only if List has a focus.

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDelKey
            If (_WinAPI_GetFocus() = GUICtrlGetHandle($hList)) And (GUICtrlRead($hList)) Then
                MsgBox(0, "", "DELETE pressed with an item selected")
            EndIf
    EndSwitch
WEnd
Edited by Yashied
Link to comment
Share on other sites

Wow, guys, thank you very much, the last code works wonderful.

@taietel:

your example looks awesome, will try integrate it in my script :) Thanks.

VAN0,Can you post some code which shows this array corruption because I have not heard of it happening before. :idea: M23

After more testing it turned out to be some sort of conflict with your first del key code, for some bizarre reason all _Array... functions would return "0" (error 1 - Returns 0 if parameter is not an array), but if I add sleep(100), before an _ArrayUnique (or any other _Array function) it worked fine...very weird. I'll see if I can reproduce this weirdness in a sample code, otherwise we could blame my sloppy coding :(
Link to comment
Share on other sites

  • Moderators

VAN0,

I'll see if I can reproduce this weirdness in a sample code

I get no problems using _Array* functions with my code - nor would I expect any. Please do try and get some reproducer code. But if that proves too difficult, and you do not want to post your code in open forum but do not mind me seeing it, feel free to PM it to me so I can take a look. :idea:

M23

Edit: Clarity, I hope! :)

Edited by Melba23

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

 

Link to comment
Share on other sites

Ok, I got everything working perfectly, thank you very much guys.

@taietel:

One little snag I found with your code: it showed context menu even if nothing selected in the list.

In case someone searching for this, here is "fixed" version of your example, it shows "delete" option only when an item selected:

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 177, 267, 371, 201)
$lstDiscipline = GUICtrlCreateList("", 8, 8, 161, 214)
GUICtrlSetData($lstDiscipline, "Biologie|Chimie|Educatie Fizica si Sport|Fizica|Geografie|Istorie|Limba Engleza|Limba Franceza|Limba Romana|Matematica|Purtare|TIC")
Global $lstDisciplinecontext = GUICtrlCreateContextMenu($lstDiscipline)
Global $cmnuAdaugaDisciplina = GUICtrlCreateMenuItem("&Add discipline", $lstDisciplinecontext)
Global $cmnuStergeDisciplina = GUICtrlCreateMenuItem("&Delete discipline", $lstDisciplinecontext) ;add delete option

$cmnuStergeDisciplinaAdded = True
GUIRegisterMsg($WM_CONTEXTMENU, "_WM_CONTEXTMENU") ;registering context menu event handler

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
 $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cmnuAdaugaDisciplina
            AdaugaDisciplina()
        Case $cmnuStergeDisciplina
            StergeDisciplina()

    EndSwitch
WEnd
Func StergeDisciplina()
    Local $indice = _GUICtrlListBox_GetCurSel($lstDiscipline)
    Local $iMsgBoxAnswer = MsgBox(36, "Atentie!", "Esti sigur(a) ca vrei sa stergi?")
    Select
        Case $iMsgBoxAnswer = 6 ;Da
            _GUICtrlListBox_DeleteString($lstDiscipline, $indice)
        Case $iMsgBoxAnswer = 7 ;Nu
            Exit
    EndSelect
EndFunc   ;==>StergeDisciplina

Func AdaugaDisciplina()
    Local $pozitie = MouseGetPos()
    Local $AdaugaDisciplina = GUICreate("Adauga Disciplina", 340, 40, $pozitie[0] + 5, $pozitie[1] + 5, BitOR($WS_SYSMENU, $WS_POPUP, $WS_POPUPWINDOW, $WS_BORDER, $WS_CLIPSIBLINGS))
    Local $txtAdaugaDisciplina = GUICtrlCreateInput("", 8, 8, 217, 21)
    Local $btnAdaugaDisciplina = GUICtrlCreateButton("Adauga...", 232, 7, 97, 25, $WS_GROUP)
    WinSetTrans("Adauga Disciplina", "", 240)
    GUISetState(@SW_SHOW, $AdaugaDisciplina)
    Do
        Local $actiune = GUIGetMsg()
        If $actiune = $btnAdaugaDisciplina Then
            Local $listaDiscipline = ""
            For $i = 0 To _GUICtrlListBox_GetCount($lstDiscipline) -1
                $listaDiscipline &= _GUICtrlListBox_GetText($lstDiscipline, $i) & "|"
            Next
            Local $comparaLista = StringSplit($listaDiscipline,"|")
            If StringInStr(StringUpper($listaDiscipline), StringUpper(GUICtrlRead($txtAdaugaDisciplina))) Then
                MsgBox(48, "Situatii Statistice © 2010", "Disciplina " & StringUpper(GUICtrlRead($txtAdaugaDisciplina)) & " exista deja!")
            Else
                _GUICtrlListBox_AddString($lstDiscipline, StringUpper(GUICtrlRead($txtAdaugaDisciplina)))
            EndIf
            GUIDelete($AdaugaDisciplina)
        EndIf
    Until $actiune = $btnAdaugaDisciplina
EndFunc   ;==>AdaugaDisciplina

Func _WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    If $wParam <> GUICtrlGetHandle($lstDiscipline) Then Return $GUI_RUNDEFMSG ;we only going to handle context menu on list, all others carry on as is.
    Local $index = _GUICtrlListBox_GetCurSel($lstDiscipline)
    If $index <> -1 Then
        If Not $cmnuStergeDisciplinaAdded Then Global $cmnuStergeDisciplina = GUICtrlCreateMenuItem("&Delete discipline", $lstDisciplinecontext) ;add delete option
        $cmnuStergeDisciplinaAdded = True
    Else
        GUICtrlDelete($cmnuStergeDisciplina) ;remove "delete" option
        $cmnuStergeDisciplinaAdded = False
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_CONTEXTMENU
Edited by VAN0
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...