Jump to content

Editing list view items?


Recommended Posts

Hey, in my program i would like the user to be able to edit the text in a list view item, by right clicking and pressing edit. I made a context menu for the list view so when i right click it, the edit option comes up, but i just need to figure out how to be able to change something inside like i would a excel cell. Incase im not clear ill take a screenshot of what i want my program to be able to do:

Posted Image

Edited by Smirk_zero
Link to comment
Share on other sites

I'm guessing this will be for your own AutoIt script:

#include <GuiConstants.au3>
#include <GuiListview.au3>

Global Const $DebugIt = 1

Global Const $WM_NOTIFY = 0x004E

Global Const $NM_FIRST = 0
Global Const $NM_CLICK = ($NM_FIRST - 2)
Global Const $NM_DBLCLK = ($NM_FIRST - 3)

Global $ListView = -999
Global $Gui, $editFlag
Global $editCtrl = "Edit1";name of the Edit control that pops up when edit a listitem....

_Main()

Func _Main()
    Local $dll
    Local $Gui = GUICreate("Double Click Demo", 417, 356, 192, 125)
    $ListView = GUICtrlCreateListView("Items", 10, 10, 300, 200, $LVS_EDITLABELS);Important Style!!!
    GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
    ; Populate list and make it wide enough to see
    For $i = 1 To 10
        GUICtrlCreateListViewItem("DoubleClick or press F2, then press Enter", $ListView)
    Next
    GUICtrlSendMsg($ListView, 0x101E, 0, -1);$listview, LVM_SETCOLUMNWIDTH, 0,  resize to widest value
    GUISetState(@SW_SHOW)
    ;Register WM_NOTIFY  events
    GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")
    $dll = DllOpen("user32.dll")
    While 1
        $msg = GUIGetMsg()
        _MonitorEditState($Gui, $editCtrl, $editFlag, $ListView, $dll)
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case Else
                ;;;;;;;
        EndSelect
    WEnd
    DllClose($dll)
EndFunc   ;==>_Main

Func ListView_Click()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("$NM_CLICK")
    ;----------------------------------------------------------------------------------------------
    If ControlCommand($Gui, "", $editCtrl, "IsVisible", "") Then
        CancelEdit($ListView)
        $editFlag = 0
    EndIf
EndFunc   ;==>ListView_Click

Func ListView_DoubleClick()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("$NM_DBLCLK")
    ;----------------------------------------------------------------------------------------------
    Rename($ListView)
EndFunc   ;==>ListView_DoubleClick

; WM_NOTIFY event handler
Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event
    $tagNMHDR = DllStructCreate("int;int;int", $lParam);NMHDR (hwndFrom, idFrom, code)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $ListView
            Select
                Case $event = $NM_CLICK
                    ListView_Click()
                Case $event = $NM_DBLCLK
                    ListView_DoubleClick()
                    Return 0
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_Notify_Events

Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")
    ConsoleWrite("!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint

Func _MonitorEditState(ByRef $h_gui, ByRef $editCtrl, ByRef $editFlag, ByRef $ListView, ByRef $dll)

    Local $pressed = _IsPressedMod($dll)
    If $editFlag And $pressed = 13 Then; pressed enter
        Update($h_gui, $editCtrl, $ListView)
    ElseIf $editFlag And $pressed = 27 Then; pressed esc
        CancelEdit($ListView)
        $editFlag = 0
    ElseIf Not $editFlag And $pressed = 113 Then; pressed f2
        Rename($ListView)
        $editFlag = 1
    EndIf
    Sleep(50)

    If ControlCommand($h_gui, "", $editCtrl, "IsVisible", "") Then
        If $editFlag = 0 Then
            $editFlag = 1
            Rename($ListView)
        EndIf
    Else
        $editFlag = 0
    EndIf

EndFunc   ;==>_MonitorEditState

Func Rename(ByRef $ListView)
    Local $itemIndex = _GUICtrlListViewGetCurSel($ListView)
    GUICtrlSendMsg($ListView, $LVM_EDITLABEL, $itemIndex, 0)
    HotKeySet("{Enter}", "Enter")
EndFunc   ;==>Rename

Func Enter()
    ; just a dummy function
EndFunc   ;==>Enter

Func Update(ByRef $h_gui, ByRef $editCtrl, ByRef $ListView)
    Local $newText = ControlGetText($h_gui, "", $editCtrl)
    Local $item = GUICtrlRead($ListView)
    GUICtrlSetData($item, $newText)
    HotKeySet("{Enter}")
    Send("{Enter}");quit edit mode
    $editFlag = 0
    $update = 0
EndFunc   ;==>Update

Func CancelEdit(ByRef $ListView)
    GUICtrlSendMsg($ListView, $LVM_CANCELEDITLABEL, 0, 0)
EndFunc   ;==>CancelEdit

Func _IsPressedMod($dll = "user32.dll")
    Local $aR, $bRv, $hexKey, $i
    For $i = 8 To 128
        $hexKey = '0x' & Hex($i, 2)
        $aR = DllCall($dll, "int", "GetAsyncKeyState", "int", $hexKey)
        If $aR[0] <> 0 Then Return $i
    Next
    Return 0
EndFunc   ;==>_IsPressedMod
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Is there a way of editing specific data for a listviewitem without using "|"? Just double click on some data and modify it?

Did you try the above post?

The above will only work on the 1st column.

If you need to work on other columns, might check with Holger.

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Yes I modified the code to have two columns and if you double click on a listview item on Column #2. The entire row is editable and you have to use "|" to change the data. Bascially how excel works. Just double click on a cell and you can edit just that cell instead of having to use the "|". For some reason when I quit out of the code the "scroll up" key was stuck.

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...