locked
[UWP][VB.NET] Button click event gets called twice when a messagedialog is present??? RRS feed

  • Question

  • I am having a problem with a messagedialog being shown twice because a button click event is called twice. Here is my code which causes the problem:

       If addedtodb = False Then
                        Dim r As Task(Of Boolean) = itemadd.EnterNewItem(iteminform, 1, ItemType.LimitedEdition)
                        r.Wait()
    
                        If r.Result = True And r.IsCompleted = True Then
                            'if item is successfully added to database check out if they want to evaluate the item and send them to evaluation form.
                            If result Is Nothing Then
    
                                If DirectCast(sender, Button).IsPointerOver = True Then
                                    result = Await evalmsg.ShowAsync()
    
                                Else
                                    Log("Not doing anything or error on item entry...")
                                End If
    
                                If result IsNot Nothing Then
                                    If result.Label = "Evaluate" Then
                                        If successadd.ShowAsync.Status = AsyncStatus.Completed Then
                                            Await successadd.ShowAsync()
                                            Await System.Threading.Tasks.Task.Delay(1000)
                                            addedtodb = True
                                            result = Nothing
                                            evalmsg = Nothing
                                            Frame.Navigate(GetType(EvaluateItem))
                                        End If
                                    End If
                                ElseIf result.Label = "Do Not Evaluate" Then
                                        Await successadd.ShowAsync()
                                    Await System.Threading.Tasks.Task.Delay(1000)
    
                                    addedtodb = True
                                    result = Nothing
                                    evalmsg = Nothing
                                    Frame.Navigate(GetType(MainPage))
                                End If
                            Else
                                Exit Sub
                            End If
    
    
    
                        End If
                    End If

    This current code only runs once but does not wait for the messagedialog to display before going through the click event again which makes the above false and does not run.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Tuesday, February 13, 2018 10:55 PM

Answers

  • I am having a problem with a messagedialog being shown twice because a button click event is called twice. Here is my code which causes the problem:

       If addedtodb = False Then
                        Dim r As Task(Of Boolean) = itemadd.EnterNewItem(iteminform, 1, ItemType.LimitedEdition)
                        r.Wait()
    
                        If r.Result = True And r.IsCompleted = True Then
                            'if item is successfully added to database check out if they want to evaluate the item and send them to evaluation form.
                            If result Is Nothing Then
    
                                If DirectCast(sender, Button).IsPointerOver = True Then
                                    result = Await evalmsg.ShowAsync()
    
                                Else
                                    Log("Not doing anything or error on item entry...")
                                End If
    
                                If result IsNot Nothing Then
                                    If result.Label = "Evaluate" Then
                                        If successadd.ShowAsync.Status = AsyncStatus.Completed Then
                                            Await successadd.ShowAsync()
                                            Await System.Threading.Tasks.Task.Delay(1000)
                                            addedtodb = True
                                            result = Nothing
                                            evalmsg = Nothing
                                            Frame.Navigate(GetType(EvaluateItem))
                                        End If
                                    End If
                                ElseIf result.Label = "Do Not Evaluate" Then
                                        Await successadd.ShowAsync()
                                    Await System.Threading.Tasks.Task.Delay(1000)
    
                                    addedtodb = True
                                    result = Nothing
                                    evalmsg = Nothing
                                    Frame.Navigate(GetType(MainPage))
                                End If
                            Else
                                Exit Sub
                            End If
    
    
    
                        End If
                    End If

    This current code only runs once but does not wait for the messagedialog to display before going through the click event again which makes the above false and does not run.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Nevermind, solved by using the invoke handlers for each command seperately and it makes my main code neater too!

    Here's the handlers for reference in-case anyone needs a confirmation dialog  shown with little code after entering an item:

      Private Async Sub EvaluateItem(ByVal command As IUICommand)
            If command.Label = "Evaluate" Then
                Await successadd.ShowAsync()
                Await System.Threading.Tasks.Task.Delay(1000)
                addedtodb = True
                Frame.Navigate(GetType(EvaluateItem))
            End If
        End Sub
        Private Async Sub NoEvaluateItem(ByVal command As IUICommand)
            If command.Label = "Do Not Evaluate" Then
                Await successadd.ShowAsync()
                Await System.Threading.Tasks.Task.Delay(1000)
                addedtodb = True
                Frame.Navigate(GetType(MainPage))
            End If
        End Sub

    Note: only works/tested with one confirmation dialog after the initial one and may crash if shown anymore during the process.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    • Marked as answer by The Thinker Tuesday, February 13, 2018 11:40 PM
    Tuesday, February 13, 2018 11:40 PM

All replies

  • I am having a problem with a messagedialog being shown twice because a button click event is called twice. Here is my code which causes the problem:

       If addedtodb = False Then
                        Dim r As Task(Of Boolean) = itemadd.EnterNewItem(iteminform, 1, ItemType.LimitedEdition)
                        r.Wait()
    
                        If r.Result = True And r.IsCompleted = True Then
                            'if item is successfully added to database check out if they want to evaluate the item and send them to evaluation form.
                            If result Is Nothing Then
    
                                If DirectCast(sender, Button).IsPointerOver = True Then
                                    result = Await evalmsg.ShowAsync()
    
                                Else
                                    Log("Not doing anything or error on item entry...")
                                End If
    
                                If result IsNot Nothing Then
                                    If result.Label = "Evaluate" Then
                                        If successadd.ShowAsync.Status = AsyncStatus.Completed Then
                                            Await successadd.ShowAsync()
                                            Await System.Threading.Tasks.Task.Delay(1000)
                                            addedtodb = True
                                            result = Nothing
                                            evalmsg = Nothing
                                            Frame.Navigate(GetType(EvaluateItem))
                                        End If
                                    End If
                                ElseIf result.Label = "Do Not Evaluate" Then
                                        Await successadd.ShowAsync()
                                    Await System.Threading.Tasks.Task.Delay(1000)
    
                                    addedtodb = True
                                    result = Nothing
                                    evalmsg = Nothing
                                    Frame.Navigate(GetType(MainPage))
                                End If
                            Else
                                Exit Sub
                            End If
    
    
    
                        End If
                    End If

    This current code only runs once but does not wait for the messagedialog to display before going through the click event again which makes the above false and does not run.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Nevermind, solved by using the invoke handlers for each command seperately and it makes my main code neater too!

    Here's the handlers for reference in-case anyone needs a confirmation dialog  shown with little code after entering an item:

      Private Async Sub EvaluateItem(ByVal command As IUICommand)
            If command.Label = "Evaluate" Then
                Await successadd.ShowAsync()
                Await System.Threading.Tasks.Task.Delay(1000)
                addedtodb = True
                Frame.Navigate(GetType(EvaluateItem))
            End If
        End Sub
        Private Async Sub NoEvaluateItem(ByVal command As IUICommand)
            If command.Label = "Do Not Evaluate" Then
                Await successadd.ShowAsync()
                Await System.Threading.Tasks.Task.Delay(1000)
                addedtodb = True
                Frame.Navigate(GetType(MainPage))
            End If
        End Sub

    Note: only works/tested with one confirmation dialog after the initial one and may crash if shown anymore during the process.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    • Marked as answer by The Thinker Tuesday, February 13, 2018 11:40 PM
    Tuesday, February 13, 2018 11:40 PM
  • Hi Thinker,

    Glad to hear that you have solved your problem.

    It is appreciated to share your solution here.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 14, 2018 2:44 AM