ControlSend in a Listbox Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CosmicThing2
Posts: 19
Joined: 07 Oct 2016, 06:37
Location: UK

ControlSend in a Listbox

25 May 2018, 07:09

Hello all,

Optional Reading Backstory bit:
I work as an IT Tech in a school and run an after school games club for the students. I give them access to a few different appropriate games including Minecraft, PaintBall 2 and Trackmania. I've already made scripts for these games, one for minecraft which will copy all needed game files to the machine including mods, forge and the core files, and then launch the game. This is great and we have four servers with various mods installed which the students can play on. However students always want different mods and I wanted a way to use AHK to create a kind of 'Mod Manager'. Now for those that know minecraft, I know one exists as MultiMC but this allows access to a tonne more than just mods, I don't want the students playing around with the core game files.
Backstory bit over!

SO! I'm working on my own very basic mod manager which will have a large selection of mods to choose from. They simply choose from a list and press OK. It will then copy all needed mod files to the Minecraft directory and launch the game.

In AHK terms, it's two listboxes which you can move items between using arrows. This works fine but I wanted the selection in the listbox to move automatically to the next item when you move one across, but it only does this sortof 'half' the time. I have created an example at the moment using colours (all commented!). See code below, it runs fine, you are very welcome to test it and the issue would probably be more understandable to see it in action:

Code: Select all

;These variables keep track of everything which is in the left listbox and the right listbox
leftbox = Red|Green|Blue|Black|White|
rightbox =

Gui,Font,norm s14,Calibri Light
Gui, Add, ListBox, vColorChoice1 Sort x20 y20 w100 h200, Red|Green|Blue|Black|White|
Gui,Font,norm s20 w700,Calibri Light
Gui,Add,Button,x135 y90 w40 h40 gMover, >


Gui, 2:Font,norm s14,Calibri Light
Gui, 2:Add, ListBox, vColorChoice2 Sort x80 y20 w200 h380,
Gui, 2:Font,norm s20 w700,Calibri Light
Gui, 2:Add,Button,x20 y175 w40 h40 gMovel, <

Gui , Show, w300 h250, Mod Manager
WinGetPos, gui1x, gui1y,gui1w,,Mod Manager
gui2x := gui1x+gui1w+5
Gui,2:Show, x%gui2x% w300 h420, Selected Mods
Return



Mover:
;Reset Variables
selectmore = False
selectedlindex = 1

;Firstly submit everything on the GUI. If they haven't selected anything, just stop there.
Gui, Submit, NoHide
GuiControlGet, selectedl, 1:, ColorChoice1
if selectedl =
	return
	
;Add the selected item to the right box and to the rightbox variable
GuiControl, 2:, ColorChoice2, %selectedl% 
rightbox = %rightbox%%selectedl%|

;Retrieve all items from the current (old) list and count how many there are
ControlGet, cc1rows, List, Count, ListBox1, Mod Manager
cc1rowscount = 0
Loop, Parse, cc1rows, `n
{
	if selectedl = %A_LoopField%
		selectedlindex = %A_Index%
	cc1rowscount += 1
}
;If there's more than one item, we will need to run the selection code (to automatically select the next item)
if cc1rowscount > 1
	selectmore = True
	;If the item they selected is at the bottom, it's a special case because the selection code will then need to select the previous item (instead of the next one)
	if selectedlindex = %cc1rowscount%
			selectedlindex -= 1

;Search the leftbox variable for this selected item and remove it
selectedl = %selectedl%|
leftbox := StrReplace(leftbox, selectedl)

;Update the GUI to the leftbox variable
GuiControl, 1:, ColorChoice1, |%leftbox% 

;If we do need to select more items (because there's more than one, run this code)
if selectmore = True
{
	;Find the new item where the old one was
	ControlGet, cc1rows, List, Count, ListBox1, Mod Manager
	Loop, Parse, cc1rows, `n
	{
		if selectedlindex = %A_Index%
			selectl = %A_LoopField%
	}
	;Sleep (not sure if this is needed) and then send this to the ListBox which *should* select this item
	Sleep 1
	ControlSend, ListBox1, %selectl%, Mod Manager
}

return

Movel:
;Firstly submit everything on the GUI. If they haven't selected anything, just stop there.
Gui, 2:Submit, NoHide
GuiControlGet, selectedr, 2:, ColorChoice2
if selectedr =
	return

;Add the selected item to the left box and to the leftbox variable
GuiControl, 1:, ColorChoice1, %selectedr% 
leftbox = %leftbox%%selectedr%|

;Search the rightbox variable for this selected item and remove it
selectedr = %selectedr%|
rightbox := StrReplace(rightbox, selectedr)

;Update the GUI to the rightbox variable
GuiControl, 2:, ColorChoice2, |%rightbox%
;Msgbox, leftbox is %leftbox%
;Msgbox, rightbox is %rightbox% 
return

GuiClose:
ExitApp

2GuiClose:
ExitApp
I've only set up the selection code for the Move Right arrow. I want to be able to just 'mash' the right arrow key 5 times and all 5 items whiz across to the other box. But it deselects them sometimes... I really don't understand why.

Any help or suggestions to my code is greatly appreciated, you guys are awesome.

Thanks!
CosmicThing2
Posts: 19
Joined: 07 Oct 2016, 06:37
Location: UK

Re: ControlSend in a Listbox  Topic is solved

25 May 2018, 08:29

NEVERMIND!

Have fixed it, I just needed a ControlClick to ensure the control was active I think. Anyone is still welcome to browse/peruse/change/steal the code, I'm not fussed.

Thanks
Guest

Re: ControlSend in a Listbox

27 May 2018, 05:59

Here is an alternative which you may find useful. There is some duplication in the code which you could to shorten it even more. I use two additional variables (lists) to simply keep track of what is in the left/right listboxes. Seems to work for me. If you don't select anything it will use the first item in the listbox.

Code: Select all

#SingleInstance, force

;These variables keep track of everything which is in the left listbox and the right listbox
LeftBoxStart:="Red|Green|Blue|Black|White|"
LeftBox:=LeftBoxStart
RightBoxStart:=""
RightBox:=""

Gui, LeftBox: New, , Mod Manager
Gui, LeftBox: Font,norm s14,Calibri Light
Gui, LeftBox: Add, ListBox, vColorChoice1 Sort x20 y20 w100 h200, %LeftBoxStart%
Gui, LeftBox: Font,norm s20 w700,Calibri Light
Gui, LeftBox: Add, Button,x135 y90 w40 h40 gMover, >

Gui, RightBox: New, , Selected Mods
Gui, RightBox: Font,norm s14,Calibri Light
Gui, RightBox: Add, ListBox, vColorChoice2 Sort x80 y20 w200 h380, %RightBoxStart%
Gui, RightBox: Font,norm s20 w700,Calibri Light
Gui, RightBox: Add, Button,x20 y175 w40 h40 gMovel, <

Gui, LeftBox:Show, w300 h250
WinGetPos, gui1x, gui1y,gui1w,,Mod Manager
gui2x := gui1x+gui1w+5
Gui, RightBox:Show, x%gui2x% w300 h420
Return

Mover:
Gui, LeftBox:Submit, Nohide
; if something was selected ColorChoice1 has a value, otherwise it doesn't so we can just grab the first item of the list
If (ColorChoice1 = "")
	{
	 Gui +LastFound 
	 GuiControl, Choose, ColorChoice1, 1
	 Gui, LeftBox:Submit, Nohide
	}
If (ColorChoice1 = "") ; if still empty the list is empty
	Return
LeftBox:=Trim(StrReplace("|" LeftBox "|", "|" ColorChoice1 "|", "|"),"|")
RightBox:=Trim("|" RightBox "|" ColorChoice1 "|", "|")
GuiControl,LeftBox:, ColorChoice1, |%LeftBox%
GuiControl,RightBox:, ColorChoice2, |%RightBox%

Return

Movel:
Gui, RightBox:Submit, Nohide
; if something was selected ColorChoice1 has a value, otherwise it doesn't so we can just grab the first item of the list
If (ColorChoice2 = "")
	{
	 Gui +LastFound  
	 GuiControl, Choose, ColorChoice2, 1
	 Gui, RightBox:Submit, Nohide
	}
If (ColorChoice2 = "") ; if still empty the list is empty
	Return
RightBox:=Trim(StrReplace("|" RightBox "|", "|" ColorChoice2 "|", "|"),"|")
LeftBox:=Trim("|" LeftBox "|" ColorChoice2 "|", "|")
GuiControl,LeftBox:, ColorChoice1, |%LeftBox%
GuiControl,RightBox:, ColorChoice2, |%RightBox%

Return

LeftBoxGuiClose:
RightBoxGuiClose:
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GEOVAN, mikeyww and 223 guests