question about module variables and window close events

B

Bert Szoghy

Hello,

I am missing something about Visual Basic .NET module variables and window
close events.

In the following code, after opening Form2 by clicking a button on Form1 and
then closing Form2, I would expect to click on the second Form1 button and
get intMyValue = 0.

Form2 has code to reinitialize it in its close event, but this doesn't seem
to happen.

It looks like Form2 is using a new instance of the variable intMyValue
rather than the first instance.

How do I fix this?

Thanks,
Bert

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 48)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 48)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Open pop up window"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(80, 128)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(104, 48)
Me.Button2.TabIndex = 1
Me.Button2.Text = "current value of intMyValue"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(240, 237)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
If intMyValue = 0 Then
frm.ShowDialog()
frm.Dispose()
intMyValue = 1
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
MessageBox.Show(CStr(intMyValue))

End Sub
End Class
Module Module1
Public intMyValue As Integer = 0
End Module
Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "Form2"
Me.Text = "Form2"

End Sub

#End Region


Private Sub Form2_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
intMyValue = 0
End Sub
End Class
 
L

Larry Lard

Bert said:
Hello,

I am missing something about Visual Basic .NET module variables and window
close events.

In the following code, after opening Form2 by clicking a button on Form1 and
then closing Form2, I would expect to click on the second Form1 button and
get intMyValue = 0.

You are setting intMyValue to 1 after Form2 is closed. Remember,
Form.ShowDialog blocks until the form it shows is closed. This is what
is happening:

(Start)
intMyValue <- 0
(Click button)
show form2 (modally)
close form 2; intMyValue <- 0
intMyValue <- 1

Now clicking the button again will do nothing, as instructed by the If
statement.
 
B

Bert Szoghy

Thanks Larry,

I needed somebody else to look over my shoulder.

Best regards!
Bert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top