Page 1 of 1

GUI: ComboBox: update drop-down list  Topic is solved

Posted: 07 Jun 2018, 22:54
by jeeswg
I had difficulty finding information on how to update a ComboBox's drop-down list in AHK v2. I eventually figured it out and can thus present this working example:

Code: Select all

;AHK v2
;ComboBox (Edit control + drop-down list) example
;note: alt+down to display the drop-down list
#SingleInstance force
;oSearchHist := StrSplit("abcdefghijklmnopqrstuvwxyz0123456789")
oSearchHist := []
oGui := GuiCreate(, "MyWinTitle")
oGui.OnEvent("Close", "Gui_Close")
hGui := oGui.hWnd
oGui.SetFont("s18")
oCbx := oGui.Add("ComboBox", "R8", oSearchHist)
hCbx := oCbx.hWnd
oBtn := oGui.Add("Button", "Hidden Default", "OK")
oBtn.OnEvent("Click", "OnSearch")
oGui.Show()
return

;==================================================

;Proposed New GUI API for AutoHotkey v2 - Page 11 - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=2998&p=144660#p144660
;Added Ctrl.Add(ArrayOrList) and Ctrl.Delete([index]) for ListBox/ComboBox/DDL/Tab. Add works like GuiControl or Value:= in previous builds. Delete deletes one item or all items.

OnSearch()
{
	global hCbx, oCbx, oSearchHist
	;vNeedle := ControlGetText(, "ahk_id " hCbx)
	vNeedle := oCbx.Text
	if (vNeedle = "")
		return
	for vKey, vValue in oSearchHist
		if (vValue = vNeedle)
		{
			vKeyDel := vKey
			break
		}
	if !(vKeyDel = "")
		oSearchHist.RemoveAt(vKeyDel)
	oSearchHist.InsertAt(1, vNeedle)
	oCbx.Delete()
	oCbx.Add(oSearchHist)
	oCbx.Value := 1
}

Gui_Close()
{
	ExitApp()
}

Re: GUI: ComboBox: update drop-down list

Posted: 08 Jun 2018, 02:08
by Helgef
I had difficulty finding information on how to update a ComboBox's drop-down
Are you allergic to the documentation? You must be since you rather find your answer here,

Code: Select all

;Proposed New GUI API for AutoHotkey v2 - Page 11 - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=2998&p=144660#p144660
;Added Ctrl.Add(ArrayOrList) and Ctrl.Delete([index]) for ListBox/ComboBox/DDL/Tab. Add works like GuiControl or Value:= in previous builds. Delete deletes one item or all items.
than looking in the docs. If you want to use the GUI API, at least you can take one minute to read through the available methods and the their respective short descriptions, it is at the top of the page,
GuiControl Methods wrote:Add: Appends the specified entries at the current list of a ListBox, DropDownList, ComboBox, or Tab control.
Also see Add.

If there is a change in the future, I doubt lexikos will go back to his post from april 25 2017, and make a note for those who might go there instead of reading the docs.

Good luck.

Edit: This topic should be in ask for help.

Re: GUI: ComboBox: update drop-down list

Posted: 08 Jun 2018, 02:25
by jeeswg
- I looked multiple times through multiple pages. In the end I googled: AutoHotkey v2 ComboBox.
- I started here, ComboBox, which seemed like the obvious place to start, ironically, the clearer guidance was on other pages. The main text even implied it was text only, that you couldn't pass an array.
GUI Control Types
https://lexikos.github.io/v2/docs/comma ... m#ComboBox
- I didn't realise that these two pages were separated, also, one of them lacks a Related Pages list at the bottom:
GUI Control Types
https://lexikos.github.io/v2/docs/comma ... ntrols.htm
GuiControl Object
https://lexikos.github.io/v2/docs/objec ... ontrol.htm
- Incidentally, the two pages do not appear remotely close to each other on the sidebar listings, not that that needs to change.

Re: GUI: ComboBox: update drop-down list

Posted: 08 Jun 2018, 02:43
by Helgef
Seems like bad luck jeeswg :( .

About the code, I recommend you use a bound func or a closure instead of global variables.

Cheers.

Re: GUI: ComboBox: update drop-down list

Posted: 08 Jun 2018, 18:31
by kczx3
I tend to give names to my controls that I need to access from an event handler. Since you can access the GUI that a control belongs to from a GuiControl object.

Re: GUI: ComboBox: update drop-down list

Posted: 09 Jun 2018, 00:20
by Helgef
That is a good idea kczx3 :thumbup:.

Re: GUI: ComboBox: update drop-down list

Posted: 27 Jun 2018, 19:23
by jeeswg
- I have a problem that if 'abc' is in the list, but you type 'ABC', oCbx.Text returns 'abc'.
- This affects case-sensitive searching in this script:
Find dialog with whole word and RegEx support - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=50262
- Is there a way to get around this?

Re: GUI: ComboBox: update drop-down list

Posted: 28 Jun 2018, 01:36
by Helgef
Hello, I'm on the phone so it is a bit problematic to read and type. Anyways, it seems you didn't read the documentation, again.
guicontrol.text wrote:For a ComboBox, if there is no selected item, the text in the control's edit field is retrieved instead.
It could be clearer on the other page though.
Solution? Maybe,
guicontrol.value wrote:NewValue is the position of a single item/tab to select, or zero to clear the current selection
Cheers.

Re: GUI: ComboBox: update drop-down list

Posted: 28 Jun 2018, 04:07
by jeeswg
It looks like I already solved it, although any other comments are welcome. E.g. more native solutions. (I didn't find the answer in the documentation.)

Code: Select all

;this:
	vNeedle := ControlGetText(, "ahk_id " hCbx)
;not this:
	vNeedle := oCbx.Text
I tried to create a GUI control object for the Edit control, but it didn't work.

Code: Select all

hEdit := WinGetControlsHwnd("ahk_id " hCbx)[1] ;did work
;MsgBox(hEdit)
MsgBox(WinGetClass("ahk_id " hEdit)) ;Edit
oEdit := GuiCtrlFromHwnd("ahk_id " hEdit) ;didn't work
MsgBox(oEdit.hWnd) ;error

Re: GUI: ComboBox: update drop-down list

Posted: 28 Jun 2018, 13:15
by Helgef
Hello :wave: .
ControlGetText seems like the best option. I thought that you had abc selected when you did ctrl.text, with ABC in the edit, and that the solution would be to do ctrl.value := 0 to clear the selection and then ctrl.text would retrieve the text in the control's edit field, but that wasn't the case. It seems the text in the edit field is only retrieved exactly as is, if there is no selection and no match, case insensitive as it appears, I do not think it is very well described.

Cheers.

Re: GUI: ComboBox: update drop-down list | ComboBoxEx Class AHKv2

Posted: 03 Jul 2018, 23:43
by Flipeador
The methods and properties incorporated are very poor. I use this class: https://github.com/flipeador/Library-AutoHotkey/tree/master/gui/ComboBox.
Updated: 2019/03/17.

Re: GUI: ComboBox: update drop-down list

Posted: 04 Jul 2018, 00:38
by Helgef
Fancy icons :thumbup:

Cheers.