Page 1 of 1

Use DBase file to set button text

Posted: Sun Nov 04, 2007 10:24 pm
by Rook Zimbabwe
Well i was writing an APP and I solved a bit of this with some fantastic help so I thought I would share:

This is example code for PBasic 4.02

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Button_IT0
  #Button_IT1
  #Button_IT2
  #Button_IT3
  #Button_SEL0
  #Button_SEL1
  #Button_SEL2
  #Button_SEL3
  #Button_SEL4
  #Button_SEL5
  #Button_SEL6
  #Button_SEL7
  #Button_SEL8
  #String_WHAT
  #Button_SELCLR
  #Database
EndEnumeration

Global what
Global who$
Global FUBAR$

Global Dim ITEMS$(4)
Global Dim SELS$(9)

;- DBase STUFF FROM PB SAMPLES CODE

#ODBC_ADD_DSN    = 1    
#ODBC_CONFIG_DSN = 2 
#ODBC_REMOVE_DSN = 3 

Define Result

Dim DatabaseType.s(4) 
DatabaseType(0) = "Unknown" 
DatabaseType(1) = "Numeric" 
DatabaseType(2) = "String" 
DatabaseType(3) = "Float" 

Declare AddConnection(Driver.s,ConnectString.s) 
Declare RemoveConnection(Driver.s,DSN.s) 

; ** CURRENTLY USING A PREFS FILE TO LIST TABLES

OpenPreferences("Menu1.pref"); Pref stuff is from the examples too
  PreferenceGroup("Global")
    For i = 0 To 4
          se$ = Str(i)
        ITEMS$(0+i) = ReadPreferenceString("SEL"+se$, "")
    Next
ClosePreferences()

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 596, 86, 349, 236, "New window ( 0 )",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ButtonGadget(#Button_IT0, 10, 10, 90, 30, "")
      ButtonGadget(#Button_IT1, 10, 45, 90, 30, "")
      ButtonGadget(#Button_IT2, 10, 80, 90, 30, "")
      ButtonGadget(#Button_IT3, 10, 115, 90, 30, "")
      ButtonGadget(#Button_SEL0, 120, 10, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL1, 190, 10, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL2, 120, 60, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL3, 190, 60, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL4, 120, 110, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL5, 190, 110, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL6, 260, 10, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL7, 260, 60, 70, 40, "",#PB_Button_MultiLine)
      ButtonGadget(#Button_SEL8, 260, 110, 70, 40, "",#PB_Button_MultiLine)
      StringGadget(#String_WHAT, 10, 160, 330, 20, "")
      ButtonGadget(#Button_SELCLR, 10, 190, 100, 30, "CLEAR")
      
    EndIf
  EndIf
EndProcedure

Procedure FUBAR(what)
; ********** This just reads the button gadget and plops it in the string gadget
      SetGadgetText(#String_WHAT, "")
        FUBAR$ = GetGadgetText(what)
          SetGadgetText(#String_WHAT, FUBAR$)

EndProcedure

Procedure AddConnection(Driver.s,ConnectString.s) 
    Define Result
    Result=SQLConfigDataSource_(#Database,#ODBC_ADD_DSN,Driver,ConnectString ) 
    If Result
      ProcedureReturn 1 
    EndIf
     
EndProcedure 

Procedure RemoveConnection(Driver.s,DSN.s) 
    Define Resux
    Resux=SQLConfigDataSource_(#Database,#ODBC_REMOVE_DSN,Driver,"DSN="+DSN) 
    If Resux
      ProcedureReturn 1 
    EndIf
     
EndProcedure 

Procedure DBQueryITEM(what)

Z = 0 ; to be used later so make sure value is cleared

Dim ITEMS$(9) ; Redims array which resets it

For slappy = 0 To 8 ; do this 9 times
  DisableGadget(#Button_sel0+slappy, 0) ; reenable all buttons before starting
Next

 menu$ = GetGadgetText(what) ; get ITEM button pressed for NAME value  
 DatabaseQuery(#Database, "SELECT * FROM "+menu$+"';") ; read the table according to button name

    While NextDatabaseRow(#Database) 
      buttext$=GetDatabaseString(#Database, 1) ; FOOD NAME COLUMN
        SetGadgetText(#Button_SEL0 + Z, buttext$) ;
           golly$ = GetGadgetText(#Button_SEL0 + Z)
              If golly$ = "INACTIVE" ; ********** my way to turn off buttons
                 DisableGadget(#Button_SEL0 + Z, 1) ; kill the inactive buttons
              EndIf
      Z = Z + 1
    Wend

EndProcedure

Procedure ClearSELBUT()
  ; idea by Comtois
  ; you need to make sure your esigner has these buttons listed in ASCENDING ORDER 
  ; or you will get a #Gadget ERROR
  
  For x = #Button_SEL0 To #Button_SEL8 ; this way you kill 2 birds with 1 loop...
    SetGadgetText(x, "") 
  Next

EndProcedure

; ****** READ THE DB FILE
File$ = "menu1.mdb"
Result=AddConnection("Microsoft Access Driver (*.mdb)","Server=127.0.0.1; Description=MyDescription ;DSN=MyDSN;DBQ="+File$+";UID=;PWD=;") 
  InitDatabase() 
    db = OpenDatabase(#Database, "MyDSN", "", "") 
;******

Open_Window_0() ; you have to open the window before you can play with the buttons

; **** LOAD THE ITEM BUTTONS WITH TEXT FROM PREF FILE
For S = #Button_IT0 To #Button_IT3
  SetGadgetText(S, ITEMS$(Z))
     Molly$ = GetGadgetText(#Button_IT0+Z)
       If Molly$ = "INACTIVE" ; ********** USE THE INACTIVATOR CODE
           DisableGadget(#Button_IT0+Z, 1)
       EndIf
    Z = Z + 1
Next


Repeat ; Start of the event loop
  
  Event = WaitWindowEvent() ; This line waits until an event is received from Windows
  
  WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
  
  GadgetID = EventGadget() ; Is it a gadget event?
  
  EventType = EventType() ; The event type
  
  ;You can place code here, and use the result as parameters for the procedures
  
  If Event = #PB_Event_Gadget
    
    If GadgetID = #Button_IT0
      clearSELBUT() ; clean off the text in the buttons
        DBQueryITEM(#Button_IT0) ; send #Button_IT0 to  DBQuery procedure
    ElseIf GadgetID = #Button_IT1
      clearSELBUT()
        DBQueryITEM(#Button_IT1)
    ElseIf GadgetID = #Button_IT2
      clearSELBUT()
        DBQueryITEM(#Button_IT2)
    ElseIf GadgetID = #Button_IT3
      clearSELBUT()
        DBQueryITEM(#Button_IT3)
    ElseIf GadgetID = #Button_SEL0
      FUBAR(#Button_SEL0)
    ElseIf GadgetID = #Button_SEL1
      FUBAR(#Button_SEL1)
    ElseIf GadgetID = #Button_SEL2
      FUBAR(#Button_SEL2)   
    ElseIf GadgetID = #Button_SEL3
      FUBAR(#Button_SEL3)
    ElseIf GadgetID = #Button_SEL4
      FUBAR(#Button_SEL4)     
    ElseIf GadgetID = #Button_SEL5
      FUBAR(#Button_SEL5)
    ElseIf GadgetID = #Button_SEL6
      FUBAR(#Button_SEL6)
    ElseIf GadgetID = #Button_SEL7
      FUBAR(#Button_SEL7)
    ElseIf GadgetID = #Button_SEL8
      FUBAR(#Button_SEL8)
    ElseIf GadgetID = #String_WHAT
      ; nothing for us to play with here really
    ElseIf GadgetID = #Button_SELCLR
      ClearSELBUT()
    EndIf
    
  EndIf
  
Until Event = #PB_Event_CloseWindow ; End of the event loop

End
;
You are going to need 2 other files: menu.pref and menu.mdb

menu1.pref just contains a list of 1 - 4 words. My file looks like this:

Code: Select all

[Global]
SEL0 = BURGER
SEL1 = PIZZA
SEL2 = DRINK
SEL3 = INACTIVE
;
I saved this in the same directory as the saved program. I was careful to use NOTEPAD and tell notepad to SAVE AS and select ALL FILES *.* from the dropdown box so notepad would not append a .txt to my menu.pref file... If yours does not work try changing the filename or the name in the program to menu.pref.txt

pref file in same ZIP

The mdb program has a table for every line in the pref file ** EXACTLY THE SAME NAME **

I am including a sample download her for a while on my site but I am taking it down soon!

http://bluemesapc.com/menu1.zip

This also needs to be in the same folder as the saved program and the pref file.

I think there is a way with PB 4.1 to get table names from DB file... that will make the pref file obsolete and speed up the process...

I used the basis of this for a POS APP and I have ohter things in these tables like COST and COG and stuff...

Good luck! 8)