Remove from ListBox list

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Remove from ListBox list

Post by nightsalive » 22 Apr 2022, 08:16

Hi, I'm new to AHK and have been trying to remove the selected item from a ListBox (the list) by clicking ok.
The ListBox contains items from a list.
This is what I hope to achieve: I select an item from the Listbox, click ok, and the item is removed.

Code: Select all

list = blue|red|one|two       

Gui, Add, ListBox, Multi vMyListBox x12 y69 w80 h170  , %list%
Gui, Add, Button, x92 y209 w40 h20 , ok 
Gui, Show, x172 y122 h250 w240, New GUI Window
Return

GuiClose:
ExitApp


Buttonok:
{
	Gui, Submit, Nohide
	Loop, Parse, MyListBox, |
	{
		RemovedValue := nickname_list.RemoveAt(%A_Index%)
		MsgBox, Removed %RemovedValue%.
	}
}
[Mod edit: [code][/code] tags added.]

User avatar
mikeyww
Posts: 27096
Joined: 09 Sep 2014, 18:38

Re: Remove from ListBox list

Post by mikeyww » 22 Apr 2022, 09:25

Welcome to this AutoHotkey forum!

You can use :arrow: GuiControl to change an existing control.

Code: Select all

list = blue|red|one|two
b    = |
Gui, Font, s10
Gui, Add, ListBox, w230 r4 Multi vremove, %list%
Gui, Add, Button , wp   Default         , Remove
Gui, Show,, Items
Return

ButtonRemove:
Gui, Submit, NoHide
GuiControl,, remove, % b list := Trim(StrReplace(b list b, b remove b, b), b)
Return

nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Re: Remove from ListBox list

Post by nightsalive » 22 Apr 2022, 22:06

Wow, thank you so much. And it even removes multiple items!

ListBox only accepts "pipe-delimited list of entries", so using an array doesn't work.

Code: Select all

list = [one, two, blue]
Is there a way to make ListBox accept lists so I could use array commands such as adding items through Array.Push.?
Or is it better to continue operating on the list as a string (as done by trimming?)

Last question: How do I go about adding an item to the ListBox?

Code: Select all

list = blue|red|one|two

b    = |
Gui, Font, s10
Gui, Add, ListBox, w230 r4 Multi vremove , %list%     ;Multi allows multiple items to be selected 
Gui, Add, Button , wp   Default          , Remove
Gui, Add, Edit,    wp  vname             , 
Gui, Add, Button , wp                    , Add
Gui, Show, , Items 

Return

ButtonRemove:
Gui, Submit, NoHide    ;upon clicking remove
GuiControl,, remove, % b list := Trim(StrReplace(b list b, b remove b, b), b)   ;StrReplace -> goes through list, |remove| is found and replaced with |. Trim -> new list has | removed (so empty item doesn't exist), there are |'s surrounding list to allow multiple items to be selected and deleted
Return

ButtonAdd:
Gui, Submit, Nohide
GuiControl,, add, % list := list + b name b 
Return 

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Remove from ListBox list.

Post by BoBo » 22 Apr 2022, 23:44

Code: Select all

list = [one, two, blue]

Gui, Add, ListBox, w230 r4 Multi vremove , % createList(list)
   .
   .
   .
createList(array) {
   for each, item in array
	  list .= (a_index=1) ? item "||" : item "|"
   Return RTrim(list,"|")
   }
   
F11::   
F12::
   list.push(A_ThisHotkey)
   GuiControl,, remove, % createList(list)
   Return
HTH

nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Re: Remove from ListBox list

Post by nightsalive » 23 Apr 2022, 04:52

Sorry, but I don't understand why this doesn't work:

Code: Select all

list = [one, two, blue]

Gui, Add, ListBox, w230 r4 Multi vremove , % createList(list)
Gui, Show,, Mytest

createList(array) {
   for each, item in array
	  list .= (a_index=1) ? item "||" : item "|" ;takes each element of list and... surrounds it with ||, |..?
   Return RTrim(list,"|")  ;removes the excess | at the end of the list
   }
   
As the list is displayed empty... Am I missing something?

User avatar
boiler
Posts: 17079
Joined: 21 Dec 2014, 02:44

Re: Remove from ListBox list

Post by boiler » 23 Apr 2022, 05:21

There are two things wrong with this:

Code: Select all

list = [one, two, blue]

Should be:

Code: Select all

list := ["one", "two", "blue"]

See Operators in Expressions, Expressions, and Simple Arrays.

nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Re: Remove from ListBox list

Post by nightsalive » 23 Apr 2022, 07:06

Ohh... I see. That makes sense. Thank you!

nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Re: Remove from ListBox list

Post by nightsalive » 23 Apr 2022, 09:54

Thanks everyone for your help. This is what I got.

Code: Select all

list := ["one", "two", "blue", "red"]
new_item = 

my_list := createList(list)

Gui, Font, s10
Gui, Add, Text, x12 y40 w180 h30 , list practice

Gui, Add, ListBox, w230 r6  vremove, % createList(list)     ;Multi allows multiple items to be selected 
Gui, Add, Button , wp              , Remove
Gui, Add, Edit,    wp    vnew_item      , 
Gui, Add, Button , wp     Default       , Add

Gui, Show, , Items 

Return

createList(array) {
   for each, item in array
	  list .= (a_index=1) ? item "|" : item "|" ;takes each element of list and ends with | 
   Return RTrim(list,"|")  ;removes the excess | at the end of the list
   }
   
GuiClose:
ExitApp

ButtonRemove:
Gui, Submit, NoHide    
list.RemoveAt(index of remove)        ;what is the selected item?
my_list := createList(list)
GuiControl, Text, remove, |%my_list%
Return

ButtonAdd:
Gui, Submit, NoHide
list.push(new_item)
my_list := createList(list)
GuiControl, Text, remove, |%my_list%
GuiControl, , Edit1, 
Return 
Is there a way to get the index of the selected item in the list box? Or would I be better off giving up on arrays and use a string with pipes..

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Remove from ListBox list

Post by BoBo » 23 Apr 2022, 10:12

boiler wrote:
23 Apr 2022, 05:21
There are two things wrong with this:

Code: Select all

list = [one, two, blue]
Should be:

Code: Select all

list := ["one", "two", "blue"]
Obviously I wasn't really awake at that time :oops: Thx :thumbup:
list .= (a_index=1) ? item "|" : item "|" ;takes each element of list and ends with |
So, where’s the difference in this case? I can't see that you're preselecting the first item as intended?!

nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Re: Remove from ListBox list

Post by nightsalive » 23 Apr 2022, 10:14

Nevermind, I got it working by adding AltSubmit... which "gets the position rather than its text". :)

Code: Select all

list := ["one", "two", "blue", "red"]
new_item = 

my_list := createList(list)

Gui, Font, s10
Gui, Add, Text, x12 y40 w180 h30 , list practice

Gui, Add, ListBox, AltSubmit w230 r6  vremove, % createList(list)     ;Multi allows multiple items to be selected 
Gui, Add, Button , wp              , Remove
Gui, Add, Edit,    wp    vnew_item      , 
Gui, Add, Button , wp     Default       , Add

Gui, Show, , Items 

Return

createList(array) {
   for each, item in array
	  list .= (a_index=1) ? item "|" : item "|" ;takes each element of list and ends with | 
   Return RTrim(list,"|")  ;removes the excess | at the end of the list
   }
   
GuiClose:
ExitApp

ButtonRemove:
Gui, Submit, NoHide    
list.RemoveAt(remove)        ;what is the selected item?
my_list := createList(list)
GuiControl, Text, remove, |%my_list%

Return

ButtonAdd:
Gui, Submit, NoHide
list.push(new_item)
my_list := createList(list)
GuiControl, Text, remove, |%my_list%
GuiControl, , Edit1, 
Return 

nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Re: Remove from ListBox list

Post by nightsalive » 23 Apr 2022, 11:06

BoBo wrote:
23 Apr 2022, 10:12
boiler wrote:
23 Apr 2022, 05:21
There are two things wrong with this:

Code: Select all

list = [one, two, blue]
Should be:

Code: Select all

list := ["one", "two", "blue"]
Obviously I wasn't really awake at that time :oops: Thx :thumbup:
list .= (a_index=1) ? item "|" : item "|" ;takes each element of list and ends with |
So, where’s the difference in this case? I can't see that you're preselecting the first item as intended?!
What? Sorry, I didn’t completely understand what that part of the code was doing. I assumed it was taking the first item in the list and ended it with ||, and ended the rest with |. So the string becomes item1||item2|item3. And ending a item with || in a list makes it preselected, right?

I didn’t find keeping the first item preselected necessary if that’s what you’re asking… Is there a reason why I should keep it preselected? I’d rather preselect the most recently added item, but I haven’t got my head around that yet.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Remove from ListBox list

Post by BoBo » 23 Apr 2022, 11:39

I didn’t find keeping the first item preselected necessary if that’s what you’re asking…
"Der Wurm muß dem Fisch schmecken, nicht dem Angler!" … AKA it's about the user. Maybe less important if ignored using a list box, but doing the same with a DropDownList instead will show an initial empty box, that AFAIK is bad practice. JFTR :)

nightsalive
Posts: 8
Joined: 22 Apr 2022, 08:11

Re: Remove from ListBox list

Post by nightsalive » 23 Apr 2022, 22:09

BoBo wrote:
23 Apr 2022, 11:39
I didn’t find keeping the first item preselected necessary if that’s what you’re asking…
"Der Wurm muß dem Fisch schmecken, nicht dem Angler!" … AKA it's about the user. Maybe less important if ignored using a list box, but doing the same with a DropDownList instead will show an initial empty box, that AFAIK is bad practice. JFTR :)
Hmm, I see… Thanks for the tip. :thumbup:

ahkAndSteak
Posts: 10
Joined: 01 Apr 2022, 20:58

Re: Remove from ListBox list

Post by ahkAndSteak » 12 Aug 2022, 17:37

In this line:

Code: Select all

GuiControl,, remove, % b list := Trim(StrReplace(b list b, b remove b, b), b)
the "remove" string is referring to two different things, correct?

The first remove is referring to the listbox control itself
The second instance is supposed to be a string variable that is holding a value that the listbox will now hold, as the only item ( ? )

Not trying to criticize, but if this is so, it can be confusing for newbies.

But if the "remove" in both cases both refer to the Listbox control, then I"m confused :)
mikeyww wrote:
22 Apr 2022, 09:25
Welcome to this AutoHotkey forum!

You can use :arrow: GuiControl to change an existing control.

Code: Select all

list = blue|red|one|two
b    = |
Gui, Font, s10
Gui, Add, ListBox, w230 r4 Multi vremove, %list%
Gui, Add, Button , wp   Default         , Remove
Gui, Show,, Items
Return

ButtonRemove:
Gui, Submit, NoHide
GuiControl,, remove, % b list := Trim(StrReplace(b list b, b remove b, b), b)
Return

User avatar
mikeyww
Posts: 27096
Joined: 09 Sep 2014, 18:38

Re: Remove from ListBox list

Post by mikeyww » 12 Aug 2022, 18:07

You are correct in concept, though this is one variable name being used in two different ways. The following will work just as well.

Code: Select all

GuiControl,, ListBox1, % b list := Trim(StrReplace(b list b, b remove b, b), b)
Explained: https://www.autohotkey.com/docs/commands/GuiControl.htm#Parameters

Post Reply

Return to “Ask for Help (v1)”