Grid Transfer

New to FreeBASIC? Post your questions here.
Post Reply
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Grid Transfer

Post by RNBW »

In the attached code, in all cases, the values transferred from the top grid to the bottom grid have the last digit omitted. Can someone explain to me why. It may be obvious, but my old brain just can't see it.

Code: Select all

'=============================================================================
'GridTransfer
'Author: RNBW
'1 August 2017

'Includes Row AND Column Headings
'Transfers values from one grid to another
'==========================================
'
' INSTRUCTIONS:
' -------------------
' ENTER DATA INTO THE TOP GRID. WHEN YOU CLICK IN NEXT CELL
' OR CLICK <Click me!> Button, DATA WILL BE TRANSFERRED
' FROM TOP GRID TO BOTTOM GRID
'==========================================

#INCLUDE "window9.bi"

DIM AS LONG NumOfRows=3, NumOfCols=3, id1, id2
DIM AS STRING sRows, sCols, stRow, stCol, stID1, stID2

DIM SHARED AS HWND Window_Main, Button_Click
DIM SHARED AS MSG msg
DIM AS STRING text, text2, result
DIM AS HWND event
DIM AS LONG vPos, hPos, wide, rHt, row, col

'---------------------
' MAIN WINDOW
'---------------------

OpenWindow("Multi-Column Grid",100,100,550,300)
ButtonGadget(1,10,10,100,20, "Click me!")

'-------------------------------
'  SET UP THE FIRST GRID
'-------------------------------
vPos = 40: rHt = 20
FOR row = 1 TO NumOfRows
   FOR col = 1 TO 3
      SELECT CASE col
         CASE 1
            hPos = 10: wide = 65
         CASE 2
            hPos = 75: wide = 380
         'CASE 3 TO 8
			Case 3
            hPos = col*65+(455-65*3) : wide = 65
         'CASE 9
           ' hPos = (10+65+380)+(col-3)*65 : wide = 75
      END SELECT
      stRow = STR(row): stCol = STR(col)
      stID1 = "1" + stRow+stCol
      id1 = VAL(stID1)
      StringGadget(id1, hPos, vPos+rHt*(row-1), wide+1, rHt+1, "", WS_BORDER)
   NEXT
NEXT

'----------------------------------
'  SET UP THE SECOND GRID
'----------------------------------
rHt = 20
vPos = 40 + rHt * (NumOfRows + 3)
FOR row = 1 TO NumOfRows
   FOR col = 1 TO 3
      SELECT CASE col
         CASE 1
            hPos = 10: wide = 65
         CASE 2
            hPos = 75: wide = 380
         'CASE 3 TO 8
			Case 3
            hPos = col*65+(455-65*3) : wide = 65
        ' CASE 9
           ' hPos = (10+65+380)+(col-3)*65 : wide = 75
      END SELECT
      stRow = STR(row): stCol = STR(col)
      stID2 = "2" + stRow+stCol
      id2 = VAL(stID2)
      StringGadget(id2, hPos, vPos+rHt*(row-1), wide+1, rHt+1,"", WS_BORDER)
   NEXT
NEXT

'-----------------------------------
' SET UP HEADINGS IN ROW 1
' IN BOTH GRIDS
'-----------------------------------
row = 1
FOR row = 1 TO 1
   FOR col = 2 TO NumOfCols
      stRow = STR(row): stCol = STR(col)
      stID1 = "1" + stRow+stCol
      id1 = VAL(stID1)
      SetGadgetText(id1,"Col"+STR(col-1))
      
      stID2 = "2" + stRow+stCol
      id2 = VAL(stID2)
      SetGadgetText(id2,"Col"+STR(col-1))
   NEXT
NEXT

'----------------------------------
' SET UP HEADINGS IN COL 1
' IN BOTH GRIDS
'----------------------------------

col = 1
FOR row = 2 TO NumOfRows
   FOR col = 1 TO 1
      stRow = STR(row): stCol = STR(col)
      stID1 = "1" + stRow+stCol
      id1 = VAL(stID1)
      SetGadgetText(id1,"Row"+STR(row-1))
      
      stID2 = "2" + stRow+stCol
      id2 = VAL(stID2)
      SetGadgetText(id2,"Row"+STR(row-1))
   NEXT
NEXT

'-----------------------------------------
'TRANSFER DATA ENTERED IN THE FIRST GRID
'INTO THE SECOND GRID.
'-----------------------------------------
DO
   var EVENT =  WaitEvent()
   IF event=WM_LBUTTONDOWN THEN
      '-----------------------------------------
      ' GET VALUES FROM FIRST GRID
      ' AND TRANSFER TO SECOND GRID
      '-----------------------------------------
      FOR row AS INTEGER = 2 TO NumOfRows
         FOR col AS INTEGER = 2 TO 3

            stRow = STR(row): stCol = STR(col)
            stID1 = "1" + stRow+stCol
            id1 = VAL(stID1)
            stID2 = "2" + stRow+stCol
            id2 = VAL(stID2)
            SetGadgetText(id2,GetGadgetText(id1))
         NEXT col
      NEXT row
   END IF
   IF event=EventClose THEN END
LOOP

END
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Grid Transfer

Post by fxm »

'VAL' converts a string to a floating point number.
Try 'VALINT' instead (converts a string to a 32bit integer), so as not to lose precision.
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Grid Transfer

Post by RNBW »

fxm wrote: Apr 10, 2022 11:57 'VAL' converts a string to a floating point number.
Try 'VALINT' instead (converts a string to a 32bit integer), so as not to lose precision.
Thanks for your prompt response.

The code is taken from a much larger program and the conversion would be a floating point number. Also, it doesn't explain why it chops off the last digit, which is my problem.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Grid Transfer

Post by badidea »

I thought that 'window9' is a GUI library for windows & linux. But this code is for windows only?
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Grid Transfer

Post by RNBW »

badidea wrote: Apr 10, 2022 13:16 I thought that 'window9' is a GUI library for windows & linux. But this code is for windows only?
Window9 is a library that can be used for b9th Windows and Linux. I don't use Linux. The code is written specifically for Windows.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Grid Transfer

Post by VANYA »

Hi All!

@RNBW , I did not quite understand the question. Look at the screenshot, mine all the text is transferred to the bottom line:

Image
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Grid Transfer

Post by RNBW »

Hi Vanya

I've changed the directory that I was running the code from and now it works without a problem. I've no idea why. There is nothing special with either directory. At least it puts my mind at rest that there was something wrong with the code.
3oheicrw
Posts: 25
Joined: Mar 21, 2022 6:41

Re: Grid Transfer

Post by 3oheicrw »

With tables you should really consider SGL as you already use the Windows API anyway. SGL is just a simple wrapper and will not get in the way of you. If you download the SGL samples from Henri's site you will see SGL is optimized to use with tables and Henri really used SGL to develop commercial software that is full of tables (and graphs).

viewtopic.php?t=31599
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Grid Transfer

Post by RNBW »

fxm wrote: Apr 10, 2022 11:57 'VAL' converts a string to a floating point number.
Try 'VALINT' instead (converts a string to a 32bit integer), so as not to lose precision.
@fxm
My apologies. You are absolutely correct. I should have been using VALINT.
Post Reply