Aug 7, 2008

Can you put a file in the desired location?

When you want to put a file in a certain location, you may want to check if it is possible to write to that location. You might not have write permission, the location might not exist or a file with the same name allready exists and cannot be overwritten because it is in use. With the code below, you can check this out:

* FUNCTION FILEOVERWRITE()

PARAMETERS cGo

fhandle =FCREATE(cGo)
IF fhandle > -1
=FCLOSE(fhandle)
DELE FILE (cGo)
RETURN .T.
ELSE
RETURN .F.
ENDIF

Aug 1, 2008

Function that returns a part of a comma-separated string

Whenever you need to to handle comma-separated string, you might want to check the function below, which makes use of VFP's ALINES() function to return a certain value in the string.


* Parameters: String (character) , which part (numeric), delimiter (character)
PARAMETERS cCsv, nPart, pdelimiter

IF PCOUNT() < 3
pDelimiter = ","
ENDIF

cCsv = STRTRAN(cCsv, pDelimiter, " "+CHR(13) )

ALINES(aCsv, cCsv , .T.)

* When array element 1 = .False. => return ""
* When the number of array elements < nPart => return ""
* Otherwise, return the piece of the string

RETURN IIF( TYPE( "aCsv[1]" ) = "L" , "", IIF( ALEN(aCsv,1) < nPart , "", aCsv[nPart] ) )

Jul 28, 2008

Resize or reposition a desktop form to fit the Windows desktop

In your VFP application, you want your users to have the application form re-appear as it was left behind the last time. So, you store the screen coordinates (in your apps mechanism that saves the application settings) and restore the form to the saved coordinates, the next time it is run.
But in the mean time, the user may have altered the screen resolution, changed to a p.c. with a different screen resolution or moved/resized the Windows startbar.

I these scenario's, your application form may not fit in the Windows desktop anymore.
Below, you find the code of SETFORMSIZE.PRG, which repositions the form to fit the desktop again.

To test it:
- create a form with the desktop property to .True.
- Put this code in the load method of the form:
This.Top = -100
This.Left = -100
This.Width = 100000
This.Height = 100000
DO SETFORMSIZE WITH This

- Run the form


************************************************************************
* SETFORMSIZE.PRG *
************************************************************************
PARAMETERS oForm

LOCAL nWinHeight, nWinWidth, nCorrection

* Width en height of screenspace minus space occuped by the startmenu bar
* wherever that is placed or whatever size it is
nWinHeight = SYSMETRIC(22)
nWinWidth = SYSMETRIC(21)
nCorrection = sysmetric(3)*2

* First: check if the form is off screen above and to the left
IF oForm.Left < 0
oForm.Left = 0
ENDIF
IF oForm.Top < 0
oForm.Top = 0
ENDIF

* Next: check if the form is to high or wide to fit in the screen
IF (oForm.Top + oForm.Height) > nWinHeight && Oh Oh, Formheight beyond screen
nHowMuch = (oForm.Top + oForm.Height ) - nWinHeight
IF nHowMuch > oForm.Top && Oh oh, form does not fit in the screen
IF oForm.MinHeight >= (nWinHeight - oForm.Height )
oForm.Height = nWinHeight - nCorrection
ENDIF

nNewTop = 0
ELSE
nNewTop = (oForm.Top - nHowMuch) - nCorrection
ENDIF
ELSE
nNewTop = oForm.Top
ENDIF
IF (oForm.Left + oForm.Width) > nWinWidth && Oh Oh, Formwidth beyond screen
nHowMuch = (oForm.Left + oForm.Width ) - nWinWidth
IF nHowMuch > oForm.Left && Oh oh, form does not fit in the screen
IF oForm.MinWidth >= (nWinWidth - oForm.Width )
oForm.Width = nWinWidth - nCorrection
ENDIF
nNewLeft = 0
ELSE
nNewLeft = (oForm.Left - nHowMuch) - nCorrection
ENDIF
ELSE
nNewLeft = oForm.Left
ENDIF

oForm.Left = nNewLeft
oForm.Top = nNewTop