Disposing of form

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
I've been reading recently about passing vars between forms and all is going well with me designing my own msgbox as a trial.
Problem i have is the timing of the msgbox form disposal.

I'm using the following to hold the msgbox value...
VB.NET:
    Public Property MsgResult() As Object
        Get
            Return RVal
        End Get
        Set(ByVal MyVar As Object)
            RVal = MyVar
        End Set
    End Property

...which is passed by the msgbox button click:
VB.NET:
    Sub btnAClick()
        Me.MsgResult = 1
        Me.Close()
    End Sub

Note the 'Me.Close' as if i 'Dispose' then the value isn't returned.

So from my main form i can call like (overloaded, of course)...
VB.NET:
    frmMsg.ShowDialog("Confirm - Clear note?", 2, 2, "Yes, Do It", "No!!")

...then use the return value to know which button was pressed (could be 1 - 3 buttons displayed)

However, if i call with one button, then sometime later call with two buttons then the first button is still there as the form wasn't disposed. Surely i don't have to dispose every time after i've retrieved the return value? Please help me understand further.
 
When creating any short-lived disposable object, including forms used as modal dialogues, you use a Using block. The lifetime of the variable and the object are scoped to the block, i.e. the variable is declared and the object created at the start of the block and the variable falls out of scope and the object is disposed at the end of the block, e.g.
Using dialogue As New DialogueForm
    'Pass input
    dialogue.Input = input

    'Display dialogue
    If dialogue.ShowDialog() = DialogResult.OK Then
        'Retrieve output
        output = dialogue.Output

    End If
End Using
 
Sorry, not quite sinking in, although i think i understand the concept.
When i try this in my code i get 'DialogueForm is not defined'
 
I think you've missed the point about examples, i.e. they are examples. That code was just to show the structure of a Using block. As I said in my post, you use a Using block to create and destroy objects of every disposable type. Use it for a type that is defined, not the example type I chose to use in my example code. You might use it for a StreamReader, FileStream or any form that you have defined in your project.
 
Apologies, i thought 'DialogForm' was a specific type that handled the disposing - i'm thinking too much into it now! I understand that the Using block handles the declaration and scope of the variable. Thanks for your help.
 
To conclude (for any others reading this)

In my case it would be:
VB.NET:
    Using F As New frmMsg
        F.ShowDialog("Confirm - Clear note?", 2, 2, "Yes, Do It", "No!!")
        If F.MsgResult = 1 Then Me.txtNote.Text = vbNullString
    End Using
(i dont use 'if dialogresult=ok' because i've ensured my custom dialog always returns a value)

and the disposal is handled in the dialog form:
VB.NET:
    Sub btnAClick()
        Me.MsgResult = 1
        Me.Dispose()
    End Sub

Thanks again for your help. I now understand.
 
and the disposal is handled in the dialog form:
VB.NET:
    Sub btnAClick()
        Me.MsgResult = 1
        Me.Dispose()
    End Sub

Thanks again for your help. I now understand.

Nope. The whole point of the Using block is to create the object at the Using statement and dispose it at the End Using statement. The form should just close itself, not dispose. When you call Close, the form handles internally whether to just hide (if it was displayed by ShowDialog) or to dispose (if it was displayed by Show).
 
Nope. The whole point of the Using block is to create the object at the Using statement and dispose it at the End Using statement. The form should just close itself, not dispose. When you call Close, the form handles internally whether to just hide (if it was displayed by ShowDialog) or to dispose (if it was displayed by Show).

(Sorry to continue, just trying to learn)
I understand what you are saying, and from your earlier posts in this thread that was what i'd expected. However my dialog form doesn't close unless i explicitly tell it to. Must still be missing something...

Also remember to set DialogResult, read this article: Form.DialogResult Property (System.Windows.Forms)
Will read that John, thanks for the input.
 
So i think i may be there now:

VB.NET:
    Using F As New frmMsg
        F.ShowDialog("Confirm - Clear note?", 2, 2, "Yes, Do It", "No!")
        If F.DialogResult = DialogResult.OK Then
            If F.MsgResult = 1 Then Me.txtNote.Text = vbNullString
        End If
    End Using

VB.NET:
    Sub btnAClick()
        Me.MsgResult = 1
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

I still need MsgResult as the dialog buttons could be used for different things (guess i could use the returned result though)
 
Me.DialogResult = Windows.Forms.DialogResult.OK
You don't need to write that code, you can select the button in designer and select a value for its DialogResult property.

You also don't need the additional code line to check DialogResult value, the result is reflected and returned through the ShowDialog function. This goes for both when setting the property explicitly and when a button is clicked that has a specific DialogResult value.
If F.ShowDialog(...) = DialogResult.OK Then
 
You don't need to write that code, you can select the button in designer and select a value for its DialogResult property.

I am adding the buttons dynamically so have moved said lines into the constructor.

You also don't need the additional code line to check DialogResult value, the result is reflected and returned through the ShowDialog function. This goes for both when setting the property explicitly and when a button is clicked that has a specific DialogResult value.
If F.ShowDialog(...) = DialogResult.OK Then

Of course you are right, this was legacy from earlier attempts to get this working.

Thanks to both for your patience in helping me understand this.
 
Back
Top