Firstly thanks to the three newbies that contacted me by email, and sorry yes I did not post the final code for the end of the chapter, so here it is.
Just a line to clarify. I feel that most newbies start to look at a programming language to support their hobby mostly looking to produce a database of some kind to keep track of their collections or other data related to their hobby. So I wrote a small tutorial to help with getting started. The final code, built through the tutorial, shows multiple windows with multiple databases the usage of modules and a few other bits with two simple databases one for fruits and one for vegetables. The we start chapter I feel would benefit from an approach like this.
Here is the code:-
First the main form.
Code: Select all
EnableExplicit
IncludeFile "Fruit.pb"
IncludeFile "Vegetable.pb"
;Window Variables
Global MainWindow,FruitWindow.i,VegeWindow.i
;Main Window Gadgets
Global btnFruit.i,btnVegetable.i
Define Event.i
Procedure.i IsValidPBEvent(Event)
Select event
Case #PB_Event_Menu, ; a menu has been selected
#PB_Event_Gadget , ; a gadget has been pushed
#PB_Event_SysTray, ; an icon in the systray zone was clicked
#PB_Event_Timer , ; a timer has reached its timeout
#PB_Event_CloseWindow, ; the window close gadget has been pushed
#PB_Event_Repaint, ; the window content has been destroyed
#PB_Event_SizeWindow, ; the window has been resized
#PB_Event_MoveWindow, ; the window has been moved
#PB_Event_MinimizeWindow, ; the window has been minimized
#PB_Event_MaximizeWindow, ; the window has been maximized
#PB_Event_RestoreWindow, ; the window has been restored To normal size
#PB_Event_ActivateWindow, ; the window has been activated (got the focus)
#PB_Event_DeactivateWindow,; the window has been deactivated (lost the focus)
#PB_Event_WindowDrop, ; a Drag & Drop operation was finished on a window
#PB_Event_GadgetDrop, ; a Drag & Drop operation was finished on a gadget
#PB_Event_RightClick, ; a right mouse button click has occurred on the window.
#PB_Event_LeftClick, ; a left mouse button click has occurred on the window
#PB_Event_LeftDoubleClick ; a left mouse button double-click has occurred on the window
ProcedureReturn #True
Default
ProcedureReturn #False
EndSelect
EndProcedure
Procedure Event_Handler(Event)
Select event
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
Select EventGadget()
Case btnFruit
If Not IsWindow(FruitWindow)
;Show the Fruit window
FruitWindow = Fruit::Open()
Else
;Make Fruit Window Active
SetActiveWindow(FruitWindow)
EndIf
Case btnVegetable
If Not IsWindow(VegeWindow)
;Show the Vegetable window
VegeWindow = Vegetable::Open()
Else
;Make Vegetable Window Active
SetActiveWindow(VegeWindow)
EndIf
EndSelect ;EventGadget()
EndSelect ;Event
EndProcedure
MainWindow = OpenWindow(#PB_Any, 0, 0, 290, 90, "Fruit & Veg", #PB_Window_SystemMenu)
btnFruit = ButtonGadget(#PB_Any, 50, 20, 70, 40, "Fruits")
btnVegetable = ButtonGadget(#PB_Any, 170, 20, 70, 40, "Vegetables")
Repeat
Event = WaitWindowEvent()
If IsValidPBEvent(Event)
Select EventWindow()
Case MainWindow
Event_Handler(Event)
Case FruitWindow
Fruit::Event_Handler(Event)
Case VegeWindow
Vegetable::Event_Handler(Event)
EndSelect ;EventWindow()
EndIf
ForEver
Code: Select all
DeclareModule Fruit
Declare.i Open()
Declare Event_Handler(Event)
EndDeclareModule
Module Fruit
UseSQLiteDatabase()
;This window variable
Global ThisWindow.i
;This windows gadgets
Global btnSearch.i,btnExit.i,cmbSearch.i,strSearch.i
;Database Variable for this module only
Global DBID.i
Procedure LoadCombo()
Define Criteria.s
Criteria = "SELECT * FROM Items; "
If DatabaseQuery(DBID, Criteria)
FirstDatabaseRow(DBID)
AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
While NextDatabaseRow(DBID)
AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
Wend
EndIf
FinishDatabaseQuery(DBID)
EndProcedure
Procedure SearchData(Name.s)
Define Criteria.s
Criteria.s = "SELECT * FROM Items WHERE Name = '" + Name + "';"
If DatabaseQuery(DBID, Criteria)
If FirstDatabaseRow(DBID) = 0
MessageRequester("Error","No Fruits of type " + Name + " Exist in this database")
Else
MessageRequester("Fruits","Fruit " + Name + " Found in this database")
EndIf
EndIf
FinishDatabaseQuery(DBID)
EndProcedure
Procedure.i Open()
ThisWindow = OpenWindow(#PB_Any, 10, 10, 320, 80, "Fruits", #PB_Window_Tool)
cmbSearch = ComboBoxGadget(#PB_Any, 10, 10, 150, 20)
strSearch = StringGadget(#PB_Any, 180, 10, 120, 20, "")
btnSearch = ButtonGadget(#PB_Any, 170, 40, 130, 20, "Search")
btnExit = ButtonGadget(#PB_Any, 10, 40, 150, 20, "Exit")
;Open Fruits Database
DBID = OpenDatabase(#PB_Any,GetCurrentDirectory() + "Fruits.db", "", "")
LoadCombo()
ProcedureReturn ThisWindow
EndProcedure
Procedure Event_Handler(Event.i)
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case btnExit
CloseWindow(ThisWindow)
Case btnSearch
SearchData(GetGadgetText(strSearch))
Case cmbSearch
SearchData(GetGadgetText(cmbSearch))
EndSelect ;EventGadget()
EndSelect ;event
EndProcedure
EndModule
Code: Select all
DeclareModule Vegetable
Declare.i Open()
Declare Event_Handler(Event)
EndDeclareModule
Module Vegetable
UseSQLiteDatabase()
;This window variable
Global ThisWindow.i
;This windows gadgets
Global btnSearch.i,btnExit.i,cmbSearch.i,strSearch.i
;Database Variable for this module only
Global DBID.i
Procedure LoadCombo()
Define Criteria.s
Criteria = "SELECT * FROM Items; "
If DatabaseQuery(DBID, Criteria)
FirstDatabaseRow(DBID)
AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
While NextDatabaseRow(DBID)
AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
Wend
EndIf
FinishDatabaseQuery(DBID)
EndProcedure
Procedure SearchData(Name.s)
Define Criteria.s
Criteria = "SELECT * FROM Items WHERE Name = '" + Name + "';"
If DatabaseQuery(DBID, Criteria)
If FirstDatabaseRow(DBID) = 0
MessageRequester("Error","No Vegetables of type " + Name + " Exist in this database")
Else
MessageRequester("Vegetables","Vegetable " + Name + " Found in this database")
EndIf
EndIf
FinishDatabaseQuery(DBID)
EndProcedure
Procedure.i Open()
ThisWindow = OpenWindow(#PB_Any, 10, 10, 320, 80, "Vegetables", #PB_Window_Tool)
cmbSearch = ComboBoxGadget(#PB_Any, 10, 10, 150, 20)
strSearch = StringGadget(#PB_Any, 180, 10, 120, 20, "")
btnSearch = ButtonGadget(#PB_Any, 170, 40, 130, 20, "Search")
btnExit = ButtonGadget(#PB_Any, 10, 40, 150, 20, "Exit")
;Open Vegetables Database
DBID = OpenDatabase(#PB_Any,GetCurrentDirectory() + "Vegetables.db", "", "")
LoadCombo()
ProcedureReturn ThisWindow
EndProcedure
Procedure Event_Handler(Event.i)
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case btnExit
CloseWindow(ThisWindow)
Case btnSearch
SearchData(GetGadgetText(strSearch))
Case cmbSearch
SearchData(GetGadgetText(cmbSearch))
EndSelect ;EventGadget()
EndSelect ;event
EndProcedure