[RESOLVED] how to keep a modal form displayed under a messagebox?
Hi all.
I have a parent form, which can lanch a dialog box. A wrong selection in the box launches a message box with an error message. When the message box pops up, the dialog box disappears, so the messagebox is sitting on top of the parent form.
Once the user clicks "OK" in the messagebox, the box disappears, and only the parent form remains.
How do I make it so that the dialog box remains when the messagebox is displayed? And also, I want the dialog box to remain when the user closes the message box. The user should have the option to make the right selection in the dialogbox once the messagebox disappears.
I tried to hunt for a property for the dialog box to achieve this, but couldn't find any.
Here is the code in the parent form to lauch the dialog box, and to also launch the messagebox, when the user has made a selection in the dialogbox:
Code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim selectedFile As DialogResult = frmOpenFileDialog.ShowDialog
If selectedFile = DialogResult.OK Then
Dim extension As String
' get index of "." in the filename
Dim index1 = frmOpenFileDialog.lstDisplayFiles.SelectedItem.indexof(".", 0)
' get the extension of the file
extension = frmOpenFileDialog.lstDisplayFiles.SelectedItem.substring(index1 + 1)
' open file
If extension = "txt" Or extension = "html" Then
rchtxtDisplay.LoadFile(frmOpenFileDialog.longPath & frmOpenFileDialog.lstDisplayFiles.SelectedItem, _
RichTextBoxStreamType.PlainText)
Else
MessageBox.Show("Can only open files ending in .txt or .html", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End Sub
Am I having this problem because all the code to launch the dialog and message boxes reside in the parent form? If I put the code to launch the messagebox in the dialog box, would that make the dialog box persistent when the messagebox is launched? I am putting all the code in the parent form because it can process the DialogResult.
Re: how to keep a modal form displayed under a messagebox?
You are looking at this the wrong way. It's not that the dialogue disappears when the MessageBox is displayed. The dialogue has alread disappeared well before that. Look at your code. The first line calls ShowDialog. ShowDialog doesn't return until the dialogue has been dismissed. Your dialogue has already disappeared before your first if statement.
If you don't the dialogue to disappear then you can't let ShowDialog return, which means that you would need to do your validation in the dialogue, not back in the main form. The other alternative is to display the dialogue within a Do loop, so it will keep getting redisplayed over and over until you execute an Exit Do statement, which you would put in your second If block. That way, if the MessageBox gets displayed, the dialogue will get redisplayed immediately after.
Re: how to keep a modal form displayed under a messagebox?
jmcilhinney, thanks for your reply.
I am trying to implement validation in the dialogue box with the below code.
Code:
Public Class frmOpenFileDialog
Public longPath As String
Public extension As String
Dim lstboxpopulated As Boolean
Private Sub frmOpenFileDialog_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim path As String
'Diplay path in combobox
path = My.Computer.FileSystem.SpecialDirectories.MyDocuments
cboDisplayDirectory.Text = path
' Clear listbox if it is already populated from previous call
If lstboxpopulated = True Then
lstDisplayFiles.Items.Clear()
End If
' Populate listbox
If My.Computer.FileSystem.DirectoryExists(path) Then
' Populate with directories
For Each directory As String In My.Computer.FileSystem.GetDirectories(path)
Dim index2 As Integer = directory.LastIndexOf("\")
directory = directory.Remove(0, index2)
lstDisplayFiles.Items.Add(directory)
Next
' Populate with files
For Each file As String In My.Computer.FileSystem.GetFiles(path)
Dim index1 As Integer = file.LastIndexOf("\")
longPath = file.Substring(0, index1 + 1)
file = file.Remove(0, index1 + 1)
lstDisplayFiles.Items.Add(file)
Next
End If
lstboxpopulated = True
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim extension As String
' get index of "." in the filename
Dim index1 As Integer = lstDisplayFiles.SelectedItem.indexof(".", 0)
' get the extension of the file
extension = lstDisplayFiles.SelectedItem.substring(index1 + 1)
' validate
If extension <> "txt" And extension <> "html" Then
MessageBox.Show("Can only open files ending in .txt or .html", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.ShowDialog()
End If
End Sub
End Class
But I just can't get the dialog box to reload once the user clicks "Ok" in the messagebox. I've tried reading up on visibility, but I can't find the property for it on the properties toolbox. And do I set visibility to be false in the frmOpenFileDialog_Load event, or in the button_click event?
Re: how to keep a modal form displayed under a messagebox?
Originally Posted by jmcilhinney
The other alternative is to display the dialogue within a Do loop, so it will keep getting redisplayed over and over until you execute an Exit Do statement, which you would put in your second If block.