How handle dates to make a little agenda?

Find quick help here to get you started with Hollywood
amyren
Posts: 87
Joined: Thu May 02, 2019 11:53 am

Re: How handle dates to make a little agenda?

Post by amyren » Thu Jun 13, 2019 9:39 pm

Here is a working example for an addressbook, with the option to export the data as a text file or cvs style.
The user interface is very basic though

Code: Select all

@DISPLAY {Title = "Addressbook", x = 100, y = 100, Width = 480, Height = 620, Color = #WHITE, Noclose = True, Sizeable = True}

; check for previously saved data before loading
addressbook = {}
If Exists("abook.dat") 
	OpenFile(1, "abook.dat", #MODE_READ)
	addressbook = ReadTable(1)
	CloseFile(1)
EndIf

; Will bring up a window with buttons to act as the main menu
Function p_mainmenu()
	sel = SystemRequest("Address Book", "Select action:",
						"Add new entry|Remove entry|Export to text file|Export to CVS|Cancel|Exit")
	Switch sel
	Case 1:
		p_add_entry
		p_save	
		p_showlist
	Case 2:
		p_remove_entry
		p_showlist
		p_save
	Case 3:
		p_export_txt
	Case 4:
		p_export_cvs
	Case 5:
		p_showlist	
	Case 0:	
		quit = True		
	EndSwitch
EndFunction

; Add a new entry to the end of the list
Function p_add_entry()
	InsertItem(addressbook, {name = StringRequest("New entry", "Enter name"), phone = StringRequest("New entry", "Enter phone number"), address = StringRequest("New Entry", "Enter address"), email = StringRequest("New entry", "Enter e-mail")})
EndFunction

; Remove user selected entry
Function p_remove_entry()
	rm$ = StringRequest("Remove entry", "Enter record to remove (between 1 to "..ListItems(addressbook).."):")
	If Val(rm$) > 0 And Val(rm$) < ListItems(addressbook)+1
		RemoveItem(addressbook, (Val(rm$)-1))
	Else SystemRequest("Not removed", "Record not found", "OK", #REQICON_WARNING)
	EndIf	
EndFunction	

; List the addressbook in the main window
Function p_showlist()
	SelectDisplay(1)  
	SetFontColor(#BLACK)
	Cls
	SetMargins(20, 430)
	NPrint("")
	NPrint("Adress book content") 
	For Local i= scroll To ListItems(addressbook)-1
		NPrint("")
		NPrint("Record number: "..i+1)
		NPrint("Name: "..addressbook[i].name)
		NPrint("Phone: "..addressbook[i].phone)
		NPrint("Address: "..addressbook[i].address)
		NPrint("e-mail: "..addressbook[i].email)
	Next
	NPrint("")
	NPrint("Press UP or DOWN to scroll, SPACE for menu.")
	skipmenu = True
EndFunction ; End about routine


; Save the table to a file:
Function p_save()
	OpenFile(1, "abook.dat", #MODE_WRITE)
	WriteTable(1, addressbook)
	CloseFile(1)
EndFunction
	
; export your addressbook to a textfile:
Function p_export_txt()
	OpenFile(2, "abook.txt", #MODE_WRITE)
	For Local i=0 To ListItems(addressbook)-1
		WriteLine(2, i+1)	
		WriteLine(2,"Name:"..addressbook[i].name)
		WriteLine(2,"Phone:"..addressbook[i].phone)
		WriteLine(2,"Address:"..addressbook[i].address)
		WriteLine(2,"e-mail:"..addressbook[i].email)
	Next
	CloseFile(2)
EndFunction

; export it into a cvs-style textfile:
Function p_export_cvs()
	OpenFile(3, "abook.cvs", #MODE_WRITE)
		WriteLine(3,"name;phone;address;email")	
	For Local i=0 To ListItems(addressbook)-1
		WriteLine(3,addressbook[i].name..";"..addressbook[i].phone..";"..addressbook[i].address..";"..addressbook[i].email)
	Next
	CloseFile(3)
EndFunction

 
; This function is for handling keyboard actions
; The skipmenu variable is set to avoid the button-menuwindow to pop up unwanted 
Function p_HandlerFunc(msg)
	Switch(msg.action)
	Case "OnKeyDown":
		If scroll > 0	
			If msg.key = "UP" 
				scroll = scroll-1
				p_showlist
				skipmenu = True
			EndIf	
		EndIf
		If scroll < ListItems(addressbook)-1	
			If msg.key = "DOWN" 
				scroll = scroll+1
				p_showlist
				skipmenu = True
			EndIf	
		EndIf	
		If msg.key = " "
			skipmenu = False
		EndIf	
	EndSwitch
EndFunction

scroll=0
skipmenu = False
p_showlist

; create an event when window is resized or keyboard is used 
InstallEventHandler({SizeWindow = p_showlist, OnKeyDown = p_HandlerFunc})

Repeat
	WaitEvent
	If skipmenu = False
		p_mainmenu()
	EndIf	
Until quit = True

Post Reply