Jump to content

Button enabled only when a ListViewItem is selected


 Share

Go to solution Solved by TheDcoder,

Recommended Posts

Hello, Please look at this GUI:

#include-once

; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <GuiListView.au3>

Global $test = GUICreate("test",283,120,-1,-1,-1,-1)
GUISetBkColor(0xFFFFFF,$test)
$hListMyList = GUICtrlCreatelistview("My List",7,4,269,65,-1,512)
$hButtonAlwaysEnabled = GUICtrlCreateButton("Always Enabled",20,80,100,30,-1,-1)
$hButtonEnabled = GUICtrlCreateButton("Enabled",160,80,100,30,-1,-1)
$hListViewItemMyItem = GUICtrlCreateListViewItem("My List Item", $hListMyList)
GUISetState()

While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        
    EndSwitch
WEnd

I want $hButtonEnabled disabled when a ListViewItem is not selected. I tried inserting a custom function in the while loop but it didn't work that well (button was flickering & high memory usage)

I think its not possible to do in AutoIt AutoIt is awsome!

If you know to do such thing, please post it here :), TD

P.S Will be greatly appreciated cause its impossible for me to do :)

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Hello again, I found out the solution myself I suck!!!, This solution adds flicker effect to your buttons (Only visible when your mouse is above your button)

Add the following to your script:

AdlibRegister("ButtonCheck") ; Use this to check ListView

Func ButtonCheck()
    If GUICtrlRead($hListMyList) = 0 Then
        GUICtrlSetState($hYourButton, 128) ; 128 = $GUI_DISABLE
                ; Any extra buttons to check here
    Else
        GUICtrlSetState($hYourButton, 64) ; 64 = $GUI_ENABLE
                ; Any extra buttons to check here
    EndIf
EndFunc

This is the working version of my script:

#include-once

AdlibRegister("ButtonCheck")

; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <GuiListView.au3>

Global $test = GUICreate("test",283,120,-1,-1,-1,-1)
GUISetBkColor(0xFFFFFF,$test)
$hListMyList = GUICtrlCreatelistview("My List",7,4,269,65,-1,512)
$hButtonAlwaysEnabled = GUICtrlCreateButton("Always Enabled",20,80,100,30,-1,-1)
$hButtonEnabled = GUICtrlCreateButton("Enabled",160,80,100,30,-1,-1)
$hListViewItemMyItem = GUICtrlCreateListViewItem("My List Item", $hListMyList)
GUISetState()

While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        
    EndSwitch
WEnd

Func ButtonCheck()
    If GUICtrlRead($hListMyList) = 0 Then
        GUICtrlSetState($hButtonEnabled, 128) ; 128 = $GUI_DISABLE
    Else
        GUICtrlSetState($hButtonEnabled, 64) ; 64 = $GUI_ENABLE
    EndIf
EndFunc

May it help you, TD :)

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

you might want to consider WM_NOTIFY (i'm using it to detect listview clicks then call a function accordingly..

or in this example when i click on a listview it updates input boxes on different tabs but enabling or disabling a button would be the same logic

; Handle WM_NOTIFY messages
; used to get listview clicks on the fly
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo, $sClickType
    $hListView = $GUI_BackupQueueListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $sClickType = "Left Click"
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    $sClickType = "Left Double-Click"
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the right mouse button
                    $sClickType = "Right Click"
                Case $NM_RDBLCLK ; Sent by a list-view control when the user double-clicks an item with the right mouse button
                    $sClickType = "Right Double-Click"
                Case Else ; We aren't interested in other messages
                    Return $GUI_RUNDEFMSG
            EndSwitch
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
            ConsoleWriteGUI("WM_NOTIFY: " & $sClickType & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                    "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                    "-->Code:" & @TAB & $iCode & @LF & _
                    "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
                    "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF)

            ; Get ListView item by index.... [1] ID... [2] File Name... [3] Path...
            Global $selectedItem = _GUICtrlListView_GetItemTextArray($GUI_BackupQueueListView, DllStructGetData($tInfo, "Index"))

            GUICtrlSetData($GUI_Action_SourceInput, $selectedItem[1] & " - " & $selectedItem[2])
            GUICtrlSetData($GUI_Schedule_SourceInput, $selectedItem[1] & " - " & $selectedItem[2])


    EndSwitch
    Return $GUI_RUNDEFMSG
 EndFunc   ;==>WM_NOTIFY

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

TheDcoder,

The state of the button is both Enabled and Shown so guictrlgetstate returns $GUI_ENABLE (64) + $GUI_SHOW (16).  Same principle applies to disabled.  You only want to change the state of the button when you have to or it will flicker.

See below...

#include-once

; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <GuiListView.au3>

Global $test = GUICreate("test", 283, 120, -1, -1, -1, -1)
GUISetBkColor(0xFFFFFF, $test)
$hListMyList = GUICtrlCreateListView("My List", 7, 4, 269, 65, -1, 512)
$hButtonAlwaysEnabled = GUICtrlCreateButton("Always Enabled", 20, 80, 100, 30, -1, -1)
$hButtonEnabled = GUICtrlCreateButton("Enabled", 160, 80, 100, 30, -1, -1)
$hListViewItemMyItem = GUICtrlCreateListViewItem("My List Item", $hListMyList)
GUISetState()

While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch

    If GUICtrlRead($hListMyList) = 0 And GUICtrlGetState($hButtonEnabled) = 80 Then GUICtrlSetState($hButtonEnabled, 128)
    If GUICtrlRead($hListMyList) <> 0 And GUICtrlGetState($hButtonEnabled) = 144 Then GUICtrlSetState($hButtonEnabled, 64)

WEnd

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Solution

@kylomas You saved my GUI, Thanks :)

Solution:

I enhanced kylomas's code and made this function:

Func ListCheck()
    Switch GUICtrlRead($hListYourList) ; Your List
        Case 0
            Switch GUICtrlGetState($hYourButton) ; Your button to check
                Case 80
                    GUICtrlSetState($hButtonEnabled, 128)
                                        ; Add any buttons here if you have multiple buttons

                Case Else
                Return

            EndSwitch

        Case Else
            Switch GUICtrlGetState($hYourButton) ; Your button to check
                Case 144
                    GUICtrlSetState($hYourButton, 64)
                                        ; Add any buttons here if you have multiple buttons

                Case Else
                    Return

            EndSwitch

    EndSwitch
EndFunc

Use the above function in your GUI's while loop

May it help you, TD :thumbsup:

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

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