Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Message Box YesNoCancel

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2008
    Location
    Louisville, Ky
    Posts
    40

    Resolved [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

  2. #2
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [2005] Message Box YesNoCancel

    vb Code:
    1. Dim test As DialogResult
    2.         test = MessageBox.Show("dssf", "sds", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error)
    3.         If test = Windows.Forms.DialogResult.Yes Then
    4.  
    5.         ElseIf test = Windows.Forms.DialogResult.No Then
    6.  
    7.         ElseIf test = Windows.Forms.DialogResult.Cancel Then
    8.  
    9.         End If

  3. #3
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2008
    Location
    Louisville, Ky
    Posts
    40

    Re: [2005] Message Box YesNoCancel

    Thanks gnaver. Worked perferctly.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  6. #6
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    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
  •  



Click Here to Expand Forum to Full Width