Listview add remove

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

Listview add remove

18 Dec 2017, 05:43

Code: Select all

Gui, Add, ListView, x152 y20 w480 h290 , ListView
Gui, Add, Edit, x12 y40 w130 h20 , 
Gui, Add, Text, x12 y20 w130 h20 , Name
Gui, Add, Text, x12 y70 w130 h20 , Surname
Gui, Add, Edit, x12 y90 w130 h20 , 
Gui, Add, Text, x12 y120 w130 h20 , Birth Date
Gui, Add, Edit, x12 y140 w130 h20 , 
Gui, Add, Button, x12 y170 w130 h40 , Add
Gui, Add, Button, x12 y220 w130 h40 , Remove

Gui, Show, w664 h379, Untitled GUI
return

GuiClose:
ExitApp
Hello.

When I click on the add button,

I want to add the data in the name, surname and birth date boxes to the ListView.

  I want to delete it when I click on the select and remove button from ListView.

also: 3 columns in the ListView object. the titles of the columns will be the names of the "edit" boxes.
Thank you.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

18 Dec 2017, 06:31

Hi.
Try this:

Code: Select all

Gui, Add, ListView, x152 y20 w480 h290  LV0x1
Gui, Add, Edit, x12 y40 w130 h20 vName, 
Gui, Add, Text, x12 y20 w130 h20, Name
Gui, Add, Text, x12 y70 w130 h20, Surname
Gui, Add, Edit, x12 y90 w130 h20 vSurname, 
Gui, Add, Text, x12 y120 w130 h20 , Birth Date
Gui, Add, Edit, x12 y140 w130 h20 vBirth, 
Gui, Add, Button, x12 y170 w130 h40 gAdd, Add
Gui, Add, Button, x12 y220 w130 h40 gRemove, Remove

LV_InsertCol(1, 100, "Name")
LV_InsertCol(2, 175, "Surname")
LV_InsertCol(3, auto, "Birth Date")

Gui, Show, w664 h379, Untitled GUI
GuiControl, Focus, Name
return

GuiClose:
ExitApp

Add:
Gui, Submit, NoHide
LV_Add("", Name, Surname, Birth)
return

Remove:
LV_Delete(LV_GetNext())
return
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

18 Dec 2017, 06:53

Adamsın ! :)
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

18 Dec 2017, 07:06

ramazan brother will have another responsibility.

If I double click on any of the records you added to ListView,

Give your name, surname, date of birth with MsgBox respectively


ramazan abi bir sorum daha olacak.

listview'e eklediğin kayıtlardan herhangibirinin üzerine çift tıkladığımda,

adı, soyadı, doğum tarihini sırasıyla msgbox ile uyarı versin
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

18 Dec 2017, 07:08

Hi chngrcn,

I made a little mistake.
Using LV_InsertCol(...) is for modifiying ListViews.
You can even use directly

Code: Select all

Gui, Add, ListView, x152 y20 w480 h290  LV0x1, Name|Surname|Birth Date
without LV_InsertCol(...).
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

18 Dec 2017, 07:16

also how can I keep records in memory. I do not want my records to be deleted every time I restart the script. How do I create a database?
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

18 Dec 2017, 07:19

Hallo chngrcn,

you endanger my anonymity. Please don't use my real name :crazy: :D :D

For your case, you have to add a gLabel into your ListView and a new label. Look code below:

Code: Select all

Gui, Add, ListView, x152 y20 w480 h290  LV0x1 gSelection, Name|Surname|Birth Date
....
Selection:
msgbox % Name " - " Surname " - " Birth
return
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

18 Dec 2017, 07:27

also how can I keep records in memory. I do not want my records to be deleted every time I restart the script. How do I create a database?
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

18 Dec 2017, 07:56

chngrcn wrote:... how can I keep records in memory ... How do I create a database?
For this case, you have to store the content of the ListView into a variable, stored onto Harddrive.
By starting your script, you have to readout the stored variable and to insert into your ListView.
Look code below:

Code: Select all

Gui, Add, ListView, x152 y20 w480 h290  LV0x1 vListViewContent gSelectionLV, Name|Surname|Birth Date
Gui, Add, Edit, x12 y40 w130 h20 vName, 
Gui, Add, Text, x12 y20 w130 h20, Name
Gui, Add, Text, x12 y70 w130 h20, Surname
Gui, Add, Edit, x12 y90 w130 h20 vSurname, 
Gui, Add, Text, x12 y120 w130 h20 , Birth Date
Gui, Add, Edit, x12 y140 w130 h20 vBirth, 
Gui, Add, Button, x12 y170 w130 h40 gAdd, Add
Gui, Add, Button, x12 y220 w130 h40 gRemove, Remove

ifexist, %A_ScriptDir%\Content.txt
{
	Fileread, Content, %A_ScriptDir%\Content.txt
	Loop, parse, Content, `n, `r
	{
		StringSplit, i_, A_LoopField, `t
		LV_Add("", i_1, i_2, i_3)
	}
	
}
Gui, Show, w664 h379, Untitled GUI
GuiControl, Focus, Name
return

Add:
Gui, Submit, NoHide
LV_Add("", Name, Surname, Birth)
return

Remove:
LV_Delete(LV_GetNext())
return

SelectionLV:
if (LV_GetNext() < 1 || LV_GetNext() > LV_GetCount())
return
msgbox % Name " - " Surname " - " Birth
return

GuiClose:
ControlGet, ListViewContent, List,, SysListView321
if !(ListViewContent = Content) ; to prevent storing data when nothing has changed
{
	; Filedelete, %A_ScriptDir%\Content.txt
	FileMove, %A_ScriptDir%\Content.txt, %A_ScriptDir%\Content_%a_now%.txt
	sleep 1000
	Fileappend, %ListViewContent%, %A_ScriptDir%\Content.txt, UTF-8
}
exitapp
return
By closing the gui, the content is stored into a text-file in scipt directory.
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

18 Dec 2017, 16:31

divanebaba wrote: SelectionLV:
if (LV_GetNext() < 1 || LV_GetNext() > LV_GetCount())
return
msgbox % Name " - " Surname " - " Birth
return

When I double click on Listview object, MsgBox is empty? Where does the code come from?
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

18 Dec 2017, 17:04

Hallo chngrcn.
I'm sorry, I forgot a bit of code. Please modify like below:

Code: Select all

SelectionLV:
SelItem := LV_GetNext() ; get the number of the activated row
loop 3
Var%A_Index% = ; clearing the Variables to prevent wrong content while clicking not complete filled rows
LV_GetText(Var1, SelItem, 1) ; first column
LV_GetText(Var2, SelItem, 2) ; second column
LV_GetText(Var3, SelItem, 3) ; third column
if (SelItem  < 1 || SelItem > LV_GetCount()) ; since we have stored LV_GetNext() into SelItem, we can use the variable instead of the function.
return
msgbox % Var1 " - " Var2 " - " Var2
return
The fault was forgetting the three lines with LV_GetText(...).
Replacing LV_GetNext() inside the if-statement is only for (maybe) more performance. I think storing and invoking this value had to be faster than invoking LV_GetNext().
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

19 Dec 2017, 01:36

Thank you. there will be another responsibility.
add another button, and when this button is pressed, delete the data in the "edit" boxes and move the cursor to the first box.

with code?

I also want the cursor to be in the first "edit" box when the script starts.
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

19 Dec 2017, 04:24

Code: Select all

Clear:

for i, c in ["Name", "Surname", "Birth"] { ; Clear these edit boxes
%c% := ""
GuiControl,, %c%, % %c%
}
GuiControl, Focus, Name
return
Thank you. I'm find it.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

19 Dec 2017, 10:29

Hallo chngrcn.

To put new content into the controls, you have to use GuiControl.
Look into help file to learn some options round about this command.
For your issue, modify your label, called clear: as described below:

Code: Select all

Clear:
GuiContol,, Name
GuiContol,, Surname
GuiContol,, Birth
GuiControl, Focus, Name
return
You can make it complicated, by using loops or simple like described.
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

20 Dec 2017, 04:25

Code: Select all

Gui, Add, Tab2, x2 y0 w663 h378, Tab1|Tab2
Gui, Tab, Tab1
Gui, Add, ListView, x152 y20 w480 h290  LV0x1 vListViewContent gSelectionLV, Name|Surname|Birth Date
Gui, Add, Edit, x12 y40 w130 h20 vName, 
Gui, Add, Text, x12 y20 w130 h20, Name
Gui, Add, Text, x12 y70 w130 h20, Surname
Gui, Add, Edit, x12 y90 w130 h20 vSurname, 
Gui, Add, Text, x12 y120 w130 h20 , Birth Date
Gui, Add, Edit, x12 y140 w130 h20 vBirth, 
Gui, Add, Button, x12 y170 w130 h40 gAdd, Add
Gui, Add, Button, x12 y220 w130 h40 gRemove, Remove

Gui, Tab, Tab2
Gui, Add, ListView, x152 y20 w480 h290  LV0x1 vListViewContent2 gSelectionLV, Age|Size|Gender
Gui, Add, Edit, x12 y40 w130 h20 vAge, 
Gui, Add, Text, x12 y20 w130 h20, Age
Gui, Add, Text, x12 y70 w130 h20, Size
Gui, Add, Edit, x12 y90 w130 h20 vSize, 
Gui, Add, Text, x12 y120 w130 h20 , Gender
Gui, Add, Edit, x12 y140 w130 h20 vGender, 
Gui, Add, Button, x12 y170 w130 h40 gAdd, Add
Gui, Add, Button, x12 y220 w130 h40 gRemove, Remove

ifexist, %A_ScriptDir%\Content.txt
{
	Fileread, Content, %A_ScriptDir%\Content.txt
	Loop, parse, Content, `n, `r
	{
		StringSplit, i_, A_LoopField, `t
		LV_Add("", i_1, i_2, i_3)
	}
	
}
Gui, Show, w664 h379, Untitled GUI
GuiControl, Focus, Name
return

Add:
Gui, Submit, NoHide
LV_Add("", Name, Surname, Birth)
return

Remove:
LV_Delete(LV_GetNext())
return

SelectionLV:
SelItem := LV_GetNext() ; get the number of the activated row
loop 3
Var%A_Index% = ; clearing the Variables to prevent wrong content while clicking not complete filled rows
LV_GetText(Var1, SelItem, 1) ; first column
LV_GetText(Var2, SelItem, 2) ; second column
LV_GetText(Var3, SelItem, 3) ; third column
if (SelItem  < 1 || SelItem > LV_GetCount()) ; since we have stored LV_GetNext() into SelItem, we can use the variable instead of the function.
return
msgbox % Var1 " - " Var2 " - " Var3
return


GuiClose:
ControlGet, ListViewContent, List,, SysListView321
if !(ListViewContent = Content) ; to prevent storing data when nothing has changed
{
	; Filedelete, %A_ScriptDir%\Content.txt
	FileMove, %A_ScriptDir%\Content.txt, %A_ScriptDir%\Content_%a_now%.txt
	sleep 1000
	Fileappend, %ListViewContent%, %A_ScriptDir%\Content.txt, UTF-8
}
exitapp
return

code (tab1) and tab2 (tab2) are added.
I want shudder;

On the tab1 tab, create the "content1.txt" database for the operations that I have done on that tab.

On the tab2 tab, create the "content2.txt" database that contains the actions that I made on that tab.

when I toggle between tabs, I get a list of the tabs in that list.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

20 Dec 2017, 06:55

Hallo chngrcn
I found a good template here.

EDIT: I changed the GuiClose label. Before it was buggy.
Last edited by divanebaba on 20 Dec 2017, 07:40, edited 1 time in total.
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

20 Dec 2017, 07:35

Thank you very much. divanebaba
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

20 Dec 2017, 09:30

there is a shortage in the "guiclose" section of the encoding.

After running the script after logging on and off on both tabs, it creates "content1" and "content2" files. So far, no problem. If we turn off the scipti and turn it back on again without entering any data, we are changing the names of the "content1" and "content2" files and reloading the existing "content1" and "content2" files.

for turkish

kodlamanın "guiclose" bölümünde bir eksiklik var.

script çalıştırtan sonra her iki sekme'de kayıt yapıp kapattıktan sonra, "content1" ve "content2" dosyalarını oluşturuyor. buraya kadar bir sorun yok. scipti kapatıp yeniden açtığımızda herhangi bir veri girişi yapmadan tekrar kapattığımızda, " "content1" ve "content2" dosyalarının adlarını değiştirip yedekliyor. mevcut "content1" ve "content2" dosyalarını yutuyor. bu sorunu nasıl halledebiliriz ?
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Listview add remove

20 Dec 2017, 09:47

Hallo chngrcn
my turkish is like my english, badly worse.
I've corrigated the code, because storing the contents was faulty. I think you have tried the faulty version.
If there are still any faults, tell me.

Best regards from Germany.
chngrcn
Posts: 190
Joined: 29 Feb 2016, 08:55

Re: Listview add remove

21 Dec 2017, 01:25

Good Morning divanebaba.

There was a backup problem. thank you.
MsJBOX gives warning when we click on empty lines in Listview. I want you to give a warning when we click on only the filled lines. terminate the loop when you click on empty lines.

also, do we have a chance to resize the listview columns in the existing code?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 136 guests