Jump to content

Problem with GuiCtrlList


Fink89
 Share

Recommended Posts

Hi,

I have problem with GuiCtrlList. I use GUICtrlCreateList and GUICtrlSetData with 10 row. When I select 1 row and click button  , It will run func exp(). 

I want when I use keyboard up or down, It will run func exp() with that's row, without click button. 

Link to comment
Share on other sites

#include <guiconstantsex.au3>;
#include <GUIConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <_httprequest.au3>
#include <GuiListView.au3>
#include <Array.au3>
Global $aSorting[3]
 Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
 Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
 GUICreate ("Check Tokens Address Myetherwallet" ,800 ,600 ,150 ,50)
   $address=GUICtrlCreateLabel ("List Address:" , 10 , 20 , 100 , 20)
   $input=GUICtrlCreateButton ("Choose File" ,5,40 ,70 ,25)
   $run=GUICtrlCreateButton ("Check" ,405,40 ,70 ,25)
   GUICtrlSetState($run, $GUI_FOCUS)
   $mylist=GUICtrlCreateList ("" ,100 , 0 , 300 ,600)   
   $myview=GUICtrlCreateListView("Tokens Name|Tokens Balance|Tokens Symbol" ,480 , 0 , 300 ,600,-1,$iExWindowStyle)
   $cButton_0 = GUICtrlCreateButton("Sort Name", 405, 80, 70, 25)
   $cButton_1 = GUICtrlCreateButton("Sort Balance", 405, 120, 70, 25)
   $up = GUICtrlCreateButton("Up", 405, 160, 70, 25)
   $down = GUICtrlCreateButton("Down", 405, 200, 70, 25)
   _GUICtrlListView_SetColumnWidth($myview, 0, 100)
   _GUICtrlListView_SetColumnWidth($myview, 1, $LVSCW_AUTOSIZE_USEHEADER)
   _GUICtrlListView_SetExtendedListViewStyle($myview, $iExListViewStyle)
    Local $aAccelKeys[2][2] = [["{UP}", $run], ["{Down}", $run]]
    GUISetAccelerators($aAccelKeys)
   GUISetState(@SW_SHOW)


 While 1
   Switch GUIGetMsg()

   Case $GUI_EVENT_CLOSE
        ExitLoop

   Case $input
        $var = FileOpenDialog('Open File', @ScriptDir & "\", "Text (*.txt)", 1 + 4 )
        Global $line=_FileCountLines($var)
      For $x = 1 to _FileCountLines($var)
      $a = FileReadLine($var, $x)
    $mew=GUICtrlSetData ($mylist ,$a) 
      Next

   Case $run
   _GUICtrlListView_DeleteAllItems($myview)
   $b=GUICtrlRead($mylist, $GUI_READ_EXTENDED)
   $search = 'https://api.ethplorer.io/getAddressInfo/' & $b & '?apiKey=freekey'
   $rq1 = _HttpRequest(4, $search)
   $name=StringRegExp($rq1[1], 'name":"(.*?)"', 3)
    $row=ubound($name,1)-1
   $sym=StringRegExp($rq1[1], 'symbol":"(.*?)"', 3)
   $balance1=StringRegExp($rq1[1], 'balance":(.*?),', 3)
   $u=ubound($balance1,1)-1
   $decimal=StringRegExp($rq1[1], 'decimals":(.*?),', 3)
      Select
      Case $u = 0
      GUICtrlCreateListViewItem('No Tokens Balance', $myview)
      Case Else
      For $i=0 to $row
      $item=GUICtrlCreateListViewItem('name|balance|symbol', $myview)
         Select
         Case $decimal[$i]= '0'
         $balance= $balance1[$i+1]
         Case $decimal[$i]= '"1"'
         $balance= $balance1[$i+1] * 0.1
         Case $decimal[$i]= '"2"'
         $balance= $balance1[$i+1] * 0.01
         Case $decimal[$i]= '"3"'
         $balance= $balance1[$i+1] * 0.001
         Case $decimal[$i]= '3'
         $balance= $balance1[$i+1] * 0.001
         Case $decimal[$i]= '"4"'
         $balance= $balance1[$i+1] * 0.0001
         Case $decimal[$i]= '"5"'
         $balance= $balance1[$i+1] * 0.00001
         Case $decimal[$i]= '"6"'
         $balance= $balance1[$i+1] * 0.000001
         Case $decimal[$i]= '"7"'
         $balance= $balance1[$i+1] * 0.0000001
         Case $decimal[$i]= '"8"'
         $balance= $balance1[$i+1] * 0.00000001
         Case $decimal[$i]= '"9"'
         $balance= $balance1[$i+1] * 0.000000001
         Case $decimal[$i]= '"10"'
         $balance= $balance1[$i+1] * 0.0000000001
         Case $decimal[$i]= '"11"'
         $balance= $balance1[$i+1] * 0.00000000001
         Case $decimal[$i]= '"12"'
         $balance2=stringsplit($balance1[$i+1],'e+')
         $balance=$balance2[1] * 10^ ($balance2[3]-12)
         Case $decimal[$i]= '"16"'
         $balance2=stringsplit($balance1[$i+1],'e+')
         $balance=$balance2[1] * 10^ ($balance2[3]-16)
         Case $decimal[$i]= '"18"'
         $balance2=stringsplit($balance1[$i+1],'e+')
         $balance=$balance2[1] * 10^ ($balance2[3]-18)
         Case $decimal[$i]= '18'
         $balance2=stringsplit($balance1[$i+1],'e+')
         $balance=$balance2[1] * 10^ ($balance2[3]-18)
         Case Else
         MsgBox($MB_SYSTEMMODAL, "Error", "Name token error: " & " " & $name[$i])
         EndSelect
         GUICtrlSetData($item, $name[$i])
         GUICtrlSetData($item, '||' & $sym[$i])
         GUICtrlSetData($item, '|' & $balance)
      Next
   EndSelect
      Case $cButton_0
            _GUICtrlListView_SimpleSort($myview, $aSorting, 0)
      Case $cButton_1
            _GUICtrlListView_SimpleSort($myview, $aSorting, 1)
EndSwitch
   WEnd

I use GUISetAccelerators but It run func button , don't up or down row.

Link to comment
Share on other sites

Here is your code changed to example how to achieve invoking of function when selection of ListView changes (set text in StatusBar):

You can accomodate this example also for another controls in GUI (List instead of ListView?)

Note: Keyboard shortcuts Up/Down works too when ListView has focus

#include <guiconstantsex.au3>;
#include <GUIConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
;~ #include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
;~ #include <_httprequest.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <Array.au3>

Global $disable_refresh_status = False
Global $aSorting[3]
Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)

$Form1 = GUICreate("Check Tokens Address Myetherwallet", 800, 600, 150, 50)
$address = GUICtrlCreateLabel("List Address:", 10, 20, 100, 20)
$input = GUICtrlCreateButton("Choose File", 5, 40, 70, 25)
$run = GUICtrlCreateButton("Check", 405, 40, 70, 25)
GUICtrlSetState($run, $GUI_FOCUS)
$mylist = GUICtrlCreateList("", 100, 0, 300, 600)
$myview = GUICtrlCreateListView("Tokens Name|Tokens Balance|Tokens Symbol", 480, 0, 300, 600, -1, $iExWindowStyle)
$cButton_0 = GUICtrlCreateButton("Sort Name", 405, 80, 70, 25)
$cButton_1 = GUICtrlCreateButton("Sort Balance", 405, 120, 70, 25)
$up = GUICtrlCreateButton("Up", 405, 160, 70, 25)
$down = GUICtrlCreateButton("Down", 405, 200, 70, 25)
_GUICtrlListView_SetColumnWidth($myview, 0, 100)
_GUICtrlListView_SetColumnWidth($myview, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetExtendedListViewStyle($myview, $iExListViewStyle)
;~ Local $aAccelKeys[2][2] = [["{UP}", $run],["{Down}", $run]]
;~ GUISetAccelerators($aAccelKeys)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

$disable_refresh_status = True
For $i = 0 To 10
    GUICtrlCreateListViewItem('name '&$i&'|balance '&$i&'|symbol '&$i&'', $myview)
Next
$disable_refresh_status = False

While 1
    Switch GUIGetMsg()

        Case $GUI_EVENT_CLOSE
            ExitLoop

        Case $cButton_0
            _GUICtrlListView_SimpleSort($myview, $aSorting, 0)
        Case $cButton_1
            _GUICtrlListView_SimpleSort($myview, $aSorting, 1)
    EndSwitch
WEnd

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event

    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    $event = DllStructGetData($tagNMHDR, 3)

    ; when $disable_refresh_status = True don't invoke --> on mass changes in ListView
    ; to not it be slow and don't paint in statusbar change of each row in such situations
    If $hWndGUI = $Form1 And $wParam = $myview  And $disable_refresh_status = False Then
        If $event = $LVN_ITEMCHANGED Then
            $NMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
            If BitAND(DllStructGetData($NMLISTVIEW, "Changed"), $LVIF_STATE) = $LVIF_STATE And _
                DllStructGetData($NMLISTVIEW, "NewState") <> DllStructGetData($NMLISTVIEW, "OldState") Then RefreshStatusBar()
        EndIf
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc

; invoked everytime when selection changes
Func RefreshStatusBar()
    $i = _GUICtrlListView_GetNextItem($myview)
    $name = _GUICtrlListView_GetItemText($myview, $i)
    _GUICtrlStatusBar_SetText($StatusBar1, 'Selected item: index=' & $i & ' name=' & $name)
EndFunc

 

Edited by Zedna
Link to comment
Share on other sites

Here it's accomodated to ListBox, when selection changes function is invoked (here StatusBar text is refreshed):

Note: Keyboard shortcuts Up/Down works too when ListBox has focus

#include <guiconstantsex.au3>;
#include <GUIConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
;~ #include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
;~ #include <_httprequest.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <Array.au3>

Global $aSorting[3]
Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)

$Form1 = GUICreate("Check Tokens Address Myetherwallet", 800, 600, 150, 50)
$address = GUICtrlCreateLabel("List Address:", 10, 20, 100, 20)
$input = GUICtrlCreateButton("Choose File", 5, 40, 70, 25)
$run = GUICtrlCreateButton("Check", 405, 40, 70, 25)
GUICtrlSetState($run, $GUI_FOCUS)
$mylist = GUICtrlCreateList("", 100, 0, 300, 580)
$hListBox = GUICtrlGetHandle($mylist)
$myview = GUICtrlCreateListView("Tokens Name|Tokens Balance|Tokens Symbol", 480, 0, 300, 600, -1, $iExWindowStyle)
$cButton_0 = GUICtrlCreateButton("Sort Name", 405, 80, 70, 25)
$cButton_1 = GUICtrlCreateButton("Sort Balance", 405, 120, 70, 25)
$up = GUICtrlCreateButton("Up", 405, 160, 70, 25)
$down = GUICtrlCreateButton("Down", 405, 200, 70, 25)
_GUICtrlListView_SetColumnWidth($myview, 0, 100)
_GUICtrlListView_SetColumnWidth($myview, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetExtendedListViewStyle($myview, $iExListViewStyle)
;~ Local $aAccelKeys[2][2] = [["{UP}", $run],["{Down}", $run]]
;~ GUISetAccelerators($aAccelKeys)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

For $i = 0 To 10
    GUICtrlSetData($mylist, 'Item '&StringFormat('%02i', $i))
Next

While 1
    Switch GUIGetMsg()

        Case $GUI_EVENT_CLOSE
            ExitLoop

        Case $cButton_0
            _GUICtrlListView_SimpleSort($myview, $aSorting, 0)
        Case $cButton_1
            _GUICtrlListView_SimpleSort($myview, $aSorting, 1)
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $msg, $wparam, $lparam)
    #forceref $hWnd, $msg, $wparam, $lparam

    Local $i_command_code = BitAND($wparam, 0xFFFF)
    Local $i_command_notify = BitShift($wparam, 16)

    Switch $lparam
        Case $hListBox
            Switch $i_command_notify
                Case $LBN_SELCHANGE
                    RefreshStatusBar()
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

; invoked everytime when selection changes
Func RefreshStatusBar()
    $text = GUICtrlRead($mylist)
    _GUICtrlStatusBar_SetText($StatusBar1, 'ListBox, Selected item text: ' & $text)
EndFunc

 

Edited by Zedna
typo
Link to comment
Share on other sites

And here is funcionality of both previous scripts joined together to only one script

show catching of selection change in ListBox+ListView at the same time:

; https://www.autoitscript.com/forum/topic/193573-problem-with-guictrllist/

#include <guiconstantsex.au3>;
#include <GUIConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
;~ #include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
;~ #include <_httprequest.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <Array.au3>

Global $disable_refresh_status = False
;~ Global $aSorting[3]
Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)

$Form1 = GUICreate("Check Tokens Address Myetherwallet", 800, 600, 150, 50)
$address = GUICtrlCreateLabel("List Address:", 10, 20, 100, 20)
$input = GUICtrlCreateButton("Choose File", 5, 40, 70, 25)
$run = GUICtrlCreateButton("Check", 405, 40, 70, 25)
GUICtrlSetState($run, $GUI_FOCUS)
$mylist = GUICtrlCreateList("", 100, 0, 300, 580)
$hListBox = GUICtrlGetHandle($mylist)
$myview = GUICtrlCreateListView("Tokens Name|Tokens Balance|Tokens Symbol", 480, 0, 300, 580, -1, $iExWindowStyle)
$cButton_0 = GUICtrlCreateButton("Sort Name", 405, 80, 70, 25)
$cButton_1 = GUICtrlCreateButton("Sort Balance", 405, 120, 70, 25)
$up = GUICtrlCreateButton("Up", 405, 160, 70, 25)
$down = GUICtrlCreateButton("Down", 405, 200, 70, 25)
_GUICtrlListView_SetColumnWidth($myview, 0, 100)
_GUICtrlListView_SetColumnWidth($myview, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetExtendedListViewStyle($myview, $iExListViewStyle)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

For $i = 0 To 10
    GUICtrlSetData($mylist, 'Item '&StringFormat('%02i', $i))
Next

$disable_refresh_status = True
For $i = 0 To 10
    GUICtrlCreateListViewItem('name '&$i&'|balance '&$i&'|symbol '&$i&'', $myview)
Next
$disable_refresh_status = False

While 1
    Switch GUIGetMsg()

        Case $GUI_EVENT_CLOSE
            ExitLoop

;~      Case $cButton_0
;~          _GUICtrlListView_SimpleSort($myview, $aSorting, 0)
;~      Case $cButton_1
;~          _GUICtrlListView_SimpleSort($myview, $aSorting, 1)
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $msg, $wparam, $lparam)
    #forceref $hWnd, $msg, $wparam, $lparam

    Local $i_command_code = BitAND($wparam, 0xFFFF)
    Local $i_command_notify = BitShift($wparam, 16)

    Switch $lparam
        Case $hListBox
            Switch $i_command_notify
                Case $LBN_SELCHANGE
                    RefreshStatusBar('LB')
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event

    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    $event = DllStructGetData($tagNMHDR, 3)

    ; when $disable_refresh_status = True don't invoke --> on mass changes in ListView
    ; to not it be slow and don't paint in statusbar change of each row in such situations
    If $hWndGUI = $Form1 And $wParam = $myview  And $disable_refresh_status = False Then
        If $event = $LVN_ITEMCHANGED Then
            $NMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
            If BitAND(DllStructGetData($NMLISTVIEW, "Changed"), $LVIF_STATE) = $LVIF_STATE And _
                DllStructGetData($NMLISTVIEW, "NewState") <> DllStructGetData($NMLISTVIEW, "OldState") Then RefreshStatusBar('LV')
        EndIf
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc

; invoked everytime when selection changes
Func RefreshStatusBar($what)
    If $what == 'LB' Then ; ListBox
        $text = GUICtrlRead($mylist)
        _GUICtrlStatusBar_SetText($StatusBar1, 'ListBox, Selected item text: ' & $text)
    EndIf

    If $what == 'LV' Then ; ListView
        $i = _GUICtrlListView_GetNextItem($myview)
        $name = _GUICtrlListView_GetItemText($myview, $i)
        _GUICtrlStatusBar_SetText($StatusBar1, 'ListView, Selected item: index=' & $i & ' name=' & $name)
    EndIf
EndFunc

 

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