|
-
Jun 17th, 2008, 12:35 PM
#1
Thread Starter
Member
[RESOLVED] [2005] Message Box YesNoCancel
Need some assistance using the MessageBox YesNoCancel buttons. I'm trying to check and see if a file exists, if it does, then prompt the user to select yes to overwrite, select no, and the file will copy to another location, or select cancel and close the dialog. My code I have below, goes to the "YES" statement no matter if I press Yes, No or Cancel. Any help is appreciated!!!
Thanks.
Code:
Private Function FileExists(ByVal oldname, ByVal newname)
If File.Exists(oldname) Then
If File.Exists(newname) Then
MessageBox.Show("File" & newname & " already exists. Do you wish to overwrite?", "File Exists", _
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If Windows.Forms.DialogResult.Yes Then
My.Computer.FileSystem.CopyFile(oldname, newname, True)
ElseIf Windows.Forms.DialogResult.No Then
File.Copy(oldname, "s:\pfirs\joann\" & oldname)
Else 'Windows.Forms.DialogResult.Cancel()
Me.Close()
End If
Else
My.Computer.FileSystem.CopyFile(oldname, newname, True)
End If
-
Jun 17th, 2008, 12:45 PM
#2
Hyperactive Member
Re: [2005] Message Box YesNoCancel
vb Code:
Dim test As DialogResult
test = MessageBox.Show("dssf", "sds", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error)
If test = Windows.Forms.DialogResult.Yes Then
ElseIf test = Windows.Forms.DialogResult.No Then
ElseIf test = Windows.Forms.DialogResult.Cancel Then
End If
-
Jun 17th, 2008, 01:02 PM
#3
Frenzied Member
Re: [2005] Message Box YesNoCancel
Use gnaver's code, it's exactly what you need. And I'd strongly suggest that you turn Option Strict On, since you will get cleaner, faster, more reliable code, as well as learning to code properly.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Jun 17th, 2008, 01:02 PM
#4
Thread Starter
Member
Re: [2005] Message Box YesNoCancel
Thanks gnaver. Worked perferctly.
-
Jun 17th, 2008, 01:09 PM
#5
Re: [RESOLVED] [2005] Message Box YesNoCancel
This is just a personal preference, but I think it is a bit cleaner to use a case statement in situations like this. It is also a bit easier to read.
Code:
Dim Response As DialogResult = MessageBox.Show("Do you want to?", "Are you sure?", MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
Select Case Response
Case Windows.Forms.DialogResult.Yes
MessageBox.Show("Yes")
Case Windows.Forms.DialogResult.No
MessageBox.Show("No")
Case Windows.Forms.DialogResult.Cancel
MessageBox.Show("Cancel")
End Select
As I said, that is just a personal preference. The If/ElseIf/etc works just as well.
-
Jun 17th, 2008, 01:19 PM
#6
Hyperactive Member
Re: [RESOLVED] [2005] Message Box YesNoCancel
i agree with you , better to read and easier to write
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|