Modify

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#3233 closed Bug (Fixed)

_GUICtrlListBox_GetSelCount returns 0 also if $hwnd isn't a handle

Reported by: autoBert Owned by: guinness
Milestone: Component: AutoIt
Version: 3.3.14.0 Severity: None
Keywords: Cc:

Description

_GUICtrlListBox_GetSelCount returns 0 if no items selected. Helpfile say: Failure: -1 if no items are selected.
also if $hwnd isn't a handle 0 is returned.

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
	Local $idListBox

	; Create GUI
	GUICreate("List Box Get Sel Count", 400, 296)
	$idListBox = GUICtrlCreateList("", 2, 2, 396, 296, BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL))
	GUISetState(@SW_SHOW)

	; Add strings
	_GUICtrlListBox_BeginUpdate($idListBox)
	For $iI = 1 To 9
		_GUICtrlListBox_AddString($idListBox, StringFormat("%03d : Random string", Random(1, 100, 1)))
	Next
	_GUICtrlListBox_EndUpdate($idListBox)
	;Should show -1 but show 0
	;Helpfile: Failure: -1 if no items are selected.
	MsgBox($MB_SYSTEMMODAL, "Information (no Item selected)", "Items Selected: " & _GUICtrlListBox_GetSelCount($idListBox))

	; Select a few items
	_GUICtrlListBox_SetSel($idListBox, 3)
	_GUICtrlListBox_SetSel($idListBox, 4)
	_GUICtrlListBox_SetSel($idListBox, 5)

	;Should show -1 but show 0
	;Helpfile: Failure: -1 if no items are selected.
	;note the func is called with no hwnd so maybe in future use @error and @extended
	MsgBox($MB_SYSTEMMODAL, "Information (no HWND)", "Items Selected: " & _GUICtrlListBox_GetSelCount('1234'))

	; Show the item selection count
	MsgBox($MB_SYSTEMMODAL, "Information (that's correct)", "Items Selected: " & _GUICtrlListBox_GetSelCount($idListBox))

	; Loop until the user exits.
	Do
	Until GUIGetMsg() = $GUI_EVENT_CLOSE
	GUIDelete()
EndFunc   ;==>Example

Attachments (0)

Change History (6)

comment:1 by Melba23, 10 years ago

How about if we change the Help file to read:

Failure:
-1 if Control ID/Handle invalid

and then use this modified function:

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
	Local $idListBox

	; Create GUI
	GUICreate("List Box Get Sel Count", 400, 296)
	$idListBox = GUICtrlCreateList("", 2, 2, 396, 296, BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL))
	GUISetState(@SW_SHOW)

	; Add strings
	_GUICtrlListBox_BeginUpdate($idListBox)
	For $iI = 1 To 9
		_GUICtrlListBox_AddString($idListBox, StringFormat("%03d : Random string", Random(1, 100, 1)))
	Next
	_GUICtrlListBox_EndUpdate($idListBox)
	MsgBox($MB_SYSTEMMODAL, "No Sel", "Items Selected: " & _GUICtrlListBox_GetSelCount_Mod($idListBox))

	; Select a few items
	_GUICtrlListBox_SetSel($idListBox, 3)
	_GUICtrlListBox_SetSel($idListBox, 4)
	_GUICtrlListBox_SetSel($idListBox, 5)

	MsgBox($MB_SYSTEMMODAL, "No HWND", "Items Selected: " & _GUICtrlListBox_GetSelCount_Mod("1234"))

	; Show the item selection count
	MsgBox($MB_SYSTEMMODAL, "OK", "Items Selected: " & _GUICtrlListBox_GetSelCount_Mod($idListBox))

	; Loop until the user exits.
	Do
	Until GUIGetMsg() = $GUI_EVENT_CLOSE
	GUIDelete()
EndFunc   ;==>Example

Func _GUICtrlListBox_GetSelCount_Mod($hWnd)
	If IsHWnd($hWnd) Then
		Return _SendMessage($hWnd, $LB_GETSELCOUNT)
	Else
		If IsHwnd(GUICtrlGetHandle($hWnd)) Then
			Return GUICtrlSendMsg($hWnd, $LB_GETSELCOUNT, 0, 0)
		EndIf
		Return -1 ; Invalid controlID
	EndIf
EndFunc   ;==>_GUICtrlListBox_GetSelCount_Mod

M23

comment:2 by mLipok, 10 years ago

Small proposal:

Func _GUICtrlListBox_GetSelCount_Mod($hWnd)
	If IsHWnd($hWnd) Then
		Return _SendMessage($hWnd, $LB_GETSELCOUNT)
	ElseIf IsHwnd(GUICtrlGetHandle($hWnd)) Then
		Return GUICtrlSendMsg($hWnd, $LB_GETSELCOUNT, 0, 0)
	EndIf
	Return -1 ; Invalid controlID
EndFunc   ;==>_GUICtrlListBox_GetSelCount_Mod

comment:3 by Melba23, 10 years ago

mLipok,

Looks fine to me.

M23

comment:4 by guinness, 10 years ago

Milestone: 3.3.15.1
Owner: set to guinness
Resolution: Fixed
Status: newclosed

Fixed by revision [11708] in version: 3.3.15.1

comment:5 by guinness, 10 years ago

Fixed by revision [11709] in version: 3.3.15.1

comment:6 by guinness, 10 years ago

Milestone: 3.3.15.1

I commited with a slight variation of mLipok's code suggestion

Modify Ticket

Action
as closed The owner will remain guinness.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.