Need Help With List Box Functions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guest

Need Help With List Box Functions

11 Feb 2015, 17:32

Greetings everyone!

I am currently writing a script that is utilizing the "ListBox" function. The problem I am having is that I have more than one item in my list box. I want to have each item do its own function while you choose it and click the button to make it execute. I wrote an example below, its not the best but it gives a better idea on what I am trying to accomplish.

Thank you!

Code: Select all

#SingleInstance, Force
 
LBContents =
( Join|
Trench Coat Soilder
)
LBContents1 =
( Join|
XOF Soilder
)
;size and lbcontents code blow
Gui, Add, ListBox, vMyListBox gMyListBox w350 r15 x10 y90, %LBContents%|%LBContents1%
;button to execute command
Gui, Add, Button, gHALButton2 x10 y300 w350 h30, Copy Contents to Clipboard
;random box
Gui, Add, Edit, x10 y350 w350 h30


Gui, Show
return
    ; end of auto-execute section

MyListBox:
{
if A_GuiEvent <> DoubleClick
    return
return
    ; end of MyListBox subroutine
}
; THIS IS WHERE I WANT THE EXECUTION TO START WHEN YOU PRESS THE BUTTON
;
HALButton2:
{

    Gui, Submit, NoHide
    ControlGet, List1, List, , ListBox1
    
}
ControlGet, ChooseString, %LBContents% = 1
;then
	MsgBox, okay
{
; AFTER THIS , IF I WANT TO CHOOSE ITEM NUMBER TWO PREFORM ITS OWN FUNCTION.
; HOWEVER IT WILL GO TO TWO MESSAGES IN STEAD OF ONE.
ControlGet, ChooseString, %LBContents1% = 1
;IF I CHOOSE SECOND OPTION THEN BEGIN SCRIPT BELOW FOR THAT OPTION
    MsgBox, okay 2
}
return

Guiclose:
exitapp
jpginc
Posts: 124
Joined: 29 Sep 2013, 22:35

Re: Need Help With List Box Functions

11 Feb 2015, 18:16

I'm not exactly sure what this line is supposed to do?

Code: Select all

ControlGet, ChooseString, %LBContents% = 1
http://ahkscript.org/docs/commands/GuiC ... tm#ListBox says
When the Gui Submit command is used, the control's associated output variable (if any) receives the text of the currently selected item.
In your case the associated output variable is MyListBox. I think what you want to do is something like this:

Code: Select all

Gui, Submit, NoHide
if(MyListBox == "Trench Coat Soilder")
{
    MsgBox, Hello Trench Coat Soldier
} else if ( MyListBox == "XOF Soilder")
{
    MsgBox, Hello XOF Soldier
} else 
{ 
    MsgBox, Nothing is selected...
}
Guest

Re: Need Help With List Box Functions

11 Feb 2015, 18:24

Thank you, I also went on the IRC and talked to Elesar. He gave me a better, more simple script.

Here's the correct method.

Code: Select all

RawEdit
#SingleInstance, Force
 
;<=====  Settings  ===========================================================>
LBContents := "Trench Coat Soilder|XOF Soilder"
 
;<=====  GUI  ================================================================>
Gui, Add, ListBox, vMyListBox gMyListBox AltSubmit w350 r15 x10 y90, %LBContents%
Gui, Add, Button, gHALButton2 x10 y300 w350 h30, Copy Contents to Clipboard
Gui, Add, Edit, x10 y350 w350 h30
Gui, Show
return
 
;<=====  Labels  =============================================================>
MyListBox:
	if A_GuiEvent <> DoubleClick
		return
	return
 
HALButton2:
    Gui, Submit, NoHide
	if (MyListBox == 1)
		MsgBox, First item selected.
	else if (MyListBox == 2)
		MsgBox, Second item selected.
	else
		MsgBox, Nothing selected.
	return
 
Guiclose:
	exitapp
Guest

Re: Need Help With List Box Functions

12 Feb 2015, 09:06

I ran into a quick problem. Since my GUI has two listboxes, how can I add another "LBContent" ?

Example:

Code: Select all

;Settings For List Box =========================================
LBContents := "Item One|Item Two)"
LBContents2 := "Item Three|Item Four|"
I have the two list boxes set up like this:

Code: Select all

/* 

LIST BOXES BELOW

*/
Gui, Add, ListBox, vMyListBox gMyListBox AltSubmit x174 y154 w133 h69, %LBContents%

Gui, Add, ListBox, vMyListBox gMyListBox2 AltSubmit x174 y241 w133 h69, %LBContents2%
/*

END LIST BOXES

*/
When I try to execute the program via SciTE4 it gives me a debug error stating that the variable cannot be used for more than one control.
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Need Help With List Box Functions

12 Feb 2015, 09:15

In your ListBox controls, the v (variable) can't be the same. Change the second one to vMyListBox2 and you should be OK.
Guest

Re: Need Help With List Box Functions

12 Feb 2015, 09:52

Elesar wrote:In your ListBox controls, the v (variable) can't be the same. Change the second one to vMyListBox2 and you should be OK.
I've tried that and I still get an error message.

Here is what the code looks like based upon your recommendations.

Code: Select all

/* 

LIST BOXES BELOW

*/
Gui, Add, ListBox, vMyListBox gMyListBox AltSubmit x174 y154 w133 h69, %LBContents%

[color=#FF0000]Gui, Add, ListBox, vMyListBox2 MyListBox2 AltSubmit x174 y241 w133 h69, %LBContents2%[/color]
/*

END LIST BOXES

*/
Gui, Add, Text, x174 y139 w85 h13, Box One
Gui, Add, Text, x176 y226,Box Two
; Generated using SmartGuiXP Creator mod 4.3.29.0
;=============================================================================>
;Title
Gui, Show, Center w462 h362, Metal Gear Solid V - RM Extraction v1.1
return
;<=====  Labels  =============================================================>
MyListBox:
	if A_GuiEvent <> DoubleClick
		return
	return
ButtonExtract:
    Gui, Submit, NoHide
	if (MyListBox == 1)
		MsgBox, You've selected %1%
	else if (MyListBox == 2)
		MsgBox, Second item selected.
	else if (MyListBox == 3)
		MsgBox, 3rd item selected.
	else if (MyListBox == 4)
		MsgBox, Second item selected.
	else if (MyListBox == 5)
		MsgBox, Second item selected.
	else if (MyListBox == 6)
		MsgBox, Second item selected.
	else if (MyListBox == 7)
		MsgBox, Second item selected.
	else if (MyListBox == 8)
		MsgBox, Second item selected.
	else if (MyListBox == 9)
		MsgBox, Second item selected.
	else if (MyListBox == 10)
		MsgBox, Second item selected.
	else if (MyListBox == 11)
		MsgBox, Second item selected.

	[color=#FF0000]else if (MyListBox2 == 1)[/color]
		msgbox, Test
	else 
		msgbox, Test 2
	return
;
;===============exit app
GuiClose:
ExitApp

Guest

Re: Need Help With List Box Functions

12 Feb 2015, 09:53

Take out the BB codes that I added (Forgot they don't display under [code\] quotes)

Thanks
Elesar
Posts: 70
Joined: 31 Oct 2013, 07:56

Re: Need Help With List Box Functions

12 Feb 2015, 09:58

Your second code was almost correct. The second LB you are missing the g for its label. Once you do that, you need to add in a label for it, and with those two changes, it runs fine on my system.

Code: Select all

LBContents := "item1|item2|item3"
LBContents2 := "item4|item5|item6"

Gui, Add, ListBox, vMyListBox gMyListBox AltSubmit x174 y154 w133 h69, %LBContents%
Gui, Add, ListBox, vMyListBox2 gMyListBox2 AltSubmit x174 y241 w133 h69, %LBContents2%
Gui, Add, Text, x174 y139 w85 h13, Box One
Gui, Add, Text, x176 y226,Box Two
Gui, Show, Center w462 h362, Metal Gear Solid V - RM Extraction v1.1
return

MyListBox:
MyListBox2:
    if A_GuiEvent <> DoubleClick
        return
    return

ButtonExtract:
    Gui, Submit, NoHide
    if (MyListBox == 1)
        MsgBox, You've selected %1%
    else if (MyListBox == 2)
        MsgBox, Second item selected.
    else if (MyListBox == 3)
        MsgBox, 3rd item selected.
    else if (MyListBox == 4)
        MsgBox, Second item selected.
    else if (MyListBox == 5)
        MsgBox, Second item selected.
    else if (MyListBox == 6)
        MsgBox, Second item selected.
    else if (MyListBox == 7)
        MsgBox, Second item selected.
    else if (MyListBox == 8)
        MsgBox, Second item selected.
    else if (MyListBox == 9)
        MsgBox, Second item selected.
    else if (MyListBox == 10)
        MsgBox, Second item selected.
    else if (MyListBox == 11)
        MsgBox, Second item selected.

    else if (MyListBox2 == 1)
        msgbox, Test
    else 
        msgbox, Test 2
    return

GuiClose:
	ExitApp
montagesnmore
Posts: 19
Joined: 11 Feb 2015, 17:26

Re: Need Help With List Box Functions

13 Feb 2015, 11:04

Thank you Elesar, I enjoy learning new things each time I write a GUI interface lol. Good take aways.

Lastly, I forget what was the correct code to add to display the item they selected?

Example:

Once you hit the button, I have the msgbox state "You've selected xx" But instead of writing in what xx is manually I use a shortcut to display it. I think its %variable% I may be incorrect. Did that make sense at all? LOL


Elesar, I sent you a PM

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 324 guests