Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Last active April 29, 2020 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmplinshi/ab4f6d61518805c73a35ef663f124da3 to your computer and use it in GitHub Desktop.
Save tmplinshi/ab4f6d61518805c73a35ef663f124da3 to your computer and use it in GitHub Desktop.
; Works for DropDownList/ComboBox/ListBox
Class ListBox
{
__New(hwnd) {
; GuiControl, +AltSubmit, %hwnd%
this.hwnd := hwnd
}
Add(str) {
GuiControl,, % this.hwnd, %str%
}
; RowNumbers - Row number(s). Use non-number to delimit multiple numbers. e.g. 1|2|3 or 1,2,3
; If the parameter is omitted, all rows are deleted.
Delete(ByRef RowNumbers := "") {
if !RowNumbers
return this.DeleteAll()
RowNumbers := RegExReplace(RowNumbers, "\D+", "|")
RowNumbers := Trim(RowNumbers, "|")
Sort, RowNumbers, R D| N ; Reverse numbers
Loop, Parse, RowNumbers, |
Control, Delete, %A_LoopField%,, % "ahk_id " this.hwnd
}
DeleteAll() {
GuiControl,, % this.hwnd, |
}
DeleteSelected() {
GuiControlGet, RowNumbers,, % this.hwnd
if RowNumbers
this.Delete(RowNumbers)
}
ImportFromFile(FileName) {
FileRead, s, %FileName%
s := RegExReplace(s, "\R+", "|")
s := Trim(s)
GuiControl,, % this.hwnd, %s%
}
SaveToFile(FileName) {
ControlGet, v, List,,, % "ahk_id " this.hwnd
FileOpen(FileName, "w").Write(v)
}
}
#Include Class_ListBox.ahk
Gui, Font, s12
Gui, Add, Button, gAdd, Add
Gui, Add, Button, x+50 gDelete, Delete
Gui, Add, Button, x+50 gDeleteAll, Delete All
Gui, Add, Button, x+50 gSave, Save To File
Gui, Add, ListBox, xm r20 w470 AltSubmit Multi HWNDhLB, ; Note the use of AltSubmit
Gui, Show
LB := New ListBox(hLB)
if FileExist("info.txt")
LB.ImportFromFile("info.txt")
Return
Add:
Gui, +OwnDialogs
InputBox, newItem, Add,,, 387, 105
if (!ErrorLevel && newItem != "")
LB.Add(newItem)
return
Delete:
LB.DeleteSelected()
return
DeleteAll:
LB.DeleteAll()
return
Save:
LB.SaveToFile("info.txt")
MsgBox, Saved!
return
GuiClose:
ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment