Question Why do control have different behavior in modal forms vs non modal?

groadsvb

Well-known member
Joined
Nov 13, 2006
Messages
75
Programming Experience
Beginner
I have a form and in one set of code it is call as modal and in another set of code it is called non modal. There is a control on the form that presents a dropdown box. We provide a datasource to the dropdown which is a list. When we get the all the data elements from the database if there is a value that can be applied to the dropdown before any user input is performed we assigned it to the selectedvalue property and it should be displayed in the text area of the control.

On the non modal form the the correct value is shown in the text area of the dropdown control. When in modal it does not. I have walked through the code several times to make sure we hit the same code and we do and the selectedvalue is assigned to the control. Why does the control not display correctly when in modal form.

What is happening that causes the different behavior?

modal
VB.NET:
         Protected Overrides Function MyFunction(ByVal args As StatusChangeArgs, ByRef isNewStatusAvailable As Boolean) As Boolean
            Dim aod As frmAOD = New frmAOD(True)
            aod.Text = "title text"
            isNewStatusAvailable = True
             aod.LoadUnit(args.UnitName)
            Return aod.ShowDialog() = System.Windows.Forms.DialogResult.OK
        End Function

non modal
VB.NET:
        Public Sub Mysub(ByVal arg As IUserCommand)
         dim _aod as frmAOD = New frmAPD(True)
              _aod.TopMost = True
            _aod.WindowState = _aod.LastNonMinimizedWindowState
            _aod.Text = "title text"
            _aod.Show()
            _aod.LoadUnit(arg)
            _aod.Activate()
        End Sub
 
Is it that LoadUnit method that is supposed to be performing the action in question? The obvious difference there is that you're calling it before ShowDialog but after Show. Whether or not that's the casue of the issue is anyone's guess, given that we don't know what that method or what happens in the Load event handler.

By the way, closing a modal dialogue doesn't dispose it so it is your responsibility to dispose and form that you display with ShowDialog. To that end, you should create it with a Using statement, so that it will be disposed implicitly at the End Using statement:
Protected Overrides Function MyFunction(ByVal args As StatusChangeArgs, ByRef isNewStatusAvailable As Boolean) As Boolean
    Using aod As New frmAOD(True)
        aod.Text = "title text"
        isNewStatusAvailable = True
        aod.LoadUnit(args.UnitName)
        Return aod.ShowDialog() = System.Windows.Forms.DialogResult.OK
    End Using
End Function
Note that the object created by a Using block is ALWAYS disposed, even if an exception is thrown or a Return statement is used inside the block.
 
Back
Top