Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculate column on grid
Message
From
26/01/2024 01:14:56
Lutz Scheffler (Online)
Lutz Scheffler Software Ingenieurbüro
Dresden, Germany
 
 
To
25/01/2024 18:12:07
Luis Santos
Biglevel-Soluções Informáticas, Lda
Portugal
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01687576
Message ID:
01687578
Views:
68
>Hello community,
>
>i created these bindevents for columns 1,5,8,9 so that when i change the value of any of them the procedure can calculate the value of my column13.
>but it doesn't work!
>
>As I'm already getting a headache, can someone help me explain what's wrong and possibly correct the code?
>
>Here is the code:
>
>PUBLIC oManipulador as Manipulador
>
>*!* - Instancia o objeto manipulador
>oManipulador = CREATEOBJECT("Manipulador")
>
>
>loGrid = SFT.PAGEFRAME1.PAGE1.CONT1.GRID1
>  
>* Set the ControlSource for each column
>*loGrid.Column1.ControlSource = "FI.ALTURA"
>*loGrid.Column5.ControlSource = "FI.ESPESSURA"
>*loGrid.Column8.ControlSource = "FI.LARGURA"
>*loGrid.Column9.ControlSource = "FI.PARTES"
>
>
>* Bind the event to Column1
>BINDEVENT(loGrid.Column1, "AfterRowColChange", oManipulador, "CalculateTotal")
>
>* Bind the event to Column5
>BINDEVENT(loGrid.Column5, "AfterRowColChange", oManipulador, "CalculateTotal")
>
>* Bind the event to Column8
>BINDEVENT(loGrid.Column8, "AfterRowColChange", oManipulador, "CalculateTotal")
>
>* Bind the event to Column9
>BINDEVENT(loGrid.Column9, "AfterRowColChange", oManipulador, "CalculateTotal")
>
>DEFINE CLASS Manipulador AS Session
>LPARAMETERS nColIndex
>
>* Define the event handler method directly
>PROCEDURE CalculateTotal
>   ? "CalculateTotal method called"  && No message was return ??
>      loGrid.Column13.text1.value = VAL(loGrid.Column1.text1.value * loGrid.Column5.text1.value * loGrid.Column8.text1.value * loGrid.Column9.text1.value)
>
>ENDPROC
>enddefine
>
>
>Thanks!
>Luis

Hi Luis

# Eventbinding and Events available
AfterRowColChange is not an event of column, it's an event of the grid

##1 Try output the return value
?BINDEVENT(loGrid.Column5, "AfterRowColChange", oManipulador, "CalculateTotal")
Should be 0, nothing bound

##2 Run one time
?BINDEVENT(loGrid, "AfterRowColChange", oManipulador, "CalculateTotal")
The parameter of AfterRowColChange should be the new column for all I know.
- You might need to use the other properties like ActiveColumn inside your handler
- use additionally
?BINDEVENT(loGrid, "BEFORERowColChange", oManipulador, "SomeMethod")
to store the old column
- , or brute force and calc everytime.

Also it might be simple enough to add the code to grids BeforeRowColChange and AfterRowColChange events, there is little need for eventbinding. Something like
Proce init
THIS.ADDPROPERTY("gnLastCol",0)

Proce BeforeRowColChange
LPARA tnLastCol
THIS.ADDPROPERTY("gnLastCol",tnLastCol)

Proce AfterRowColChange
LPARA tnNewCol

IF INLIST(THIS.gnLastCol,1,5,8,9) THEN
 This.Column13.text1.value = VAL(loGrid.Column1.text1.value * loGrid.Column5.text1.value * loGrid.Column8.text1.value * loGrid.Column9.text1.value)...
# Problems
## Use of the events
There is something in your logic I don't understand. Binding to a column with AfterRowColChange like you try, means the target event fires if you ENTER this columns. But I guess you like to alter if you LEAVE the column, so BeforeRowColChange is the right handler. Or - since BeforeRowColChange runs in front of VALID, first BeforeRowColChange and store the column, then AfterRowColChange and run when the right column is exited. See above.

## Use of VAL()
> loGrid.Column13.text1.value = VAL(loGrid.Column1.text1.value * loGrid.Column5.text1.value * loGrid.Column8.text1.value * loGrid.Column9.text1.value)
A product is numeric anyway, so way VAL()? You can not multiply strings.

### Or
The values are non-numeric then it must be
loGrid.Column13.text1.value = VAL(loGrid.Column1.text1.value) * VAL(loGrid.Column5.text1.value) * VAL(loGrid.Column8.text1.value) * VAL(loGrid.Column9.text1.value)
### And
Why calc the values of grids columns textboxes? It's in the cursor anyway
loGrid.Column13.text1.value = FI.ALTURA * FI.ESPESSURA * FI.LARGURA * FI.PARTES
## Result of the operation
- This will alter column13 textboxes in all rows, for all I know
- What is controlsource of column13?

I would solve this like
*Add a help column to the cursor controlling the grid, for this example nCol13 of value ALTURA * ESPESSURA * LARGURA * PARTES
*Then set like
loGrid.Column9.ControlSource = "FI.nCol13"

*In AfterRowColChange use a simple
REPLACE nCol13 WITH FI.ALTURA * FI.ESPESSURA * FI.LARGURA * FI.PARTES IN FI
# ControlSource of Column13
Have you considered the old fashion approach, and set the control source of column13 to evaluation (note the brackets, I use it rarely, so I guess it was with brackets ...)
      loGrid.Column13.ControlSource = "[FI.ALTURA * FI.ESPESSURA * FI.LARGURA * FI.PARTES]"
This just calcs the result of each row. May need a refresh in AfterRowColChange. (Pro Tip: this works with every ControlSource on every object)

Lutz
Words are given to man to enable him to conceal his true feelings.
Charles Maurice de Talleyrand-Périgord

Weeks of programming can save you hours of planning.

Off

There is no place like [::1]
Previous
Reply
Map
View

Click here to load this message in the networking platform