Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Created a new form as a generic dialog result box, but 2 of the 3 buttons need to be clicked twice to work?

I have created a simple additional form (frmDialog) to my application. It contains 3 buttons and a label. The idea is to create a generic dialog result box to my main app.

Properties –
      Form:
      FormBorderStyle:   FixedDialog
      CancelButton:         Button3

Button1:
      DialogResult:      “No”

Button2:
    DialogResult:      “Yes”

--------------------------------------------------------------------------------
The method call used throughout my app supplying different strings in the new form:

               ShowMyDialogForm("This Label Message.")
---------------------------------------------------------------------------------

The method to show frmDialog, display the message, and get the dialog result:

   Private Sub ShowMyDialogForm (ByVal MessageToDisplay As String)
        Dim dlg As New frmDialog
        dlg.DisplayMessage(MessageToDisplay)
        If dlg.ShowDialog = DialogResult.No Then
           Debug.WriteLine("No Was Pressed")
        ElseIf dlg.ShowDialog = DialogResult.Yes Then
            Debug.WriteLine("Yes Was Pressed")
        End If
    End Sub
---------------------------------------------------------------

The only method in the frmDialog to receive and display the message:

Public Sub DisplayMessage (ByVal Message As String)
        Me.lblMessage.Text = Message
End Sub

----------------------------------------------------------------
PROBLEM:

Click Button1 – OK    but...

Both Button2 and Button3 need to be clicked twice before the respective Debug.WriteLine message/Cancel of frmDialog happens.

Please, why? And how to resolve?

A big thanks in advance.
0
IvanHowarth
Asked:
IvanHowarth
1 Solution
 
TimCotteeCommented:
Hi IvanHowarth,

Because you are showing the dialog more than once:

   Private Sub ShowMyDialogForm (ByVal MessageToDisplay As String)
        Dim dlg As New frmDialog
        Dim result as DialogResult
        dlg.DisplayMessage(MessageToDisplay)
        result = dlg.ShowDialog
        If result = DialogResult.No Then
           Debug.WriteLine("No Was Pressed")
        ElseIf result = DialogResult.Yes Then
            Debug.WriteLine("Yes Was Pressed")
        End If
    End Sub

Is better, each time you use dlg.ShowDialog, the dialog is shown so your code forces it to be opened twice. You should save the result of a single opening and then test this for the various results.
Tim Cottee
0
 
IvanHowarthAuthor Commented:
Perfect - thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now