Unexpected Behaviour, simple script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Yogi
Posts: 12
Joined: 06 Jul 2022, 23:03

Unexpected Behaviour, simple script

21 Aug 2022, 06:03

Hello. I have this reasonably simple script that's been giving me a headache for a few days. Not because it doesn't work ... but because it works!

There's two aspects to it for the most part. A hotkey that will grab the Window Title from the active window (if the first word is between 0 - 15 characters, and it's not already in the object).
It then makes a button with that that title. The button doesn't do anything, it's not relevant for this. But the hotkey will also add the text of the button to a DropDown (so you can delete the button - and "repopulate" the buttons using the object).

The label looks at what was selected from the dropdown list, deletes it from the object, clears the buttons list and recreates it.

That's it.

Code: Select all

WatchList_OBJ := {}
gosub, RepopulateButtons


Numpad1::
clipboard := ""
RegexActiveTitle1 := ""
WinGetTitle, ActiveWindowTitle, A
RegexMatch(ActiveWindowTitle, "(.+?)\s", RegexActiveTitle)
WatchList_Count := WatchList_OBJ.Count()

If (StrLen(RegexActiveTitle1) < 15) && (StrLen(RegexActiveTitle1) > 0) && (WatchList_Count < 14) && (WatchList_OBJ[RegexActiveTitle1] = "")
{
	Proceed := 1
	Try
	{
		Gui, WatchlistGUI:Add, Button, cBlack x+25 w85 gSendToTV v%RegexActiveTitle1% -Default +Hwnd%RegexActiveTitle1%, %RegexActiveTitle1%	;	this has to be above adding it to an object so that if it errors on creating the button due to duplicate Hwnds, it skips adding it to the object.
	}
	catch
	{
		Proceed := 0
		soundbeep, 100, 200
	}
	
	If Proceed = 1
	{
		WatchList_OBJ[RegexActiveTitle1] := RegexActiveTitle1
		Msgbox % ButtonList
		ButtonList .= RegexActiveTitle1 "|"	
		msgbox % ButtonList 						
		GuiControl, WatchlistGUI:, DeleteButtonDropDownVar, %ButtonList%
		msgbox % RegexActiveTitle1 		
	}
}
Else
{
	If (WatchList_OBJ[RegexActiveTitle1] != "")
	{
		soundbeep, 330, 200
		soundbeep, 200, 200
	}
	Else
		soundbeep, 100, 200
}
return


RepopulateButtons:
Gui, Submit
Gui, WatchlistGUI:New 
Gui, +AlwaysOnTop 
Gui, Margin, 10,10
Gui, Font, s11 q5 bold, Verdana
Gui, Add, DropDownList, x95 y7 w130 vDeleteButtonDropDownVar gRepopulateButtons
Gui, Add, Button, x225 y5 h0 w0										
Gui, Font, s11 q5 , Verdana
Gui, Show, NoActivate x364 y500 w1000 h40, watchlistWinTitle

WatchList_OBJ.Delete(DeleteButtonDropDownVar)
ButtonList := ""

For k, v in WatchList_OBJ
{
	Gui, WatchlistGUI:Add, Button, x+24 w85 gSendToTV v%v% -Default +Hwnd%v%, %v%
	ButtonList .= v "|"
}
GuiControl, WatchlistGUI:, DeleteButtonDropDownVar, %ButtonList%
return


SendToTV:
return


Where I'm having a headache understanding what is going on is at Line 29.

ButtonList .= RegexActiveTitle1 "|"

You can see I've dropped some msgboxes around there to try and understand what is going on. The ButtonList var should be growing by the RegexActiveTitle1 (a match from the window title) each time the hotkey is pressed (up to a limit of 14 or so).
However, what actually happens using " ButtonList .= RegexActiveTitle1 "|" " is that it acts like it's adding the entire list to itself. Even though the messageboxes will show everything as expected (a single item for regexactivetitle1 (which gets reset at the start of the hotkey), a uniformly added ButtonList. But when you click the drop down, there's duplicates - no duplicate buttons, but duplicates in the list.

The WEIRD thing is, setting it as ButtonList := RegexActiveTitle1 "|" at line 29 completely fixes the problem. Not only that, in the label, when it recreates the buttons using the object, somehow now it remembers the position that the buttons were created. However, if you look at the msgbox variables.... it shouldn't be working! The msgboxes show what you'd expect each time, but the dropdown behaves much differently.

It's not the label being attached to the dropdown edit, disabling that and the problem is still there.

Using this at line 31:

Code: Select all

GuiControl, WatchlistGUI:, DeleteButtonDropDownVar,
GuiControl, WatchlistGUI:, DeleteButtonDropDownVar, %ButtonList%
also has no effect, the list remains duplicated with .= (even the regexactivetitle1 is just a single word), and it somehow works with :=

If anyone experienced could spend a few minutes going over this I'd really love an explanation as to why it's behaving in this way (.= vs :=) and how the object is remembering the position. I'm not looking for a re-write or a fix, just an explanation as to the behavior. I already have it working with := but I don't like that it works unless I understand it.

Thank you.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Unexpected Behaviour, simple script

21 Aug 2022, 06:57

run the script with a debugger attached, set watches on ButtonList, RegexActiveTitle1, WatchList_OBJ and observe their contents as u step through the script
Yogi
Posts: 12
Joined: 06 Jul 2022, 23:03

Re: Unexpected Behaviour, simple script

21 Aug 2022, 07:38

swagfag wrote:
21 Aug 2022, 06:57
run the script with a debugger attached, set watches on ButtonList, RegexActiveTitle1, WatchList_OBJ and observe their contents as u step through the script
Thanks but it's exactly the same as the MsgBoxes.

Image

The label hasn't been triggererd after using the hotkey in this screenshot. So it's all in the hotkey I think. Notice the duplicates in the dropdown even though ButtonList has none.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Unexpected Behaviour, simple script  Topic is solved

21 Aug 2022, 10:19

Tab/DropDownList/ComboBox/ListBox: Value should contain a pipe-delimited list of entries to be appended at the end of the control's list. To replace (overwrite) the list instead, include a pipe as the first character (e.g. |Red|Green|Blue).
are u doing that somewhere?
Yogi
Posts: 12
Joined: 06 Jul 2022, 23:03

Re: Unexpected Behaviour, simple script

21 Aug 2022, 13:35

swagfag wrote:
21 Aug 2022, 10:19
Tab/DropDownList/ComboBox/ListBox: Value should contain a pipe-delimited list of entries to be appended at the end of the control's list. To replace (overwrite) the list instead, include a pipe as the first character (e.g. |Red|Green|Blue).
are u doing that somewhere?
Thank you so much, that fully explains the behaviour of the drop-down. I was not doing that, foolishly missed that in the documentation.
I assume because the GUI was submitted during the label, and it recreated the GUI with the buttonlist getting blanked that that is the reason why it wasn't appending it at that stage and replacing it despite no Pipe at the start.
Perfect, thank you!


-----
EDIT : For some reason the below isn't applying anymore, I could have sworn it was remembering the position using the other method but maybe I'm somehow imagining it! But no matter, im sort of glad it's not remembering the order anymore at least it makes sense to me now! Thanks again, now ill work on it remembering the order in a proper way. - sorry to the mod for having to approve my edits!

Disregard below ramblings.
-----

The last thing that was bothering me, is in the label, the order the buttons are recreated are affected by what method I choose to change the buttonlist (replacing or appending) in the hotkey.
When I use either of the following in the hotkey:

Code: Select all

ButtonList .= RegexActiveTitle1 "|"
GuiControl, WatchlistGUI:, DeleteButtonDropDownVar, % "|" ButtonList
OR the incorrect one, where it creates duplicates:

Code: Select all

ButtonList .= RegexActiveTitle1 "|"
GuiControl, WatchlistGUI:, DeleteButtonDropDownVar, % ButtonList
The recreation of the buttons happens in alphabetical order in the label. This is after the GUI submits and I assume (as mentioned above, gets destroyed and recreated). It's what you'd expect to happen - alphabetical recreation due to the obhect.

But then why does using the following (now confirmed valid method) at the hotkey stage;

Code: Select all

ButtonList := RegexActiveTitle1 "|"
GuiControl, WatchlistGUI:, DeleteButtonDropDownVar, % ButtonList
Result in the buttons being recreated in the order I originally made them? It's fantastic that it does but I have no idea why.

This is all that's in the label:

Code: Select all

Gui, Submit
<recreate basics of GUI>

WatchList_OBJ.Delete(DeleteButtonDropDownVar)			;	this deletes the button from the object (selected by the dropdown's var)
ButtonList := ""

For k, v in WatchList_OBJ
{
	Gui, WatchlistGUI:Add, Button, x+24 w85 gSendToTV v%v% -Default +Hwnd%v%, %v%
	ButtonList .= v "|"
}
GuiControl, WatchlistGUI:, DeleteButtonDropDownVar, %ButtonList%
Why does ( ButtonList := RegexActiveTitle1 "|" ) at the hotkey stage, cause it to remember the order?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Descolada, GroggyOtter, Mateusz53, moltenchees, peter_ahk and 190 guests