Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Common Dialogs (Open vs. Save)

Status
Not open for further replies.

NetNomad312

Technical User
Jun 16, 2004
27
US
Hey again. Sorry for my dumb questions, but I haven't had the option to take a course in this lately. My knowledge of VB is limited to a really basic course two years ago that tought us nothing about common dialog control, and a tutorial on common dialog control that didn't say anything about what happens when the user clicks "cancel" on such a dialog. I looked in the MSDN library, but unless I missed it, that didn't help me either.

Essentially, all I'm doing at this point is making a program that opens and saves a text-based type of file. It's basically just Notepad with a couple extra features that relate to this kind of file. I've used the Open dialog before, to select files in another program of mine (that would in turn pass them as arguments to another program - it was a frontend, really).

In this program, I managed to load a text file and have the program print it line-by-line into a textbox. But the "cancel" button never works; if it had a filename in the box, it would open that file anyway if I clicked cancel. It's the same thing with the save box. I ran it through debug... the line that's supposed to get the filename is:

result = cdlOpen.FileName

Maybe I'm just being newbish and I'm expecting too much, but I thought that if you clicked cancel, then cdlOpen.FileName would be empty, right? The checks that come after this test to see if result <> "" , and if I get a filename anyway, they're of no use. Same thing with the save dialog. I would, for instance, have my program create a file, then I would change it and try to resave it. When the dialog appeared, the filename would already be in the box. I clicked cancel, and it asked me to overwrite as if I had clicked OK. That's the other thing - my save-dialogs don't ask to overwrite, they just do it... so I programmed one in myself using a message box.

So my questions are these:
1. How do I get it to tell when I clicked "cancel" as opposed to "open" or "save?"
2. Aren't save dialogs supposed to figure out whether a file exists and ask to overwrite it? If not, how do I program one in the correct way (as mine will appear after the dialog is closed)?

Thanks again... you guys really are helpful. :-/
 
i haven't used this control that extensivley but I did find that you can set the cancelerror to true when you call the control

CommonDialog1.CancelError = True

then if cancel is pressed error #32755
is generated and you can code around that.

hope this helps some, a few of my future projects will use this heavly so I am sure i will be learning more.
 
Ah, that's one way to do it. I used On Error one time when I had a drivelistbox and I needed to make sure it wouldn't crash when the user selected an unavailable drive. I didn't really understand them at that point, but I just had "On Error GoTo lblDebug". For this one:

On Error GoTo lblEnd
[Other stuff]
lblEnd:
End Sub

If you figure out how to do the right kind of "overwrite?" box during your project, please tell me. :)
 
I can't say there is, except maybe this line:

.Flags = cdlOFNHideReadOnly _
Or cdlOFNPathMustExist _
Or cdlOFNExtensionDifferent

I did not set any flags for my dialog object; it's still "0." Do I need to change this to get the overwrite option to work?
 
I see you haven't read faq222-2244 yet.

For this problem follow azroberts advice and use the CancelError property. Something like:
[tt]
Private Sub Command1_Click()

On Error GoTo errhnd

With CommonDialog1
.InitDir = "c:\mydir"
.FileName = ""
.Flags = cdlOFNFileMustExist
.Filter = "Text files (*.txt)|*.txt"
.ShowOpen
MsgBox .FileName
End With
Exit Sub

errhnd:
If Err.Number = 32755 Then CommonDialog1.FileName = ""
Resume Next
End Sub[/tt]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Thanks...

About that page, this is the third time I've been sent to it. I read it the first time, it seemed reasonable. What's wrong? I do try to look for help elsewhere first. Maybe I don't know where to look in the MDSN library all the time, that's why it may seem like I go here first... but it's not true.
 
People will often point you to that faq if:
1. You haven't been on the forum for long
or
2. You haven't given any feedback
or
3. If you haven't yet posted any answers to anyone elses' questions

There is no particular issue about it, just that it gives guidelines on forum usage and etiquette. Many people have the reference in their sig (as I do)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Ah, I see. Yes, I'm afraid I have little to contribute at this point... I need to go take a course in this (or more likely vb.net, actually, since that's what's more readily offered to me) before I have anything valuable or unique.

I just now figured out that your solution was for the cancel thing... thanks. I misread it and thought that was a solution to my save-as dialog's strange inability to ask the user to overwrite. Thus, nothing really changed. Is there a flag I need to set to get it to ask before overwriting a file?
 
Sorry to double post, but I figured it out anyway. I was just looking in the wrong place. It's cdlOFNOverwritePrompt, I think. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top