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

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

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

07 Jun 2018, 22:54

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()
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: GUI: ComboBox: update drop-down list

08 Jun 2018, 02:08

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.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GUI: ComboBox: update drop-down list

08 Jun 2018, 02:25

- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: GUI: ComboBox: update drop-down list

08 Jun 2018, 02:43

Seems like bad luck jeeswg :( .

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

Cheers.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: GUI: ComboBox: update drop-down list

08 Jun 2018, 18:31

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.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: GUI: ComboBox: update drop-down list

09 Jun 2018, 00:20

That is a good idea kczx3 :thumbup:.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GUI: ComboBox: update drop-down list

27 Jun 2018, 19:23

- 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?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: GUI: ComboBox: update drop-down list

28 Jun 2018, 01:36

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.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GUI: ComboBox: update drop-down list

28 Jun 2018, 04:07

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: GUI: ComboBox: update drop-down list

28 Jun 2018, 13:15

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.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

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

03 Jul 2018, 23:43

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.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: a_bolog, Descolada, gero, kunkel321 and 33 guests