help with WM_COMMAND and ComboBox text issue

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

help with WM_COMMAND and ComboBox text issue

Post by RaptorX » 04 Oct 2022, 14:58

Hello guys,

When capturing this particular message I have noticed that the combo box control some how has the previous value for .Text.
Is this a bug or am I misunderstanding something about what I'm expecting (expecting cb.text to contain the current value of the control)+?

Code: Select all

onmessage(0x0111, handleMSG)

main := gui()
main.add("combobox", "vSearch w200", ["one", "two", "three"])
main.show()
return

handleMSG(wparam, lParam, msg, hwnd)
{
	static CBN_SELCHANGE := 1
	static cb := main["search"]
	
	if (wParam & 0xFFFF0000) >> 16 = CBN_SELCHANGE
		tooltip  "CBN_SELCHANGE: " cb.text
	else
		tooltip
}
The reason why im using this instead of onEvent("Cange") is because Im actually targeting a different message that is not handled natively: CBN_CLOSEUP and it was triggering this other message so i was about to handle it inside the function but some how i get these unexpected values.

Any information as to what is going on? are my expectations wrong?
Projects:
AHK-ToolKit

iPhilip
Posts: 801
Joined: 02 Oct 2013, 12:21

Re: help with WM_COMMAND and ComboBox text issue

Post by iPhilip » 05 Oct 2022, 02:26

It looks like the OnMessage event happens before the ComboBox control changes its value while the OnEvent("Change", ...) function gets called after the control changes its value. Below is a slightly modified version of your code to demonstrate the timing:

Code: Select all

CoordMode "ToolTip", "Window"
onmessage(0x0111, handleMSG)

main := gui()
cb := main.add("combobox", "vSearch w200", ["one", "two", "three"])
cb.OnEvent("Change", ComboBox_Change)
main.show()
return

handleMSG(wparam, lParam, msg, hwnd)
{
	static CBN_SELCHANGE := 1
	static cb := main["search"]
	
	if (wParam & 0xFFFF0000) >> 16 = CBN_SELCHANGE
		tooltip  "CBN_SELCHANGE: " cb.text " (" A_TickCount ")"
	else
		tooltip
}

ComboBox_Change(GuiCtrlObj, Info)
{
   tooltip GuiCtrlObj.text " (" A_TickCount ")", 0, 0, 2
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: help with WM_COMMAND and ComboBox text issue

Post by just me » 05 Oct 2022, 04:14

The contents of the .Text property is updated when AHK's window procedure processes the CBN_SELCHANGE notification. If you intercept the notification, you get the previous property value.

Code: Select all

CoordMode "ToolTip", "Window"
onmessage(0x0111, handleMSG)

main := gui()
cb := main.add("combobox", "vSearch w200", ["one", "two", "three"])
main.show()
return

handleMSG(wparam, lParam, msg, hwnd)
{
	static CBN_SELCHANGE := 1
	static cb := main["search"]

	if (wParam & 0xFFFF0000) >> 16 = CBN_SELCHANGE {
		Selected := ""
		VarSetStrCapacity(&Selected, 64)
		Index := SendMessage(0x0147, 0, 0, lParam) ; CB_GETCURSEL
		SendMessage(0x0148, Index, StrPtr(Selected), lParam) ; CB_GETLBTEXT
		VarSetStrCapacity(&Selected, -1)
		tooltip  "CBN_SELCHANGE:`nSelected item: " Selected "`nCB.Text: " cb.text
	}
	else
		tooltip
}

iPhilip
Posts: 801
Joined: 02 Oct 2013, 12:21

Re: help with WM_COMMAND and ComboBox text issue

Post by iPhilip » 05 Oct 2022, 10:40

Thank you, @just me. Note that the handleMSG() function can also be written using a Buffer object as follows:

Code: Select all

handleMSG(wparam, lParam, msg, hwnd)
{
	static CBN_SELCHANGE := 1
	static cb := main["search"]

	if (wParam & 0xFFFF0000) >> 16 = CBN_SELCHANGE {
		Selected := Buffer(64)
		Index := SendMessage(0x0147, 0, 0, lParam) ; CB_GETCURSEL
		SendMessage(0x0148, Index, Selected, lParam) ; CB_GETLBTEXT
		tooltip  "CBN_SELCHANGE:`nSelected item: " StrGet(Selected) "`nCB.Text: " cb.text
	}
	else
		tooltip
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Post Reply

Return to “Ask for Help (v2)”