none
my dialog (frmdialog.show(me) ) disappears when the SUBMIT button returns an error - how keep it modal? RRS feed

  • Question

  • I have the following code in a main-form:

     Function askForFolderName(ByVal strDescription As String, ByVal fname As String) As String
            Dim askdialog As New frmAskDialog
    
            askdialog.ShowDialog(Me)
            Return GlobalVariables.NewFolderToMake
        End Function

    In the 'askdialog', the user is asked for information.  He provides it in a textbox, and then clicks a SUBMIT button.  If he provides the wrong syntax, the submit routine gives him an error message, and then exits.   So at this point, he should still be staring at the dialog box, and should fix his error.   But when I try this, and the submit button exits, the entire dialog disappears!   Instead of looking at the dialog, he is now looking at the parent form that called it!

    How do I prevent that?  The submit button code is below:

        Private Sub ButtonSubmit_Click(sender As Object, e As EventArgs) Handles ButtonSubmit.Click
            GlobalVariables.NewFolderToMake = ""
            Me.DialogResult = DialogResult.OK
            Dim str As String
            str = ComboBoxNewFolder.Text.Trim
            If str.Length = 0 Then
                MessageBox.Show("Error: you forgot to enter the folder name")
                Exit Sub
            End If
            If str.Length < 3 Then
                MessageBox.Show("Error: folder name must have at least 3 characters")
                Exit Sub
            End If
            Dim sb As New StringBuilder(str)
            If Not Char.IsLetter(sb(0)) Then
                MessageBox.Show("Error: folder name must start with a letter")
                Exit Sub
            End If
            Dim i As Integer
            For i = 0 To sb.Length - 1
                If Char.IsLetterOrDigit(sb(i)) Then
                    Continue For
                End If
                If sb(i) = " " Or sb(i) = "-" Or sb(i) = "_" Then
                    Continue For
                End If
                MessageBox.Show("Error at position " & i & " in your folder name.   You can only have letters, digits, blanks, dashes or underscores.   You have: " & sb(i))
                Exit Sub
            Next
            GlobalVariables.NewFolderToMake = sb.ToString
            Me.Hide()
        End Sub

    Tuesday, May 1, 2018 9:45 AM

Answers

  • Move "Me.DialogResult" code after you have done all the required checks. Try following code

     Private Sub ButtonSubmit_Click(sender As Object, e As EventArgs) Handles ButtonSubmit.Click
            GlobalVariables.NewFolderToMake = ""
            
            Dim str As String
            str = ComboBoxNewFolder.Text.Trim
            If str.Length = 0 Then
                MessageBox.Show("Error: you forgot to enter the folder name")
                Exit Sub
            End If
            If str.Length < 3 Then
                MessageBox.Show("Error: folder name must have at least 3 characters")
                Exit Sub
            End If
            Dim sb As New StringBuilder(str)
            If Not Char.IsLetter(sb(0)) Then
                MessageBox.Show("Error: folder name must start with a letter")
                Exit Sub
            End If
            Dim i As Integer
            For i = 0 To sb.Length - 1
                If Char.IsLetterOrDigit(sb(i)) Then
                    Continue For
                End If
                If sb(i) = " " Or sb(i) = "-" Or sb(i) = "_" Then
                    Continue For
                End If
                MessageBox.Show("Error at position " & i & " in your folder name.   You can only have letters, digits, blanks, dashes or underscores.   You have: " & sb(i))
                Exit Sub
            Next
            GlobalVariables.NewFolderToMake = sb.ToString
            Me.DialogResult = DialogResult.OK
            Me.Hide()
        End Sub


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    • Marked as answer by Gidmaestro Tuesday, May 1, 2018 9:59 AM
    Tuesday, May 1, 2018 9:51 AM