Can the newly added item be move to the top of a listbox and selected

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Can the newly added item be move to the top of a listbox and selected

10 Apr 2024, 03:16

I want to use GuiCtrl.Add(Items) method to add a new entry to a listbox, and move it to the top place.
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Can the newly added item be move to the top of a listbox and selected

10 Apr 2024, 03:58

Use GuiCtrl.Delete first, then add the entire list using GuiCtrl.Add() with the whole array that has the items in the order you want them (use Array.InsertAt(1) to add your new item at the top of the array). Then use GuiCtrl.Choose(1) to select the first item.
ntepa
Posts: 436
Joined: 19 Oct 2022, 20:52

Re: Can the newly added item be move to the top of a listbox and selected

10 Apr 2024, 04:13

Another way using SendMessage:

Code: Select all

#Requires AutoHotkey v2.0

g := Gui()
g.AddText(, "Add a new item:")
myEdit := g.AddEdit("w150")
g.AddButton("yp", "Enter").OnEvent("Click", AddItemToListBox)
myListBox := g.AddListBox("xm r10 w200", ["apple", "orange", "banana"])
g.Show()

AddItemToListBox(*) {
    LB_INSERTSTRING := 0x0181
    index := 0 ; wParam is zero-indexed position to insert at
    SendMessage(LB_INSERTSTRING, index, StrPtr(myEdit.Text), myListBox)
    myListBox.Choose(1)
}
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: Can the newly added item be move to the top of a listbox and selected

11 Apr 2024, 03:28

boiler wrote:
10 Apr 2024, 03:58
Use GuiCtrl.Delete first, then add the entire list using GuiCtrl.Add() with the whole array that has the items in the order you want them (use Array.InsertAt(1) to add your new item at the top of the array). Then use GuiCtrl.Choose(1) to select the first item.
Thanks for the pretty much straight forward solution. :clap:
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: Can the newly added item be move to the top of a listbox and selected

11 Apr 2024, 03:40

ntepa wrote:
10 Apr 2024, 04:13
Another way using SendMessage:

Code: Select all

#Requires AutoHotkey v2.0

g := Gui()
g.AddText(, "Add a new item:")
myEdit := g.AddEdit("w150")
g.AddButton("yp", "Enter").OnEvent("Click", AddItemToListBox)
myListBox := g.AddListBox("xm r10 w200", ["apple", "orange", "banana"])
g.Show()

AddItemToListBox(*) {
    LB_INSERTSTRING := 0x0181
    index := 0 ; wParam is zero-indexed position to insert at
    SendMessage(LB_INSERTSTRING, index, StrPtr(myEdit.Text), myListBox)
    myListBox.Choose(1)
}
Probably a better solution :thumbup: , by the way what about delete an item, I know there is a LB_DELETESTRING := 0x0182 counterpart.
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Can the newly added item be move to the top of a listbox and selected

11 Apr 2024, 04:52

songdg wrote: what about delete an item
GuiCtrl.Delete(Value) lets you specify the item to delete from a ListBox.
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: Can the newly added item be move to the top of a listbox and selected

11 Apr 2024, 09:08

boiler wrote:
11 Apr 2024, 04:52
songdg wrote: what about delete an item
GuiCtrl.Delete(Value) lets you specify the item to delete from a ListBox.
Thank you so much, much appreciated :bravo:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 26 guests