Results 1 to 13 of 13

Thread: [RESOLVED] Confirmation dialog box not showing

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Resolved [RESOLVED] Confirmation dialog box not showing

    Code:
     Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            Try
                ' Check if the timer is running
                If timerRunning Then
                    ' Stop the timer and wait for it to finish
                    timerRunning = False
                    Thread.Sleep(200) ' Wait for the timer thread to finish gracefully
                End If
                ' Close the form
                Me.Invoke(Sub() Me.Close())
            Catch ex As Exception
                MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            Try
                ' Check if the timer is running and any of the radio buttons inside GrpOption1 is checked
                If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
                    ' If the timer is running and any of the radio buttons inside GrpOption1 is checked,
                    ' simulate a click on the start/stop button to stop the timer
    
                    BtnExit.PerformClick()
                End If
    
            Catch ex As Exception
                MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    If I put the code below, it is not appearing.
    Code:
    If MessageBox.Show("Do you want to close the form?", "Confirm", MessageBoxButtons.YesNo) <> DialogResult.Yes Then
                        e.Cancel = True
                    End If
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,155

    Re: Confirmation dialog box not showing

    Where are you putting that code?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Confirmation dialog box not showing

    Quote Originally Posted by Shaggy Hiker View Post
    Where are you putting that code?
    in btnExit
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  4. #4

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Confirmation dialog box not showing

    I tried this one, but still no confirmation dialog appeard.
    Code:
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            Try
                ' Check if the timer is running and any of the radio buttons inside GrpOption1 is checked
                If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
                    ' Display a confirmation dialog
                    Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    
                    ' Check the user's response
                    If result = DialogResult.No Then
                        ' Cancel the form closing event
                        e.Cancel = True
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  5. #5
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,546

    Re: Confirmation dialog box not showing

    Quote Originally Posted by Simply Me View Post
    I tried this one, but still no confirmation dialog appeard.
    Code:
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            Try
                ' Check if the timer is running and any of the radio buttons inside GrpOption1 is checked
                If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
                    ' Display a confirmation dialog
                    Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    
                    ' Check the user's response
                    If result = DialogResult.No Then
                        ' Cancel the form closing event
                        e.Cancel = True
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    If you put a breakpoint on the line, does the breakpoint get hit?

  6. #6

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Confirmation dialog box not showing

    Quote Originally Posted by PlausiblyDamp View Post
    If you put a breakpoint on the line, does the breakpoint get hit?
    it's not getting hit at all.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,464

    Re: Confirmation dialog box not showing

    Code:
    If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
        ' Display a confirmation dialog
        Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If that message box is not being shown then clearly at least one of those conditions is False. Put a breakpoint on the If statement and check which one. You can then work backwards to where you expected some change to occur that would make them True.

  8. #8

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Confirmation dialog box not showing

    Quote Originally Posted by jmcilhinney View Post
    Code:
    If timerRunning AndAlso (RdoCustomStopTime.Checked OrElse RdoPredefinedStopTime.Checked OrElse RdoNoStopTime.Checked) Then
        ' Display a confirmation dialog
        Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If that message box is not being shown then clearly at least one of those conditions is False. Put a breakpoint on the If statement and check which one. You can then work backwards to where you expected some change to occur that would make them True.
    I tried putting breakpoint in if statement but when I clicked the x button or the btnexit, the form just closes with out hitting the breakpoint I put.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,464

    Re: Confirmation dialog box not showing

    I wouldn't have thought so but I wonder whether your Invoke call has anything to do with it. That's pointless anyway so you should get rid of it and just call Close. See whether that makes a difference.

  10. #10

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RESOLVED] Confirmation dialog box not showing

    It is working now. Thanks jmcilhinney.
    Code:
     
    
      Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            Try
                ' Check if the timer is running
                If timerRunning Then
                    ' Stop the timer and wait for it to finish
                    timerRunning = False
                    Thread.Sleep(200) ' Wait for the timer thread to finish gracefully
                End If
    
                ' Close the form, which will trigger the FormClosing event
                Me.Close()
    
            Catch ex As Exception
                MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            Try
                If timerRunning Then
                    ' Stop the timer and wait for it to finish
                    timerRunning = False
                    Thread.Sleep(200) ' Wait for the timer thread to finish gracefully
                End If
    
                ' Check if the close reason is user closing via the X button
                If e.CloseReason = CloseReason.UserClosing Then
                    ' Display a confirmation dialog
                    Dim result As DialogResult = MessageBox.Show("Are you sure you want to exit the application?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    
                    ' Check the user's response
                    If result = DialogResult.No Then
                        ' Cancel the form closing event
                        e.Cancel = True
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show("An error occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,464

    Re: [RESOLVED] Confirmation dialog box not showing

    You would only need to invoke that method on the UI thread if the current method could be executing on a different thread. The Click event handler of a Button is never going to be, so invoking is pointless. If you were in the situation where you might need to call Invoke, you could check InvokeRequired first and only call Invoke if necessary. Alternatively, I wonder whether BeginInvoke may not have had the same issue.

  12. #12

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: [RESOLVED] Confirmation dialog box not showing

    Quote Originally Posted by jmcilhinney View Post
    You would only need to invoke that method on the UI thread if the current method could be executing on a different thread. The Click event handler of a Button is never going to be, so invoking is pointless. If you were in the situation where you might need to call Invoke, you could check InvokeRequired first and only call Invoke if necessary. Alternatively, I wonder whether BeginInvoke may not have had the same issue.
    I used the invoke because I was getting invoke and begin invoke error when I click the exit button while the timer is running. Anyways, thanks much.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,776

    Re: [RESOLVED] Confirmation dialog box not showing

    I have a strategy I use when closing form apps that have become 'complicated'. It is built-in to our standard app template.

    We define a 'Closing / Stopping' synchronization object in the form of a Threading.ManualResetEvent. For all long running or loopy things this is checked.

    When the form is closed we treat it as a two step process. Here is the basic template code for form closing.

    Code:
    Public Class Form1
    
        Private isStop As New Threading.ManualResetEvent(False)
    
        Private firstClose As Boolean = True
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If _FormClosingHelperThrd IsNot Nothing AndAlso
                   _FormClosingHelperThrd.Status <> TaskStatus.RanToCompletion Then
                e.Cancel = True 'wait for helper to complete
            ElseIf firstClose Then
                _FormClosingHelperThrd = Task.Run(Sub() _FormClosingHelper()) 'fire up the helper
                firstClose = False
                e.Cancel = True 'cancel this closing
            Else
            End If
        End Sub
    
        Private _FormClosingHelperThrd As Task = Nothing
        Private Sub _FormClosingHelper()
            '
            'this NON UI task does closing stuff
            '
    
            'when closing stuff done close again
            Me.BeginInvoke(Sub()
                               Me.Close() 'second close
                           End Sub)
        End Sub
    End Class
    Here is a longer example with some simulated 'work'. Create a form and add two labels named, Label1 and Label2. Also add a button named BtnExit.

    Code:
    Public Class Form1
        Private isStop As New Threading.ManualResetEvent(False)
        Private MyTasks As New List(Of Task)
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            'generate simulated work
            MyTasks.Add(Task.Run(Sub() tskTMR()))
            MyTasks.Add(Task.Run(Sub() tskA()))
            MyTasks.Add(Task.Run(Sub() tskB()))
            Label2.Text = "RUN"
        End Sub
    
        Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            Me.Close()
        End Sub
    
        Private firstClose As Boolean = True
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If _FormClosingHelperThrd IsNot Nothing AndAlso
                   _FormClosingHelperThrd.Status <> TaskStatus.RanToCompletion Then
                e.Cancel = True 'first close running
            ElseIf firstClose Then
                If MessageBox.Show("Are you sure?", "AYS", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                    e.Cancel = True
                    Exit Sub
                End If
                _FormClosingHelperThrd = Task.Run(Sub() _FormClosingHelper()) 'fire up the helper
                firstClose = False
                Label2.Text = "FIRST CLOSE"
                e.Cancel = True 'cancel this closing
            Else
                Stop
            End If
        End Sub
    
        Private _FormClosingHelperThrd As Task = Nothing
        Private Sub _FormClosingHelper()
            '
            'this NON UI task does closing stuff
            '
            Me.BeginInvoke(Sub()
                               Threading.Thread.Sleep(1000)
                               Label2.Text = "HELPER"
                           End Sub) 'second close
            isStop.Set() 'signal that app wants to stop
            'these should all be .Set when all of the tasks are done
            ' timeout is set in case the .Set is not reached
            ' IF using timeout it must be appropriate
            _tskTmrComp.WaitOne()
            _tskAComp.WaitOne(5000)
            _tskBComp.WaitOne(5000)
            Task.WaitAll(MyTasks.ToArray) 'wait for all tasks to end
            _tskTmrComp.Dispose()
            _tskAComp.Dispose()
            _tskBComp.Dispose()
            Me.BeginInvoke(Sub()
                               Me.Close() 'second close
                           End Sub)
        End Sub
    
        '<*><*><*><*> Simulated work <*><*><*><*>
    
        Private _tskTmrComp As New Threading.ManualResetEvent(False)
        Private Sub tskTMR()
            While Not isStop.WaitOne(0)
                _tskTmrComp.WaitOne(100) 'simulated work
                Me.BeginInvoke(Sub()
                                   Label1.Text = DateTime.Now.ToString("HH:mm:ss.f")
                               End Sub)
            End While
            _tskTmrComp.Set() 'this thread done
        End Sub
    
        Private _tskAComp As New Threading.ManualResetEvent(False)
        Private Sub tskA()
            While Not isStop.WaitOne(0)
                Threading.Thread.Sleep(500) 'simulated work
            End While
            Threading.Thread.Sleep(3000) 'simulate long running ;)
            _tskAComp.Set() 'this thread done
        End Sub
    
        Private _tskBComp As New Threading.ManualResetEvent(False)
        Private Sub tskB()
            While Not isStop.WaitOne(0)
                Threading.Thread.Sleep(250) 'simulated work
            End While
            Threading.Thread.Sleep(1000)
            _tskBComp.Set() 'this thread done
        End Sub
    End Class
    Hope this sparks some ideas.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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