Modify

Opened 9 years ago

Closed 9 years ago

Last modified 9 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

Change History (6)

comment:1 Changed 9 years ago by Melba23

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 Changed 9 years ago by mLipok

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 Changed 9 years ago by Melba23

mLipok,

Looks fine to me.

M23

comment:4 Changed 9 years ago by guinness

  • Milestone set to 3.3.15.1
  • Owner set to guinness
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed by revision [11708] in version: 3.3.15.1

comment:5 Changed 9 years ago by guinness

Fixed by revision [11709] in version: 3.3.15.1

comment:6 Changed 9 years ago by guinness

  • Milestone 3.3.15.1 deleted

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

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.