Import data to ListBox in txt file. Add and delete from listbox with button.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Import data to ListBox in txt file. Add and delete from listbox with button.

27 Mar 2018, 05:10

Code: Select all

Gui, Add, Edit, x182 y20 w130 h20 , Edit
Gui, Add, Button, x182 y80 w130 h30 , add
Gui, Add, Button, x182 y130 w130 h30 , remove
Gui, Add, Button, x182 y180 w130 h30 , import from txt
; Generated using SmartGUI Creator for SciTE
Gui, Show, w342 h329, Untitled GUI
return

GuiClose:
ExitApp
;info.txt
34KLM23
34AAB34
34CCC001

Hello,

I have a file called info.txt on the desktop of the computer. I want to import the data in this file into the listbox when I click on the "import from txt" button ..

To add the data I entered in the editbox box to the listbox with the "add" button,
I would like to delete it with the "remove" button if I select it from the listbox.

I need your help .. respects ..
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Import data to ListBox in txt file. Add and delete from listbox with button.

27 Mar 2018, 06:27

This should get you started.

Code: Select all

Gui, Add, Edit, x182 y20 w130 h20 , Edit
Gui, Add, Button, x182 y80 w130 h30 , add
Gui, Add, Button, x182 y130 w130 h30 , remove
Gui, Add, Button, x182 y180 w130 h30 gImport, import from txt
Gui, Add, ListBox, Choose1 vMyListBox 
; Generated using SmartGUI Creator for SciTE
Gui, Show, w342 h329, Untitled GUI
return

Import:
FileRead, info , info.txt
Loop, parse, info, `n, `r
{
	GuiControl,,MyListBox, %A_Loopfield%
	Gui, Show
}
return


GuiClose:
ExitApp
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Import data to ListBox in txt file. Add and delete from listbox with button.

27 Mar 2018, 09:21

Example:

Code: Select all

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

; 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)
	}
}
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: Import data to ListBox in txt file. Add and delete from listbox with button.

29 Mar 2018, 07:38

@tmplinshi, I like this listbox function
have you also a function for 'Listview' ?
example easy manage listview :
-add ( new row / columns )
-modify ( modify one row / columns )
-delete ( delete one or more selected row )
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Import data to ListBox in txt file. Add and delete from listbox with button.

29 Mar 2018, 08:23

@garry I don't have. ListView already have these built-in functions LV_Add / LV_Modify / LV_Delete...
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: Import data to ListBox in txt file. Add and delete from listbox with button.

29 Mar 2018, 08:32

@tmplinshi thank you, I have good working scripts with listview , I liked your function above for Listbox/DropDownlist/ComboBox

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 54 guests