Imports System.IO

Public Class frmRODSelectPage

    Private Sub btnSelectImages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectImages.Click

        '--------------DECLARE VARIABLES------------------------------------------------------------------------
        Dim FileName As String
        Dim Export As String 'filename only
        Dim DidWork As Integer
        Dim InitialDirectory As String = "E:\Hasbro\CaptureImageFolder\"
        Dim HotFolder As String = "E:\Hasbro\CaptureHotFolder\"
        '--------------END VARIABLES-----------------------------------------------------------------------------


        '--------------EXCEPTION HANDLING------------------------------------------------------------------------
        'If user pushes cancel before selecting file this error displays
        If DidWork = DialogResult.Cancel Then
            Me.Show()
        End If
        '---------------END EXCEPTION HANDLING-------------------------------------------------------------------


        '----------------FILE COPY PROCEDURE---------------------------------------------------------------------
        If OpenFD.ShowDialog = DialogResult.OK Then
            txtShowFileCopy.AppendText(vbNewLine & "----------" & DateAndTime.TimeString & "---------------" & My.User.Name & "----------" & vbNewLine)
            FileName = OpenFD.FileName 'sets file name to the variable FileName
            Export = Path.GetFileName(FileName) 'sends only the filename and extension to the variable export
            For Each selFile As String In OpenFD.FileNames
                Export = Path.GetFileName(selFile) 'sends only the filename and extension to the variable export       
                txtShowFileCopy.AppendText("SUCESSFULLY COPIED ----> " & selFile.ToUpper & vbNewLine) 'displays the files that are being copied in the textbox 
                If Export = "" Or FileName = "" Or HotFolder = "" Then
                    MsgBox("Aw Snap! Something went bad there." & vbNewLine & "Please close the application and try again", MsgBoxStyle.Information, "ERROR WILL ROBINSON!")
                End If
                FileCopy(selFile, HotFolder & Export)
            Next

        End If
        '------------------END FILE COPY------------------------------------------------------------------------
    End Sub


    '----------------------EXIT ROUTINE---------------------------------------------------------------------

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo, "Exit Program?")
        System.IO.File.AppendAllText("E:\MILWAUKEE\FlexiCaptureImageFolder\log.txt", txtShowFileCopy.Text)
        Me.Close()
    End Sub

    


    '----------------------END EXIT ROUTINE-----------------------------------------------------------------

    
    Private Sub frmRODSelectPage_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo, "Exit Program?")
        System.IO.File.AppendAllText("E:\MILWAUKEE\FlexiCaptureImageFolder\log.txt", txtShowFileCopy.Text)
        Me.Close()
    End Sub
End Class

Dani AI

Generated

Short diagnosis: the form is re-entering its closing logic because a call to Close (or Me.Close) inside the FormClosing handler triggers FormClosing again. That creates a loop of repeated confirmations. ’s handler both asks the user and then calls Me.Close inside FormClosing; is right that the Me.Close call should be removed. The clean patterns are (A) confirm in the Exit button and only call Close when the user agrees, or (B) confirm inside FormClosing but never call Close from that event — set e.Cancel = True to stop closing.

Example pattern (use a different control name and a simple log path):

Private closingConfirmed As Boolean = False

Private Sub ExitButton_Click(...) Handles ExitButton.Click
    Dim ok = MessageBox.Show("Confirm exit now?", "Confirm", MessageBoxButtons.YesNo) = DialogResult.Yes
    If ok Then
        closingConfirmed = True
        Me.Close()
    End If
End Sub

Private Sub ThisForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    If Not closingConfirmed Then
        If MessageBox.Show("Really quit?", "Confirm", MessageBoxButtons.YesNo) = DialogResult.No Then
            e.Cancel = True    ' stops the close without calling Close again
            Return
        End If
    End If

    System.IO.File.AppendAllText("C:\Temp\myapp.log", MyLogTextBox.Text)
End Sub

Troubleshooting tips: place a breakpoint at the top of FormClosing and inspect the call stack to see who invoked it; add a temporary counter or timestamped log line to detect re-entrance; check for other code paths that call Close (other forms, Application.Exit, background threads). Also respect e.CloseReason so you don’t prompt during system shutdown. For reference on how FormClosing and Close interact, see the Microsoft docs: Form.FormClosing and Form.Close.

See if this helps.

'----------------------EXIT ROUTINE---------------------------------------------------------------------
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close() '// cause app to close.
    End Sub
    '----------------------END EXIT ROUTINE-----------------------------------------------------------------
    Private Sub frmRODSelectPage_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '// when app. closes, it fires off this event just before it is done and will close.  no need to add Me.Close here.
        MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo, "Exit Program?")
        System.IO.File.AppendAllText("E:\MILWAUKEE\FlexiCaptureImageFolder\log.txt", txtShowFileCopy.Text)
    End Sub
commented: Excellent response!!! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.