You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
' Display SaveFile [hParent], [xpos], [ypos], title$, folder$, filter$, start$, DefExt$, flags To FileName$
Display SaveFile , , , "Choose a Folder and File Name", CurDir$, "", fn$, "Test", flag To FN$
?fn$ & $CrLf & Str$(Flag),, FuncName$
The above displays and returns the choice (FileName$) as expected. However how do I know if the user clicks the Cancel Button or the Save button? What it the user changes his mind and doesn't want to Save (Cancels)?
Since this is the 1st I have seen "Display" and not used it yet, I suspect it in turn calls "OpenFileDialog" in the api???
If so then 2 questions hold true and I hope you find your answer
1.) Returning results = ?????? (not documented, and not documented if there is a return value)
2.) How to tell what was returned? (Cancel?? FileName??? or what???, since till now my only experience with "OpenFileDialog" is to not offer a default name, and if still blank on return then the user must have cancelled????)
Interesting questions, and hoping someone has viable answers
Engineer's Motto: If it aint broke take it apart and fix it
"If at 1st you don't succeed... call it version 1.0"
"Half of Programming is coding"....."The other 90% is DEBUGGING"
"Document my code????" .... "WHYYY??? do you think they call it CODE? "
Thanks Pete. Probably never woul hve found the answer myself. Unusual way to check. Here's a quick sample answer:
Code:
' Display SaveFile [hParent], [xpos], [ypos], title$, folder$, filter$, start$, DefExt$, flags To FileName$
Display SaveFile , , , "Choose a Folder and File Name", "C:\Temp", "", [B]fn$[/B], "Test", flag To [B]FN1$[/B]
'check for cancel operation
If [B]fn1$ = fn$[/B] Then
? "Cancelled"
Exit Sub
End If
? "Got here, so Not cancelled"
======================================
Poetry is the deification of reality.
Edith Sitwell
======================================
Does that work if I change the selected file name, and THEN cancel?
The bottom line is still as I suggested in the linked post: when CANCEL is selected, the function should return a null string. (as in, "I didn't select nuttin'")
If it doesn't, then it's either poorly-designed or buggily implemented.
Does that work if I change the selected file name, and THEN cancel?
Yes, or at least it did on the one time I tried it.
The bottom line is still as I suggested in the linked post: when CANCEL is selected, the function should return a null string. (as in, "I didn't select nuttin'")
If it doesn't, then it's either poorly-designed or buggily implemented.
MCM
Far be it from me to disagree with the Grand Poohbah of PB.
However the Cancel can be detected, so that's where the money is. It would have been nice if it had been explained in the docs, though.
================================
If I could drop dead right now,
I'd be the happiest man alive.
Samuel Goldwyn
================================
Ok I give. (I don't use shortcuts like fn$), but quick compile and wonder why I get an error of fn$ is not defined?
Code:
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
' Display SaveFile [hParent], [xpos], [ypos], title$, folder$, filter$, start$, DefExt$, flags To FileName$
DISPLAY SAVEFILE , , , "Choose a Folder and File Name", "C:\Temp", "", fn$, "Test", flag TO FN1$
'check for cancel operation
IF fn1$ = fn$ THEN
? "Cancelled"
EXIT SUB
END IF
? "Got here, so Not cancelled"
END FUNCTION
what CNDS (as MCM has coined) am I committing????
Engineer's Motto: If it aint broke take it apart and fix it
"If at 1st you don't succeed... call it version 1.0"
"Half of Programming is coding"....."The other 90% is DEBUGGING"
"Document my code????" .... "WHYYY??? do you think they call it CODE? "
Pretty cool. I'd find it very difficult to work without shortcuts like strings (fn$). Yet another technique I'll have to learn someday.
what CNDS (as MCM has coined) am I committing????
What you did was drop what was supposed to be an illustrative snippet into working code. I probably should have made the example more clear. My bad.
=============================================
"The difference between fiction and reality?
Fiction has to make sense."
Tom Clancy
=============================================
FUNCTION PBMAIN () AS LONG
LOCAL hParent,xpos,ypos, flags,countvar AS LONG
LOCAL title,folder,filter,start,defaultexts,filevar AS STRING
DISPLAY SAVEFILE hParent&, xpos&, ypos&, title$, folder$, filter$, _
start$, defaultexts$, flags& TO filevar$ ,countvar&
IF countvar = 0 THEN ? "Cancelled or closed"
END FUNCTION
What you did was drop what was supposed to be an illustrative snippet into working code. I probably should have made the example more clear. My bad.
Not a prob, but I just missed what would be pseudo-code, would not just drop into a compilable and "Just Work"
José brought up a goood point with
As the error message says, you get an error because fn$ is not defined. If you use #DIM ALL, you have to declare the variables before you use them.
and MCM with the counter-punch, but since I am one for declaring everything myself, and not PbDefaults, then you both have a point.
1st problem. - PB by default adds the following if I select "Generic Pb Program"
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
END FUNCTION
which has the #DIM ALL, but for some reason does not include the Win32Api.Inc?????
2nd problem - Comment out #DIM ALL and only variable not defined is "flag" (could be a koinky-dink that it is a long and not shortcutted?).....hmmmm yep after test with changing "flag" to "flag&" it compiles
now maybe its just me, or the way I read the docs....but shortcut and declare can NOT be mixed and matched (which apparently I was, and did not know it)
3rd Problem (since I am one for declaring everything)
when I tried to declare, I got "Type Id (?%&!#$) not allowed"
(Gotta laugh at the point of (?%&!#$) cause that is what I was doing at the time it happened
The problem was I was attempting to declare the "Shortcut" as in
Code:
LOCAL fn$ AS STRING
LOCAL fn1$ AS STRING
local flag& as long
which come to find out has to be without the shortcut
all boils down to programming style, but since I do not use the "Shortcuts" and Pb by default forces the declare, I ran into a "What In the FUBAR" am I doing wrong??? (and the only thing different was taking shortcuts)
at least now I know and "Knowing is Half the battle"
(maybe fodder for a Gary post??)
Anyways, Gost is reallllllLLLLLLllll close to having a solution to my age ole problem of detecting if cancelled was used. (I tricked it twice but have not been able to replicate)
Code:
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
' local fn$ as string
' local fn1$ as string
' local flag& as long
LOCAL fn AS STRING
LOCAL fn1 AS STRING
LOCAL flag AS LONG
Fn$ = "Default File"
' Display SaveFile [hParent], [xpos], [ypos], title$, folder$, filter$, start$, DefExt$, flags To FileName$
DISPLAY SAVEFILE , , , "Choose a Folder and File Name", "C:\Temp", "", fn$, "Test", flag& TO FN1$
'check for cancel operation
IF fn1$ = fn$ THEN
? "Fn$ = " + Fn$ + $CR _
+ "Fn1$ = " + Fn1$ + $CR _
+ "Cancelled"
EXIT FUNCTION
END IF
? "Fn$ = " + Fn$ + $CR _
+ "Fn1$ = " + Fn1$ + $CR _
+ "Got here, so Not cancelled"
END FUNCTION
The only thing I dont get is how
Code:
DISPLAY SAVEFILE , , , "Choose a Folder and File Name", "C:\Temp", "", fn$, "Test", flag& TO FN1$
can work fine without tripping a compile problem? (I always thought anything you look for a reply from would have to be passed in parenthesis???
aka
Code:
DISPLAY SAVEFILE (, , , "Choose a Folder and File Name", "C:\Temp", "", fn$, "Test", flag&) TO FN1$
Engineer's Motto: If it aint broke take it apart and fix it
"If at 1st you don't succeed... call it version 1.0"
"Half of Programming is coding"....."The other 90% is DEBUGGING"
"Document my code????" .... "WHYYY??? do you think they call it CODE? "
See options for CALL in help file.
The one that got me was the first time I saw a FUNCTION name return a value instead of the usual FUNCTION=1
Code:
FUNCTION PBMAIN () AS LONG
LOCAL result AS LONG
Test TO result 'See CALL in help file
END FUNCTION
FUNCTION TEST() AS LONG
TEST = 1 'or FUNCTION=1
END FUNCTION
Here's a complete example, providing a Function that returns "" if the user cancelled.
After it passes inspection, I'll post its tattered remnants on the Source Code forum for future searches...
-jhm
Code:
'DisplayOpen_Cancel.bas by jhm
#Compile Exe
#Dim All
Function PBMain () As Long
Local hParent, lXpos, lYpos, lFlags As Long
Local sTitle, sFolder, sFilter, sInitFN, sDefExt, sRet, m As String
sTitle = "Choose a Folder and File Name to Open"
sFolder = "c:\tools" 'the folder you want initially displayed (trailing \ or not, doesn't matter)
sFilter = Chr$("Text", 0, "*.txt", 0) & Chr$("Config", 0, "*.cfg;*.ini", 0) 'choices for FileType drop-box
sInitFN = "Text1.txt" 'a name you want initially shown and selected
sDefExt = "*.txt" 'the extension you want appended if user omits one
'set the first 3 params to -1 if you don't care what their values are.
sRet = Display_Openfile(-1,-1,-1, sTitle, sFolder, sFilter, sInitFN, sDefExt, lFlags)
'test it with some numeric values...
'sRet = Display_Openfile(-1,300,50, sTitle, sFolder, sFilter, sInitFN, sDefExt, lFlags)
If sRet = "" Then
m = "Cancel"
'do the stuff you'd do when user cancels the selection
Else
m = sRet
'do the stuff you'd do when user has selected a file
End If
MsgBox m,,"Dialog ended with: "
'...and they lived happily ever after...
End Function
Function Display_Openfile(ByVal hParent As Long, _
ByVal xpos As Long, _
ByVal ypos As Long, _
ByVal title As String, _
ByVal folder As String, _
ByVal filter As String, _
ByVal start As String, _
ByVal sDefExt As String, _
ByVal flags As Long) As String
Local tmp As String
'by using -1 as an indicator, we allow caller to pass 0, if truly wants dialog in the corner.
'yes, we sacrifice the -1,-1 window position, but is that really a loss? then use some other value...
If hParent = -1 Then
If xpos = -1 And ypos = -1 Then
Display Openfile ,,, title, folder, filter, start, sDefExt, flags To tmp
ElseIf xpos = -1 And ypos <> -1 Then
Display Openfile ,,ypos, title, folder, filter, start, sDefExt, flags To tmp
ElseIf xpos <> -1 And ypos = -1 Then
Display Openfile ,xpos,, title, folder, filter, start, sDefExt, flags To tmp
ElseIf xpos <> -1 And ypos <> -1 Then
Display Openfile ,xpos,ypos, title, folder, filter, start, sDefExt, flags To tmp
End If
Else
If xpos = -1 And ypos = -1 Then
Display Openfile hParent,,, title, folder, filter, start, sDefExt, flags To tmp
ElseIf xpos = -1 And ypos <> -1 Then
Display Openfile hParent,,ypos, title, folder, filter, start, sDefExt, flags To tmp
ElseIf xpos <> -1 And ypos = -1 Then
Display Openfile hParent,xpos,, title, folder, filter, start, sDefExt, flags To tmp
ElseIf xpos <> -1 And ypos <> -1 Then
Display Openfile hParent,xpos,ypos, title, folder, filter, start, sDefExt, flags To tmp
End If
End If
If start = tmp Then 'user cancelled
Function = ""
Else 'user selected something
Function = tmp
End If
End Function
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment