If, else, won't work somehow

cornelvis

Active member
Joined
Jul 15, 2005
Messages
30
Programming Experience
3-5
I think I'm just overlooking something simple here, but it's freaking me out.
I'm kind of newby, but totally anymore, but still making stupid mistakes(like anyone I guess....)

here is the thing.
I try to copy a folder from one place to another and everything goes fine, untill I want to create a msgBox with an outcome of Ok or Cancel.
It doesn't matter what I do, I tried different approaches, but it ends it in both times even when I hit ok it stops.
I build in a simple message : stopped just to figure out what it was doing.

here comes the code:

VB.NET:
Private Declare Function InitCommonControls Lib "Comctl32.dll" () As Long
    Private Sub Form_Initialize()
        InitCommonControls()
    End Sub
    Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
    Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32
    Private _totalFileSize As Long = 0
    Private _totalBytesCopied As Long = 0
    Private _copyProgressRoutine As CopyProgressRoutine
    Private Source As String
    Dim UserProfileName = SystemInformation.UserName
    Private Destination As String = "D:\Documents and Settings\" & UserProfileName & "\My Documents\Program Data\test"
    Dim frmProgBar As New frmProgBar
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Me.Hide()
        Dim fbdBrowse As New FolderBrowserDialog
        If fbdBrowse.ShowDialog() = DialogResult.OK Then
            Source = fbdBrowse.SelectedPath
        Else : End
        End If
        [COLOR=red]Dim answ As Microsoft.VisualBasic.MsgBoxResult = MsgBoxResult.Cancel
        MsgBox("Following folder structure will be created: " & Chr(13) & Destination & Chr(13) & Chr(13) & "and the following data will be copied from " & Source, MsgBoxStyle.OKCancel, "Confirm")
        If answ <> MsgBoxResult.OK Then
            MsgBox("stopped", MsgBoxStyle.OKOnly, )[/COLOR]
[COLOR=red]            End[/COLOR]
[COLOR=red]        Else[/COLOR]
[COLOR=red]            frmProgBar.Show()[/COLOR]
[COLOR=red]            GetTotalFileSize(New System.IO.DirectoryInfo(Source))
            _copyProgressRoutine = New CopyProgressRoutine(AddressOf CopyProgress)
            CopyFiles(New System.IO.DirectoryInfo(Source), Destination)[/COLOR]
[COLOR=red]        End If[/COLOR]
 
 

    End Sub
    Public Function CopyDirectory(ByVal Src As String, ByVal Dest As String, Optional _
  ByVal bQuiet As Boolean = False) As Boolean
        If Not Directory.Exists(Src) Then
            Throw New DirectoryNotFoundException("The directory " & Src & " does not exists")
        End If
        If Directory.Exists(Dest) AndAlso Not bQuiet Then
           If MessageBox.Show("directory " & Dest & " already exists." & vbCrLf & _
            "If you continue, any files with the same name will be overwritten", _
            "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2) = DialogResult.Cancel Then Exit Function
        End If
        'add Directory Seperator Character (\) for the string concatenation shown later
        'If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then
        '    Dest += Path.DirectorySeparatorChar
       ' End If
        If Not Directory.Exists(Dest) Then Directory.CreateDirectory(Dest)
        Dim Files As String()
        Files = Directory.GetFileSystemEntries(Src)
        Dim element As String
        For Each element In Files
            If Directory.Exists(element) Then
                'if the current FileSystemEntry is a directory,
                'call this function recursively
                CopyDirectory(element, Dest & Path.GetFileName(element), True)
            Else
                'the current FileSystemEntry is a file so just copy it
                File.Copy(element, Dest & Path.GetFileName(element), True)
            End If
        Next
        Return True
    End Function
    Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
        frmProgBar.ProgressBar1.Value = Convert.ToInt32((_totalBytesCopied + totalBytesTransferred) / _totalFileSize * 100)
        Application.DoEvents()
    End Function
    Private Sub GetTotalFileSize(ByVal folder As System.IO.DirectoryInfo)
        For Each fi As System.IO.FileInfo In folder.GetFiles
            _totalFileSize += fi.Length
        Next
        For Each di As System.IO.DirectoryInfo In folder.GetDirectories
            GetTotalFileSize(di)
        Next
    End Sub
    Private Sub CopyFiles(ByVal folder As System.IO.DirectoryInfo, ByVal destinationFolder As String)
        If Not System.IO.Directory.Exists(destinationFolder) Then
            System.IO.Directory.CreateDirectory(destinationFolder)
        End If
        For Each fi As System.IO.FileInfo In folder.GetFiles
            CopyFileEx(fi.FullName, destinationFolder & "\" & fi.Name, _copyProgressRoutine, 0, 0, 0)
            _totalBytesCopied += fi.Length
        Next
        For Each di As System.IO.DirectoryInfo In folder.GetDirectories
            CopyFiles(di, di.FullName.Replace(Source, Destination))
        Next
    End Sub

End Class

the part in red is the thing I can't get working.
The file copy on it's own works fine, but not with the OKCancel around it.

Thanks already guys and Girls,

Greetzzz,

CornElvis
 
I thought I solved it, but no. :

VB.NET:
Dim answ As Microsoft.VisualBasic.MsgBoxResult = MsgBoxResult.OK
        MsgBox("Following folder structure will be created: " & Chr(13) & Destination & Chr(13) & Chr(13) & "and the following data will be copied from " & Source, MsgBoxStyle.OKCancel, "Confirm")
        If answ <> MsgBoxResult.OK Then
            End
        Else
            frmProgBar.Show()
            GetTotalFileSize(New System.IO.DirectoryInfo(Source))
            _copyProgressRoutine = New CopyProgressRoutine(AddressOf CopyProgress)
            CopyFiles(New System.IO.DirectoryInfo(Source), Destination)
            MsgBox("Data move done, you can now install Lotus Notes 6.5.5", MsgBoxStyle.OKOnly, "Notes Data Mover")
        End If
        End
    End Sub

It does the copy anyway.
anyone, please?
 
You declare the answ variable and assign OK, or Cancel in your original code, to it. No matter what button is pressed in the message box you don't change that value so it will always be whatever you originally set no matter what. MsgBox is a function that returns the value that corresponds to the button that was pressed. You need to assign the result of MsgBox to the answ variable.

Having said that, there are a few other changes I would make. Firstly, you don't need a variable at all. Just test the retrun value directly. Secondly, don't use MsgBox at all. Whoever taught you that must have a VB6 background. Use MessageBox.Show in VB.NET. Thirdly, all that string concatenation is unnecessary and makes your code hard to read. I would do this:
VB.NET:
If MessageBox.Show(String.Format("You have chosen to copy all files and subfolders from ""{0}"" to ""{1}"".  Do you wish to continue?", Source, Destination), _
                   "Confirm Copy", _
                   MessageBoxButtons.OKCancel, _
                   MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
    'Perform operation.
End If
 
many many thanks to you jmcilhinney.
You are priceless.
I think I'm the one with a vb6 background here, as I started of with that a long time ago and picked it up after a few years with vb.net.

So there is some vb6 legacy in my head I suppose, where I have to get rid of :D

but still thanks for the response and making my code easier this way.

Greetzzz,

CornElvis
 
ai,

I was a bit to quick.
It doesn't give me an option to cancel only ok.
plus there was an ")" missing

VB.NET:
[LEFT]If MessageBox.Show(String.Format("You have chosen to copy all files and subfolders from ""{0}"" to ""{1}"".  Do you wish to continue?", Source, Destination), _
                   "Confirm Copy", _
                   MessageBoxButtons.OKCancel, _
                   MessageBoxIcon.Question)) = Windows.Forms.DialogResult.OK Then
    'Perform operation.
End If[/LEFT]
 
Just add an Else statement i.e.
VB.NET:
[LEFT][COLOR=blue]If [/COLOR]MessageBox.Show(String.Format("You have chosen to copy all files and subfolders from ""{0}"" to ""{1}"".  Do you wish to continue?", Source, Destination), _
                "Confirm Copy", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) = Windows.Forms.DialogResult.OK [COLOR=blue]Then[/COLOR]
 [COLOR=darkgreen]'Perform operation /// only for ilustration[/COLOR]
[COLOR=blue]Me[/COLOR][SIZE=2].Text = "Ok"[/SIZE][/LEFT]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text = "Cancel"[/SIZE]
[SIZE=2]
[/SIZE][COLOR=blue]End If[/COLOR]
 
somehow it just won't work this way.
Or it stops or it does the copy anyway.
Plus MessageBoxButtons.OKCancel doesn't show OK and Cancel it only shows OK.

somethings wrong here but what?

PS thanks for all the replies so far
 
Presumably if the user presses Cancel then you simply do nothing. It's only if they press OK that you actually need to do something.
 

Attachments

  • OKCancel.JPG
    OKCancel.JPG
    44.3 KB · Views: 61
Uf uf uf ... how much explanation about soooo simple task. Btw, why you complicate with those source and destination. I would simply warn the user or ask: You have choosen to copy all files and subfolders from one place to another. Do you wish to continue?
Then your code will be less confusing for you:
VB.NET:
If MessageBox.Show("You have choosen to copy all files and subfolders from one place to another. Do you wish to continue?", "Confirm Copy", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK [COLOR=blue]Then[/COLOR]
System.IO.File.Copy(source(), destination())
[SIZE=2][COLOR=#0000ff]Else [COLOR=teal]'optional[/COLOR]
[COLOR=teal]'Exit Sub[/COLOR] [COLOR=teal]'or something[/COLOR]
[COLOR=blue]End If[/COLOR]
[/COLOR][/SIZE]
 
Last edited:
alright here we go:
I tried on 2 different installations now, different code and whatsoever.
I only get an ok button and if I go for the cross which should make anything else then ok, it installs anyway.
Does this sound strange or what?

I changed the buttons to yesNo just for testing, but I only get the Ok button as well
 

Attachments

  • yep.JPG
    yep.JPG
    78.5 KB · Views: 84
From the looks of that MessageBox you've messed up the parameters because it's not showing the title, icon or the correct buttons. Rather than posting a screenshot in which we cant' actually see the full code, you need to post the actual code, or else you need to actually type out the code yourself and observe what Intellisense tells you about what parameter you're supplying.
 
Back
Top