rewrite dbf01.prg using code

rewrite dbf01.prg using code

Postby Ehab Samir Aziz » Sat Jul 28, 2007 4:11 am

I am trying to rewrite the sample dbf01.prg from code not from resources but I face problem with dlg:update() as no change hapnneded with buttons change and with settext() method I have syntax error.
Code: Select all  Expand view  RUN
// FiveWin - DataBase Objects Tutorial - 01
// FiveWin DataBase tools

#include "FiveWin.ch"
#include "Dbfown.ch"

static oWnd

//----------------------------------------------------------------------------//

function Main()

   SET _3DLOOK ON

   DEFINE WINDOW oWnd FROM 2, 2 TO 20, 70 ;
      TITLE "Testing DataBase Objects" ;
      MENU BuildMenu()

   SET MESSAGE OF oWnd ;
      TO "FiveWin - Object Oriented DataBase Management" CENTERED

   oWnd:bLDblClick = { || MsgInfo( "Double Left Click" ) }

   ACTIVATE WINDOW oWnd MAXIMIZED ;
      VALID MsgYesNo( "Do you really want to end ?" )

return nil

//----------------------------------------------------------------------------//

function BuildMenu()

   local oMenu

   MENU oMenu
      MENUITEM "&Clients"
      MENU
         MENUITEM "&Record by Record..." ACTION ClientsRec() ;
            MESSAGE "Clients record by record management dialog"

         MENUITEM "&Browse..." ACTION ClientsBrowse() ;
            MESSAGE "Clients browse management dialog"

         SEPARATOR

         MENUITEM "&Exit Test..."  ACTION oWnd:End() ;
            MESSAGE "End doing this tutorial"
      ENDMENU
   ENDMENU

return oMenu

//----------------------------------------------------------------------------//

function ClientsRec()

   local oDbf, oDlg
   local oBtnNew:="&New", oBtnTop:="&Top", ;
   oBtnPrev:="&Previous", oBtnNext:="&Next", oBtnBottom:="&bottom", oBtnEnd:="&End"
   local aButtons
   local lNew := .f.

   USE Customer

   DATABASE oDbf        // We create a DBF Object based on the current Alias
                        // Now we can change WorkArea and the DBF Object
                        // automatically access its area !

   oDbf:bEoF = nil      // We don't want default EOF processing

DEFINE DIALOG oDlg FROM 8, 2 TO 600, 700 PIXEL ;
TITLE ("Customer" )

   @ 1,2 SAY "&First" OF oDlg PIXEL
   @ 1,50 SAY ":" OF oDlg PIXEL
   @ 1,60 GET oDbf:First OF oDlg PIXEL

   @ 15,1 SAY "&Name" OF oDlg PIXEL
   @ 15,50 SAY ":" OF oDlg PIXEL
   @ 15,60 GET oDbf:Last OF oDlg PIXEL


   @ 280, 10 BUTTON oBtnNew  OF oDlg PIXEL SIZE 30, 12 ;
      ACTION  New( @lNew, oDbf, aButtons, oDlg ) ;
      MESSAGE "Add a new customer"

   @ 280, 50 BUTTON oBtnTop  OF oDlg PIXEL SIZE 30, 12 ;
      ACTION  Top( @lNew, oDbf, aButtons, oDlg ) ;
      MESSAGE "Go to the first record of the DataBase"

   @ 280, 100 BUTTON oBtnPrev OF oDlg PIXEL SIZE 30, 12 ;
      ACTION ( oDbf:Skip( -1 ), oDlg:Update() ) ;
      MESSAGE "Go to the previous record of the DataBase"

   @ 280, 150 BUTTON oBtnNext OF oDlg PIXEL SIZE 30, 12 ;
      ACTION ( oDbf:Skip(),;
               If( oDbf:Eof(), ( MsgStop( "EoF" ), oDbf:GoBottom() ),;
               oDlg:Update() ) ) ;
      MESSAGE "Go to the next record of the DataBase"

   @ 280, 200 BUTTON oBtnBottom OF oDlg PIXEL SIZE 30, 12 ;
      ACTION ( oDbf:GoBottom(), oDlg:Update() ) ;
      MESSAGE "Go to the last record of the DataBase"

   @ 280, 250 BUTTON oBtnEnd OF oDlg PIXEL SIZE 30, 12 ;
      ACTION oDlg:End() ;
      MESSAGE "End this dialog"

   

   aButtons = { oBtnNew, oBtnTop, oBtnPrev, oBtnNext, oBtnBottom, oBtnEnd }

   ACTIVATE DIALOG oDlg CENTERED ;
         ON INIT ( oDlg:Update() )


   USE

return nil

//----------------------------------------------------------------------------//

function New( lNew, oDbf, aButtons, oDlg )

   local n

   if ! lNew
      aButtons[ 1 ]:SetText( "&Save" )
      aButtons[ 2 ]:SetText( "&Cancel" )
      for n = 3 to Len( aButtons )
         aButtons[ n ]:Disable()
      next
      oDbf:Blank()   // we 'clean' the DataBase buffer
      oDlg:Update()  // we refresh the controls
      oDlg:aControls[ 1 ]:SetFocus()  // we give focus to the first GET
   else
      oDbf:Append()  // The user selected 'save'
      oDbf:Save()    // we copy from the DataBase buffer into the 'real' DBF
      aButtons[ 1 ]:SetText( "&New" )
      aButtons[ 2 ]:SetText( "&Top" )
      for n = 3 to Len( aButtons )
    aButtons[ n ]:Enable()
      next
   endif

   lNew = ! lNew     // we toggle the lNew value

return nil

//----------------------------------------------------------------------------//

function Top( lNew, oDbf, aButtons, oDlg )

   local n

   if ! lNew      // the user wants to GO TOP
      oDbf:GoTop()
      oDlg:Update()   // we repaint the controls containts

   else       // the user canceled the 'New' option
      oDbf:Load()   // reload again fields info from the DBF
      oDlg:Update()   // we repaint the controls contains
      aButtons[ 1 ]:SetText( "&New" )
      aButtons[ 2 ]:SetText( "&Top" )
      for n = 3 to Len( aButtons )
    aButtons[ n ]:Enable()
      next
      lNew = .f.
   endif

return nil

//----------------------------------------------------------------------------//

function ClientsBrowse()

   local oDlg, oDbf, oBrw, oFnt

   USE customer
   DATABASE oDbf

   DEFINE DIALOG oDlg RESOURCE "Browse"

   REDEFINE LISTBOX oBrw ;
      FIELDS oDbf:first, oDbf:last ;
      HEADERS "first",   "last" ;
      SIZES   250, 300 ;
      ID 110 OF oDlg

   // We change the skip codeblock of the browse, to be managed
   // by the DataBase object skip method, so we can directly use
   // oDbf:Name and oDbf:Adress in the above FIELDS ... clause

   oBrw:bSkip = { | nRecs | oDbf:Skipper( nRecs ) }

   oBrw:lAutoEdit  = .T.

   oBrw:cToolTip = "This is a test"

   ACTIVATE DIALOG oDlg CENTERED

   USE

return nil

//----------------------------------------------------------------------------//

procedure AppSys   // XBase++ requirement

return

//----------------------------------------------------------------------------//
Ehab Samir Aziz
 
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Postby Antonio Linares » Sat Jul 28, 2007 7:18 am

Ehab,

This is the right way to do it:
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

static oWnd

//----------------------------------------------------------------------------//

function Main()

   SET _3DLOOK ON

   DEFINE WINDOW oWnd FROM 2, 2 TO 20, 70 ;
      TITLE "Testing DataBase Objects" ;
      MENU BuildMenu()

   SET MESSAGE OF oWnd ;
      TO "FiveWin - Object Oriented DataBase Management" CENTERED

   oWnd:bLDblClick = { || MsgInfo( "Double Left Click" ) }

   ACTIVATE WINDOW oWnd MAXIMIZED ;
      VALID MsgYesNo( "Do you really want to end ?" )

return nil

//----------------------------------------------------------------------------//

function BuildMenu()

   local oMenu

   MENU oMenu
      MENUITEM "&Clients"
      MENU
         MENUITEM "&Record by Record..." ACTION ClientsRec() ;
            MESSAGE "Clients record by record management dialog"

         MENUITEM "&Browse..." ACTION ClientsBrowse() ;
            MESSAGE "Clients browse management dialog"

         SEPARATOR

         MENUITEM "&Exit Test..."  ACTION oWnd:End() ;
            MESSAGE "End doing this tutorial"
      ENDMENU
   ENDMENU

return oMenu

//----------------------------------------------------------------------------//

function ClientsRec()

   local oDbf, oDlg
   local oBtnNew, oBtnTop, oBtnPrev, oBtnNext, oBtnBottom, oBtnEnd
   local aButtons
   local lNew := .f.

   USE Customer

   DATABASE oDbf        // We create a DBF Object based on the current Alias
                        // Now we can change WorkArea and the DBF Object
                        // automatically access its area !

   oDbf:bEoF = nil      // We don't want default EOF processing

   DEFINE DIALOG oDlg FROM 8, 2 TO 600, 700 PIXEL ;
      TITLE ("Customer" )

   @ 1,2 SAY "&First" OF oDlg PIXEL
   @ 1,50 SAY ":" OF oDlg PIXEL
   @ 1,60 GET oDbf:First OF oDlg PIXEL UPDATE

   @ 15,1 SAY "&Name" OF oDlg PIXEL
   @ 15,50 SAY ":" OF oDlg PIXEL
   @ 15,60 GET oDbf:Last OF oDlg PIXEL UPDATE


   @ 280, 10 BUTTON oBtnNew PROMPT "&New" OF oDlg PIXEL SIZE 30, 12 ;
      ACTION  New( @lNew, oDbf, aButtons, oDlg ) ;
      MESSAGE "Add a new customer"

   @ 280, 50 BUTTON oBtnTop PROMPT "&Top" OF oDlg PIXEL SIZE 30, 12 ;
      ACTION  Top( @lNew, oDbf, aButtons, oDlg ) ;
      MESSAGE "Go to the first record of the DataBase"

   @ 280, 100 BUTTON oBtnPrev PROMPT "&Previous" OF oDlg PIXEL SIZE 30, 12 ;
      ACTION ( oDbf:Skip( -1 ), oDlg:Update() ) ;
      MESSAGE "Go to the previous record of the DataBase"

   @ 280, 150 BUTTON oBtnNext PROMPT "&Next" OF oDlg PIXEL SIZE 30, 12 ;
      ACTION ( oDbf:Skip(),;
               If( oDbf:Eof(), ( MsgStop( "EoF" ), oDbf:GoBottom() ),;
               oDlg:Update() ) ) ;
      MESSAGE "Go to the next record of the DataBase"

   @ 280, 200 BUTTON oBtnBottom PROMPT "&Bottom" OF oDlg PIXEL SIZE 30, 12 ;
      ACTION ( oDbf:GoBottom(), oDlg:Update() ) ;
      MESSAGE "Go to the last record of the DataBase"

   @ 280, 250 BUTTON oBtnEnd PROMPT "&End" OF oDlg PIXEL SIZE 30, 12 ;
      ACTION oDlg:End() ;
      MESSAGE "End this dialog"

   aButtons := { oBtnNew, oBtnTop, oBtnPrev, oBtnNext, oBtnBottom, oBtnEnd }

   ACTIVATE DIALOG oDlg CENTERED ;
         ON INIT ( oDlg:Update() )


   USE

return nil

//----------------------------------------------------------------------------//

function New( lNew, oDbf, aButtons, oDlg )

   local n

   if ! lNew
      aButtons[ 1 ]:SetText( "&Save" )
      aButtons[ 2 ]:SetText( "&Cancel" )
      for n = 3 to Len( aButtons )
         aButtons[ n ]:Disable()
      next
      oDbf:Blank()   // we 'clean' the DataBase buffer
      oDlg:Update()  // we refresh the controls
      oDlg:aControls[ 1 ]:SetFocus()  // we give focus to the first GET
   else
      oDbf:Append()  // The user selected 'save'
      oDbf:Save()    // we copy from the DataBase buffer into the 'real' DBF
      aButtons[ 1 ]:SetText( "&New" )
      aButtons[ 2 ]:SetText( "&Top" )
      for n = 3 to Len( aButtons )
    aButtons[ n ]:Enable()
      next
   endif

   lNew = ! lNew     // we toggle the lNew value

return nil

//----------------------------------------------------------------------------//

function Top( lNew, oDbf, aButtons, oDlg )

   local n

   if ! lNew      // the user wants to GO TOP
      oDbf:GoTop()
      oDlg:Update()   // we repaint the controls containts

   else       // the user canceled the 'New' option
      oDbf:Load()   // reload again fields info from the DBF
      oDlg:Update()   // we repaint the controls contains
      aButtons[ 1 ]:SetText( "&New" )
      aButtons[ 2 ]:SetText( "&Top" )
      for n = 3 to Len( aButtons )
    aButtons[ n ]:Enable()
      next
      lNew = .f.
   endif

return nil

//----------------------------------------------------------------------------//

function ClientsBrowse()

   local oDlg, oDbf, oBrw, oFnt

   USE customer
   DATABASE oDbf

   DEFINE DIALOG oDlg RESOURCE "Browse"

   REDEFINE LISTBOX oBrw ;
      FIELDS oDbf:first, oDbf:last ;
      HEADERS "first",   "last" ;
      SIZES   250, 300 ;
      ID 110 OF oDlg

   // We change the skip codeblock of the browse, to be managed
   // by the DataBase object skip method, so we can directly use
   // oDbf:Name and oDbf:Adress in the above FIELDS ... clause

   oBrw:bSkip = { | nRecs | oDbf:Skipper( nRecs ) }

   oBrw:lAutoEdit  = .T.

   oBrw:cToolTip = "This is a test"

   ACTIVATE DIALOG oDlg CENTERED

   USE

return nil

//----------------------------------------------------------------------------//
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Ehab Samir Aziz » Sat Jul 28, 2007 8:25 am

Sorry I also rewrite the ClientsBrowse() with error :

No exported method: BLCLICKED

Code: Select all  Expand view  RUN
function ClientsBrowse()

   local oDlg, oDbf, oBrw, oFnt

   USE customer
   DATABASE oDbf
   
    @ 10,20 LISTBOX oBrw;
      FIELDS oDbf:first, oDbf:last ;
      HEADERS "first",   "last" ;
      SIZE   250, 300  of oDlg
   
   // We change the skip codeblock of the browse, to be managed
   // by the DataBase object skip method, so we can directly use
   // oDbf:Name and oDbf:Adress in the above FIELDS ... clause

   oBrw:bSkip = { | nRecs | oDbf:Skipper( nRecs ) }

   oBrw:lAutoEdit  = .T.

   oBrw:cToolTip = "This is a test"

   ACTIVATE DIALOG oDlg CENTERED

   USE

return nil
Ehab Samir Aziz
 
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Postby Antonio Linares » Sat Jul 28, 2007 8:37 am

You are missing DEFINE DIALOG. This is the right code:
Code: Select all  Expand view  RUN
function ClientsBrowse()

   local oDlg, oDbf, oBrw, oFnt

   USE customer
   DATABASE oDbf

   DEFINE DIALOG oDlg FROM 5, 5 TO 20, 80 TITLE "Browse"

    @ 1, 2 LISTBOX oBrw;
      FIELDS oDbf:first, oDbf:last ;
      HEADERS "first",   "last" ;
      SIZE 250, 100  of oDlg
   
   // We change the skip codeblock of the browse, to be managed
   // by the DataBase object skip method, so we can directly use
   // oDbf:Name and oDbf:Adress in the above FIELDS ... clause

   oBrw:bSkip = { | nRecs | oDbf:Skipper( nRecs ) }

   oBrw:lAutoEdit  = .T.

   oBrw:cToolTip = "This is a test"

   ACTIVATE DIALOG oDlg CENTERED

   USE

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 51 guests