Home > Software design >  Underlines of MaskedTextBox disappear
Underlines of MaskedTextBox disappear

Time:09-23

i have a problem in Windows Forms. I've created a form where I can enter the first and last name and click on the Search-Button to show another form with the following code:

Private Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click

    If Me._clsfrmChild Is Nothing Then
        Me._clsfrmChild = New clsFrmChild
    End If

    If Me._clsfrmChild.ShowDialog = False Then
        Me._clsfrmChild.ShowDialog(Me)
    End If

In the second form I have a MaskedTextbox:

Empty MaskedTextBox

Whatever I do, If I close the second form with Visible = False and reopen it again, the MaskedTextBox looks like this: MaskedTextBox without underlines

I close the second form that way:

Private Sub btnAbort_Click(sender As Object, e As EventArgs) Handles btnAbort.Click
    Me.Visible = False
End Sub

Does someone of you know why this problem is caused?

CodePudding user response:

This is really not the way you supposed to work with dialogs. The proper way is this

dim result as DialogResult
using f as Form = New MyForm()

    result = f.ShowDialog()
    ' here you can work with form's public properties etc
end using

' optionally here you can continue massaging the result
if result = DialogResult.Ok then
   ' do for ok result
else
   ' do for other result. You can have severul results - add elseif
end if

CodePudding user response:

Here is how to make a dialog with results in general. This will be similar to how MessageBox.Show() works.

Public Class clsFrmChild

    Private Sub New()
        InitializeComponent()
    End Sub

    Public Shared Shadows Function Show() As DialogResult
        Dim result As DialogResult
        Using f As New clsFrmChild()
            result = f.ShowDialog()
        End Using
        Return result
    End Function

    Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
        DialogResult = DialogResult.OK
        Close()
    End Sub

    Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click
        DialogResult = DialogResult.Cancel
        Close()
    End Sub

End Class

Note the constructor is privatized so there is no more Me._clsfrmChild = New clsFrmChild. You will see the displaying of the modal dialog is much simpler when called like MessageBox

Private Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click
    Dim result = clsFrmChild.Show() ' static method call instead of instance method
    Select Case result
        Case DialogResult.OK
            MessageBox.Show("Ok")
        Case DialogResult.Cancel
            MessageBox.Show("Cancel")
    End Select
End Sub

If you are not interested in returning a standard DialogResult, you could change the return type to whatever you like such as a custom class, with more information (such as you want to return a string in addition to DialogResult) i.e.

Public Class clsFrmChildResult
    Public Property Text As String
    Public Property DialogResult As DialogResult
End Class

...

Public Shared Shadows Function Show() As clsFrmChildResult
    Dim result As clsFrmChildResult
    Using f As New clsFrmChild()
        Dim dr = f.ShowDialog()
        result = New clsFrmChildResult With {.Text = TextBox1.Text, .DialogResult = dr}
    End Using
    Return result
End Function

...

Private Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click
    Dim result = clsFrmChild.Show()
    Select Case result.DialogResult
        Case DialogResult.OK
            MessageBox.Show("Ok")
            Dim myString = result.Text
        Case DialogResult.Cancel
            MessageBox.Show("Cancel")
    End Select
End Sub
  • Related