Save and restore arrays

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

RPC
Posts: 167
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF
Has thanked: 57 times
Been thanked: 8 times

Save and restore arrays

Post by RPC » Fri Feb 14, 2020 9:24 am

I am trying to save and restore check box state in arrays.
There is a check box which can check/uncheck all remaining check boxes. What I want is if I check one or more of the remaining check boxes(in the enclosed example 3), I should be able to save the state of these check boxes and when the program is run again the same state be restored. For eg to check 2nd box I will first uncheck all(by clicking on Sel/Unsel all check box and then click 2nd check box. After this I will press Save & Exit button.
SaveRestore.png
SaveRestore.png (1.16 MiB) Viewed 193 times
When I start my program again I expect to have only 2nd check box checked, but I find all the boxes unchecked because Sel/Unsel all check box was unchecked.
SaveRestore2.png
SaveRestore2.png (1.16 MiB) Viewed 193 times

Is there a way to have the state of check boxes restored as it was when saved.
It seems when ACTIVATE WINDOW ... is executed it executes function SelectAll() which causes this.
Please help.
Thanks

User avatar
serge_girard
Posts: 2403
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 689 times
Been thanked: 131 times
Contact:

Post by serge_girard » Fri Feb 14, 2020 9:51 am

Hello,

I suggust to use an INI file from where you restore and save to. I have a sample but cannot find it right now.

Serge

User avatar
serge_girard
Posts: 2403
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 689 times
Been thanked: 131 times
Contact:

Post by serge_girard » Fri Feb 14, 2020 9:55 am

Code: Select all

      FOR A = 1 TO 10
         c1 := 'C' + ALLTRIM(STR(A))
         SET SECTION c1          ENTRY c1       TO ''
      NEXT

// and GET

      FOR A = 1 TO 10
         c1 := 'C' + ALLTRIM(STR(A))
         GET c1 SECTION c1          ENTRY c1     
         AADD(aCONTR_AD, c1 )
      NEXT

This way !

RPC
Posts: 167
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF
Has thanked: 57 times
Been thanked: 8 times

Post by RPC » Fri Feb 14, 2020 10:34 am

Thanks Serge
I will try your suggestion.
I think problem is not with restoring array values as such. The problem is SelAll() function gets executed when window is activated which causes all the check boxes to be set to .f. since Sel/Unsel check box was set to .f. when saved.
Still I will try INI file method and see if it helps.
Thanks
PS:Sorry I forgot to enclose my program earlier. Perhaps this will help to understand my problem better.
Attachments
testmemvar.zip
(2.56 KiB) Downloaded 11 times

User avatar
AUGE_OHR
Posts: 547
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany
Has thanked: 68 times
Been thanked: 121 times

Post by AUGE_OHR » Fri Feb 14, 2020 10:48 am

hi,

what Type is your Array where your try to save "State" of Checkbox :?:

i use a Field-Wide STATIC on Top of Form to hold last Result

Code: Select all

STATIC cSeek := ""
Function Blabla()
   DEFINE WINDOW ...
      // Code     
   END WINDOW
next Time when call Function Blabla() i still have cSeek.

same Technique can be done with Array so that i a possible Way

---

you say "next time" and want "same Screen" ... so what about hide() / show() it :idea:
if those Window are Part of a Form i "o.Release" before Form.Release.

i´m not sure how harbour / HMG will "clean up" when Create/Release.
i might be a Problem when App run 24/7 with Resource Handle
have fun
Jimmy

User avatar
gfilatov
Posts: 698
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Has thanked: 25 times
Been thanked: 283 times
Contact:

Post by gfilatov » Fri Feb 14, 2020 11:09 am

RPC wrote:
Fri Feb 14, 2020 10:34 am
Thanks Serge
I will try your suggestion.
I think problem is not with restoring array values as such. The problem is SelAll() function gets executed when window is activated which causes all the check boxes to be set to .f. since Sel/Unsel check box was set to .f. when saved.
Still I will try INI file method and see if it helps.
Thanks
PS:Sorry I forgot to enclose my program earlier. Perhaps this will help to understand my problem better.
Hi,

Please try the updated working sample below:

Code: Select all

#include "HMG.CH"
FUNCTION main()

PRIVATE cVars, aVars := {}, cSetFile1, cSetFile2, lInit := .F.

aVars := array( 4, 2 )

DEFINE WINDOW TESTMEMVAR MAIN AT 20,20 WIDTH 200 HEIGHT 200 
       DEFINE CHECKBOX checkbox_0
	          ROW 10
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE .t.
			  CAPTION "Select/Unselect All"
			  ONCHANGE iif(!lInit,SelectAll(),)
        END CHECKBOX

       DEFINE CHECKBOX checkbox_1
	          ROW 30
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE .t.
			  CAPTION "Check 1"
        END CHECKBOX

       DEFINE CHECKBOX checkbox_2
	          ROW 50
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE .t.
			  CAPTION "Check 2"
        END CHECKBOX
		
       DEFINE CHECKBOX checkbox_3
	          ROW 70
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE .t.
			  CAPTION "Check 3"
        END CHECKBOX

		DEFINE BUTTON button_1
		      ROW 100
			  COL 30
			  WIDTH 120
			  HEIGHT 30
			  CAPTION "SAVE && EXIT"
              ACTION saveVars()
			  PICTURE "save"
			  PICTALIGNMENT LEFT
		END BUTTON	  
END WINDOW
RESTVARS()
CENTER WINDOW TESTMEMVAR
ACTIVATE WINDOW TESTMEMVAR

RETURN


FUNCTION saveVars()

MEMVAR aVars, cVSel, cSetFile1

fillvars()

cSetFile1 := getstartupfolder()+'\memvarfile1.mem'

cVars := HB_Serialize(aVars)
HB_MvSave(cSetFile1, "cVars")

thiswindow.release

RETURN nil

FUNCTION restvars()
MEMVAR cSetFile1, cVars, aVars

cSetFile1 := getstartupfolder()+'\memvarfile1.mem'

if file(cSetFile1)
   HB_MvRestore(cSetFile1, .t.)
   aVars := HB_DeSerialize( cVars )
lInit := .T.
   TESTMEMVAR.checkBOX_1.value := aVars[ 1, 2 ]   
   TESTMEMVAR.checkBOX_2.value := aVars[ 2, 2 ]
   TESTMEMVAR.checkBOX_3.value := aVars[ 3, 2 ]
   TESTMEMVAR.checkBOX_0.value := aVars[ 4, 2 ]
lInit := .F.
endif   
RETURN nil

FUNCTION fillvars()
    aVars[ 1, 1 ] := "CHECK1"
	aVars[ 2, 1 ] := "CHECK2"
	aVars[ 3, 1 ] := "CHECK3"	
	aVars[ 4, 1 ] := "Sel/Unsel"
    aVars[ 1, 2 ] := testmemvar.checkbox_1.value
    aVars[ 2, 2 ] := testmemvar.checkbox_2.value
    aVars[ 3, 2 ] := testmemvar.checkbox_3.value
	aVars[ 4, 2 ] := testmemvar.checkbox_0.value
RETURN	

FUNCTION SelectAll()
MEMVAR aVars
if testmemvar.checkbox_0.value == .t.
   testmemvar.checkbox_1.value := .t.
   testmemvar.checkbox_2.value := .t.
   testmemvar.checkbox_3.value := .t.
   for i = 1 to 3
       aVars[ i, 2 ] := .t.
   Next
elseif testmemvar.checkbox_0.value == .f.   
   testmemvar.checkbox_1.value := .f.
   testmemvar.checkbox_2.value := .f.
   testmemvar.checkbox_3.value := .f.
if !lInit
   for i = 1 to 3
       aVars[ i, 2 ] := .f.
   Next
endif   
endif   
RETURN nil
Hope that helps :idea:
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein

User avatar
mustafa
Posts: 882
Joined: Fri Mar 20, 2009 11:38 am
DBs Used: DBF
Location: Alicante - Spain
Been thanked: 147 times
Contact:

Post by mustafa » Fri Feb 14, 2020 12:29 pm

Hello friends:
Another option based on the sample of Master Grigory
working with the files -> "mem"

Code: Select all

#include "HMG.CH"

FUNCTION main()

  PRIVATE xeck0 ,  xeck1 ,  xeck2 ,  xeck3

  IF !FILE('xeckyn.mem')
     xeck0  := .T.
     xeck1  := .T.
     xeck2  := .T.
     xeck3  := .T.
     SAVE TO xeckyn.mem ALL LIKE  xeck*
  ENDIF

 DEFINE WINDOW Form_1 MAIN AT 20,20 WIDTH 200 HEIGHT 200 NOSIZE NOMAXIMIZE
 
  RESTORE FROM xeckyn.mem ADDITIVE
 
                  DEFINE CHECKBOX checkbox_0
	                  ROW 10
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE xeck0  //.t.
			  CAPTION "Select/Unselect All"
                          ONCHANGE ReverseCheck()
                   END CHECKBOX

                   DEFINE CHECKBOX checkbox_1
	                  ROW 30
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE  xeck1  //.t.
			  CAPTION "Check 1"
                   END CHECKBOX

                   DEFINE CHECKBOX checkbox_2
	                  ROW 50
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE  xeck2  //.t.
			  CAPTION "Check 2"
                   END CHECKBOX
		
                   DEFINE CHECKBOX checkbox_3
	                  ROW 70
			  COL 30
			  WIDTH 120
			  HEIGHT 20
			  VALUE  xeck3  //.t.
			  CAPTION "Check 3"
                   END CHECKBOX

		   DEFINE BUTTON button_1
		          ROW 100
			  COL 30
			  WIDTH 120
			  HEIGHT 30
			  CAPTION "SAVE && EXIT"
                          ACTION SaveXecky()  
			  PICTURE "save"
			  PICTALIGNMENT LEFT
		   END BUTTON	  
    END WINDOW


  CENTER WINDOW Form_1
  ACTIVATE WINDOW Form_1

RETURN


*----------------------------------*
  FUNCTION ReverseCheck()
*----------------------------------*
  LOCAL aChecboQ , i

   aChecboQ  := ( IIF( Form_1.checkbox_0.Value,"True","False") )
 
   IF aChecboQ = "True"  
     
     Form_1.checkbox_0.Value := .T.
     Form_1.checkbox_1.Value := .T.
     Form_1.checkbox_2.Value := .T.
     Form_1.checkbox_3.Value := .T.
     xeck0  :=   .T.
     xeck1  :=   .T.
     xeck2  :=   .T.
     xeck3  :=   .T.

     SAVE TO xeckyn.mem ALL LIKE  xeck*

   ELSE
         
     Form_1.checkbox_0.Value := .F.
     Form_1.checkbox_1.Value := .F.
     Form_1.checkbox_2.Value := .F.
     Form_1.checkbox_3.Value := .F.

     xeck0  :=   .F.
     xeck1  :=   .F.
     xeck2  :=   .F.
     xeck3  :=   .F.

     SAVE TO xeckyn.mem ALL LIKE  xeck*
 
  ENDIF

RETURN NIL

*----------------------------------*
  FUNCTION SaveXecky()
*----------------------------------*

   LOCAL bChec00 , bChec01 , bChec02 , bChec03 

   bChec00 := ( IIF( Form_1.checkbox_0.Value,"True","False") )
 
   IF bChec00= "True"  
      xeck0  :=   .T.
    ELSE
      xeck0  :=   .F.
   ENDIF

   bChec01 := ( IIF( Form_1.checkbox_1.Value,"True","False") )
 
   IF bChec01= "True"  
      xeck1  :=   .T.
    ELSE
      xeck1  :=   .F.
    ENDIF

  bChec02 := ( IIF( Form_1.checkbox_2.Value,"True","False") )
 
   IF bChec02= "True"  
      xeck2  :=   .T.
   ELSE
      xeck2  :=   .F.
   ENDIF

  bChec03 := ( IIF( Form_1.checkbox_3.Value,"True","False") )
 
   IF bChec03= "True"  
      xeck3  :=   .T.
   ELSE
      xeck3  :=   .F.
   ENDIF

   SAVE TO xeckyn.mem ALL LIKE  xeck*
    
    Form_1.Release

Return Nil

Regards
Mustafa :oops: :D

RPC
Posts: 167
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF
Has thanked: 57 times
Been thanked: 8 times

Post by RPC » Fri Feb 14, 2020 2:36 pm

Hi Jimmy
Thanks for the reply.
My problem was maintaining the state of check boxes after I exit the program altogether and then when I again start the program say after sometime.
Static variables are useful when you are still in the program, when you exit the program they are gone.

RPC
Posts: 167
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF
Has thanked: 57 times
Been thanked: 8 times

Post by RPC » Fri Feb 14, 2020 2:40 pm

Hi Grigory
You have really provided an elegant solution for which I was breaking my head for last 2 days.
Many many thanks.
There is just one more little problem. I am not getting the "save" bitmap image on the "Save & Exit" button, though I have mentioned it in the resource window of IDE. Can you please have a look at it ?
Many thanks again for the great help you are providing.

RPC
Posts: 167
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF
Has thanked: 57 times
Been thanked: 8 times

Post by RPC » Fri Feb 14, 2020 2:44 pm

Hi Mustafa
Great to have your solution. Many thanks.
Perhaps you can also help with "save" bitmap image problem I have mentioned above.
Thanks

Post Reply