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
 
jmcilhinney said:
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.
:confused:
the link is underneath there!!!!!
I posted all the code in here as well (first post) and set it up for download from my webserver. What else do you need.
And yes I'm using 2003.
Is this a problem? The code here should work anyway, shouldn't it?

greetzzz,

CornElvis
 
I don't want to download your whole project. You b*ggered up one line of code and that's all I need to see. Why would I want to download your whole project and open it, then search for the issue when I know that you just need to fix one line? Plus you've changed the code you're using since that first post so that is completely useless now. Post the one line of code that calls the message box. We'll tell you what's wrong with it and the job's done. Why make it harder for those trying to help you?
 
somehow this worked:

VB.NET:
[SIZE=2][COLOR=#0000ff]
If[/COLOR][/SIZE][SIZE=2] (MessageBox.Show("You have chosen to copy all files and folders from: " & Chr(13) & Source & Chr(13) & "to: " & Chr(13) & Destination, "Notes Data Mover", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = DialogResult.OK) [/SIZE][SIZE=2][COLOR=#0000ff]Then
 
[/COLOR][/SIZE][SIZE=2]frmProgBar.Show()
GetTotalFileSize([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.IO.DirectoryInfo(Source))
_copyProgressRoutine = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] CopyProgressRoutine([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] CopyProgress)
CopyFiles([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.IO.DirectoryInfo(Source), Destination)
MessageBox.Show("Data move done, you can now install Lotus Notes 6.5.5", "Notes Data Mover", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("Action Canceled", "Notes Data Mover", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

thanks for the patience or not....

Greetzz,

CornElvis
 
The error to your original post in this code....

VB.NET:
  [COLOR=red]Dim answ As Microsoft.VisualBasic.MsgBoxResult = MsgBoxResult.Cancel
        MsgBox( ....................)
        If answ <> MsgBoxResult.OK Then................[/COLOR]

you are declaring answ = Cancel

instead it should be....

VB.NET:
[COLOR=blue]Dim answ As Microsoft.VisualBasic.MsgBoxResult[/COLOR]
[COLOR=red][COLOR=blue]answ =[/COLOR]  MsgBox(..........)
        If answ <> MsgBoxResult.OK Then.........
           [/COLOR]

That's all you needed.
 
Last edited by a moderator:
ImDa .. the discussion moved on to:

"Use MessageBox.Show, not MsgBox"


Unfortunately, MessageBox.Show and MsgBox do NOT have the same method signature, so the original poster took his code:
answer = MsgBox("message", MsgBoxStyle.OKCancel, "caption")


and changed it to
answer = MessageBox.Show("message")




and then complained a lot about it not showing icons, captions, buttons etc - of course, it wont.. he hadnt told it to do so.
he then posted screenshots of code, but covered up the one line of code we wanted to look at..

very amusing..

so we get out of this:

MsgBox is a wrapper for MessageBox.Show. It is a legacy compatibility method that really shouldnt be used. Becuase it is old vb6, and old vb6 was horrible, backwards and inconsistent, it is not exactly the same signature as MessageBox.Show.
In using MessageBox.Show we must also ensure we provide all the options and in the right order (message, caption, buttons, icon.. i believe)



All this could have been solved a little more simply by giving a better code example when saying "use MessageBox.Show instead" but at the same time, the poster of this advice could reasonably have expected the OP to go and look up how to use that new function, not just assume it was the same as MsgBox.. so it's 6 of one and half a dozen of the other.



I have a couple of observations of my own:

If youre asking a question that requires a YES NO answer, dont give the user OK Cancel dialogue box!

Consider the difference between these:
"The item you are copying already exists in the destination. Do you want to overwrite it?" YES NO
"The copy process is about to start. Items that are present in the destination will be overwritten wihtout warning" OK CANCEL

yes/no = for questions. ok/cancel = for confirmation of statements of intent



My second observation:

Dont make strings like this:
"this is " & chr(13) & chr(10) & & chr(13) & chr(10) & "my multiline" & vbcrlf & vbcr & vblf & "string"
it just looks a mess. Try this:

String.Format("This is{0}{0}my multiline{0}{0}string", Environment.NewLine())
This will be more compatible across varying environments too




an another tip here:

Go to your Project's properties, and in the references, UNTICK Microsoft.VisualBasic

this will ensure youre not using any horrible old legacy compatibility code from the Microsoft.VisualBasic namespace. Every MsgBox, CStr etc call will suddenly become a compiler error and you can fix them by replacing them with their proper .net equivalents..
 
I agree. But msgbox is just an overloaded function in VB 2005 for the messagebox.show() I know in old VB6 it was different but this one is straight the .net framework.

from MS
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchMicrosoftVisualBasicNETInternals.asp

MsgBox versus MessageBox.Show







In Visual Basic .NET, the MsgBox method is a Visual Basic Runtime wrapper around a call to the MessageBox.Show method from the System.Windows.Forms namespace. MsgBox does some extra work to emulate the behavior of the Visual Basic 6 MsgBox function before culminating in a call to MessageBox.Show. The minute cost of these emulating steps is insignificant, particularly when compared to the time it takes a user to react to a dialog box.
Note MsgBox returns the same integer values returned by MessageBox.Show. Strictly speaking MessageBox.Show returns a DialogResult value and MsgBox returns a MsgBoxResult value. The values in these enumerations have the same meanings: OK = 1, Yes = 6, No = 7, and so on. You can also use CType to convert a MsgBoxResult to a DialogResult.​
The choice between MsgBox and MessageBox is a matter of consistency. If you are migrating a Visual Basic 6 application to Visual Basic .NET, there is no compelling reason to replace calls to MsgBox with MessageBox.Show.
Recommendation: Use MsgBox throughout your code.

And I'm not starting an argument, just pointing something out, but it dosn't matter which you call. They opererate the same.


Also, look at that site for VB functions that are preffered. MS admitts that some of the VB functions are maginally faster than the System namespace. Of course we want to convert to the latest and greatest but no need to ommit what works fine if not better sometimes. :)

additionally; there are also System Namespace functions that are definetaly better VB Functions. That site lists most all of them and their differences. Have fun :)
 
Last edited:
I'd say it is all correct in context - MsgBox and MessageBox.Show are synonymous, and the MsgBox entry in the Microsoft.VisualBasic namespace is a VB6 syntax-compatible way of having MessageBox.Show called...


It's important to assign the correct context though. From the same MS quote you posted, im going to just highlight a bit more:

If you are migrating a Visual Basic 6 application to Visual Basic .NET, there is no compelling reason to replace calls to MsgBox with MessageBox.Show.
Recommendation: Use MsgBox throughout your code.


These are indeed different sentences, but the inquiring mind can easily assemble a sensible context out of this.

Migration of an app is made simpler by the presence of Microsoft.VisualBasic, and too, it is sometimes unfeasible to rewrite every old function to be compatible with new ideas.. Much as the choice remains with the developer, it would be sensible to use non legacy aspects of the namespaces available when embarking on a new .NET development rather than migrating an old VB6 one. One of the reasons I can think of immediately is that it becomes more readable and cross-(de)compilable between other .NET developers. In the goal of introducing several syntaxes that compile to one overall IL, settling on the use of a consistent syntax makes it easier for other humans to switch between

In no way am i saying that youre wrong for using MsgBox - i'm reflecting a fairly widely regarded notion among new .net programmers and OO professionals that legacy routines arent helpful overall - as an example I dont think you'd find any of the moderators here strongly advocating MsgBox over MessageBox.Show, especially the ones that are C# capable too. If youve used MsgBox that's fine.. i personally wouldnt advise a newbie to do so because if they learn MessageBox.Show etc then their conversion to C# etc will be so much easier (and in an industry where C# programmers get paid more and have a higher considered status [however perverse that may seem], increasing someone's ability to apply for a C# job, only ever having done VB.NET and a bit of C syntax maybe... is the fair option)

In a word(s); one shouldnt limit ones-self to old style VB6 syntax - its gone and dead (hurrah)! Embrace MessageBox.Show and stay out of Collections. In the strive for self improvement, I guess that we should know as much as possible then we can decide when to apply it. :D

there are also System Namespace functions that are definetaly better VB Functions. That site lists most all of them and their differences. Have fun :)
I'll look it up! thanks for the tip...
 
cornelvis said:
somehow this worked:

Oh btw, I also just wanted to inform you of this "Gotcha" that I noticed when looking at your screenshot - its got me too a couple of times :)


You were wondering why your YesNo and Icons werent showing...

Well here are two lines of code that are very subtly different:


MessageBox.Show(String.Format("Hello World {0}{1}{2}"), "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
MessageBox.Show(String.Format("Hello World {0}{1}{2}", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question))


Can you see it? in the first one - it's right. In the second one was the tiny problem with the code you screenshotted.. The bracket for String.Format ends in the wrong place, so your Caption, Buttons and Icon are passed to string.format, rather than the messagebox :)

In my example above, the following messageBox texts would be seen resepectively:

_____________________
Caption
(?) Hello World {0}{1}{2}
[YES] [NO]
_____________________


_____________________

Hello World CaptionYesNoQuestion
[OK]
_____________________


I hope this will help you avoid this Gotcha in future..
 
Well, what I said was it was just an overloaded function that really internally calls the MessageBox.Show() class. Consider this. If you wrote code that you use over and over in many programs then it would be helpful to build an overloaded functions that accepts many different options to accomplish various versions of that same code. That is all the msgbox() is. It still calls the MessageBox.Show() directly from the framework but the parameters are quite simpler I think. Plus it's six letters and really quick for the exact same thing when it boils down. So yes, in C# your going to rely pretty much completely on the .NET framework but with VB many functions and VB language features (part of the compiling process) are easier and more efficient. Then again, some are not. I myself made a function to call certain forms in certain ways. For example I made an overloaded function that fades in a form. I call it in any program I want by adding the reference to the dll.
VB.NET:
Form_FadeIn(Form as Form, Optional ShowDialog)

That's one way to call it but you can see what I mean. MsgBox isn't a different MsgBox but just another way to call it and help old VB convert. It still have time saving purposes for me so I always use it. I started using MessageBox.Show() until I learned what I just told you and so I went back to MsgBox for ease of use.

and you pulled that quote from my last line but left out the one before it...

Also, look at that site for VB functions that are preffered. MS admitts that some of the VB functions are maginally faster than the System namespace.
 
Last edited:
Sure sure.. I think we are both making the same point after all, and I agree it's quicker to type MsgBox - i just found that if I did, I went and wrote it in my C# programs and i had to correct it :) (Its weird developing VBN and C# in parallel) - standardising on the one that works in both helps me save time in the same way that MsgBox helps you save time :)

Form an OO viewpoint you might choose to make a Form Fade in and out by making a parent class called FadingForm and put th relevant code in that. You can then inherit that form and al your forms that inherit it become fading. One of the OO paradigms is that generally, objects don't mess with other object's data. It gets broken all the time, i know, but It's possible to keep to it reasonably in the majority of cases..

It depends on the type of programmer you want to be, and ultimately I guess you should be whatever type gets the job done to spec! :)
 
I agree, and I also form inheritance as well for major changes in the form such as adding custom properties and overriding base methods. If you are programming parrallel with C# then I can see the need to always use System Namespace functions. I have a question about C# but not to start this thread on a tangent I will send you a private message.
 
Back
Top