Question problem with closing the dialog when passing DialogResult.Ok

pooya1072

Well-known member
Joined
Jul 5, 2012
Messages
84
Programming Experience
Beginner
hi
there is a button on my main form . when i click it ,my message form (dynamically is created and content a TableLayoutPanel with a label and a button "Ok") will display . the problem is :
when i click "ok" button (for first time) nothing is happend . but for 2th time i click , then the dialoge will close . normally if i add button on designer every thing is ok , but in dynamically adding this problem is occured. i mean i must click on ok button twice to close the dialog ....and i don't want it
this is whole program :
VB.NET:
Imports System.Windows.Forms
 
Public Class FaDialogs
    Private frm As New MsgForm
    Private WithEvents tlp As TableLayoutPanel
    Private WithEvents btn As Button
    Private WithEvents lbl_Message As Label
 
    Public Function FaMsgBox(ByVal msg As String, ByVal DlgStyle As MsgBoxStyle) As Boolean
 
        Select Case DlgStyle
            Case MsgBoxStyle.OkOnly
                'Label
                lbl_Message = New Label
                lbl_Message.Text = msg
                lbl_Message.Dock = DockStyle.Fill
                lbl_Message.Margin = New Padding(15)
 
                'Button
                btn = New Button
                btn.Name = "btn_Action"
                btn.Text = "Ok"
                btn.Anchor = (AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Left Or AnchorStyles.Top)
                btn.Margin = New Padding(100, 10, 100, 10)
 
                'TabelLayoutPanel
                tlp = New TableLayoutPanel
                tlp.ColumnCount = 1
                tlp.RowCount = 2
                tlp.Dock = DockStyle.Fill
                tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 65.0!))
                tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 35.0!))
                tlp.Controls.Add(btn, 0, 1)
                frm.Controls.Add(tlp)
                ExecForm.Text = frm.ShowDialog().ToString
         End Select
        Return True
    End Function
 
 
    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        btn.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class

and this is callig from main form :
VB.NET:
Public Class ExecForm
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New FaDialogs
        a.FaMsgBox(" this is test .", MsgBoxStyle.OkOnly)
    End Sub
End Class
 
Last edited:
The reason nothing is happening on the first click is that you are setting the Button's DialogResult property in the Click event handler rather than the form's. Setting the form's DialogResult property to something other than None is what closes the form. Setting the Button's DialogResult property does NOT close the form; it only says that when you click the Button it will set the form's DialogResult property to that same value. So, on the first click you are telling the Button that it should set the form's DialogResult property to OK on future clicks. You have two main choices to make this work:

1. Get rid of this:
    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        btn.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
and change this:
                'Button
                btn = New Button
                btn.Name = "btn_Action"
                btn.Text = "Ok"
                btn.Anchor = (AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Left Or AnchorStyles.Top)
                btn.Margin = New Padding(100, 10, 100, 10)
to this:
                'Button
                btn = New Button
                btn.Name = "btn_Action"
                btn.Text = "Ok"
                btn.Anchor = (AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Left Or AnchorStyles.Top)
                btn.Margin = New Padding(100, 10, 100, 10)
                btn.DialogResult = Windows.Forms.DialogResult.OK
2. Change this:
    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        btn.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
to this:
    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        frm.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
I would go with the first option because that class should absolutely not be handling the Click event of that Button. Also, that class should not have any members that are not Shared.
 
Back
Top