Jay Chuah Jay Chuah - 1 year ago 222
Vb.net Question

frm.showDialog dispose when openFileDialog close [vb.net]

I have two forms. First form is used to display a set of record and second form is used to edit the particular record. I called the second form using frm.ShowDialog(). Inside that form I got a button to call the OpenFileDialog. When I press OK or Cancel, then the second form dispose together with the OpenFileDialog. I'm pretty should that my code is correct, but it was the ShowDialog() problem. Anyone have idea on this issue?

This is how i called the second form from the first form to display the Information.

Private Sub btnViewOrganizationEdit_Click(sender As Object, e As EventArgs) Handles btnViewOrganizationEdit.Click, dgvOrganization.DoubleClick
Dim selectedOrganization As New Organization
'check permission because double click
If dgvOrganization.RowCount > 0 Then
strOrganizationID = dgvOrganization.SelectedRows.Item(0).Cells(0).Value
selectedOrganization = helperOrganizationCKJ.getOrganizationByID(strOrganizationID)
frmEditOrganizationCKJ.objOrganization = selectedOrganization
End If
End Sub

This is how i called the OpenFileDialog.

Private Sub btnEditOrganizationImage_Click(sender As Object, e As EventArgs) Handles btnEditOrganizationImage.Click
dlgImage.Filter = ""
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
Dim sep As String = String.Empty
For Each c As ImageCodecInfo In codecs
Dim codecName As String = c.CodecName.Substring(8).Replace("Codec", "Files").Trim()
dlgImage.Filter = String.Format("{0}{1}{2} ({3})|{3}", dlgImage.Filter, sep, codecName, c.FilenameExtension)
sep = "|"
dlgImage.FilterIndex = 5

If dlgImage.ShowDialog(Me) = DialogResult.OK Then
'Get the image name
Dim img = dlgImage.FileName
picEditOrganizationImage.Image = System.Drawing.Bitmap.FromFile(img)
End If
End Sub

The frmEditOrganizationCKJ just dispose together with the dispose of OpenFileDialog.

Answer Source

Probably you have copy/pasted your btnEditOrganizationImage from a button that has the DialogResult property set to something different than DialogResult.None.

This triggers the closing action for your modal form and the fix is really simple.
Set the property DialogResult for the btnEditOrganizationImage to DialogResult.None

From MSDN on Button.DialogResult

If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download