Sender said:
Yesterday there was a very long thread on this query. (You can search on
this by post by 'sender' with subject 'Simple Problem' post date Oct 7 time
1:43p)
And in the end the following code was decided to open a new form:
---Code in the click event of a button: button1
if frm is nothing then
frm = new form2()
frm.show
endif
---Code outside any procedure
dim frm as form2
Now when first time we click on this button (button1) it shows form2. Now we
close this form2. Now when we again click button1 ........NOTHING
HAPPENS.........it doesn't open form2 again..................WHY ?
Basically, as Armin said - the variable holding the form2 instance never gets
set to nothing. So, the second time the code runs - it is not created. Here is
one possible solution - hook the child forms closed event. That way, you know
it has shutdown and set the instance to nothing... Here's the code:
' Form1 Code....
Option Explicit On
Option Strict On
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
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(108, 40)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 105)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private frm As Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If frm Is Nothing Then
frm = New Form2
AddHandler frm.Closed, AddressOf Me.ChildClosed
frm.Show()
End If
End Sub
Private Sub ChildClosed(ByVal sender As Object, ByVal e As System.EventArgs)
frm = Nothing
End Sub
End Class
' Form2 Code
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.
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 36.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
Me.Label1.ForeColor = System.Drawing.Color.Blue
Me.Label1.Location = New System.Drawing.Point(0, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(292, 97)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Form2"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 97)
Me.Controls.Add(Me.Label1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub
#End Region
End Class
HTH,
Tom Shelton