关于form问题
现在问题是这样的:我需要两个一摸一样的form,于是我创建了form1,在一个module中新建了两个全局变量One和Two,在程序的主Form的load事件中初始化了One和Two,One=new form(),Two=New from()。在TreeView中我设置了两个节点,带有checkbox,checkbox1勾选之后显示窗体One,checkbox2勾选之后显示Two,窗体显示通过设置窗体的visible属性。在我关掉One之后问题随之而来,再勾选checkbox1之后,抛出异常,说form已经被释放,无法操作。这是怎么回事呢? --------------------编程问答----------------------------------------编程问答--------------------
Private Sub Command1_Click()
one.Visible = True
one.Caption = "one"
End Sub
Private Sub Command2_Click()
two.Visible = True
two.Caption = "two"
End Sub
Private Sub Form_Load()
Set one = New Form2
Set two = New Form2
End Sub
----Module1---
Public one As Form, two As Form
你这个不行哦。点击按钮1和2之后,One和Two显示出来,你关掉One,你再点按钮1.就会抛出异常。
我的代码跟你一样的。我的VB.net.
我贴给你看:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Module1.Initial()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Module1.One.Text = "One"
Module1.One.Visible = True
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Module1.Two.Text = "Two"
Module1.Two.Visible = True
End Sub
End Class
Module Module1
Public One As Form2
Public Two As Form2
Public Sub Initial()
One = New Form2()
Two = New Form2()
End Sub
End Module
--------------------编程问答-------------------- 在点击按钮1和2之后,One和Two都能显示出来。但是关掉One之后,再点击按钮1,就抛出异常,说Form2被释放了。
Module1.One.Text = "One"
Module1.One.Visible = True
这两行代码抛出异常是在第二行。如果说One被释放了,那么Text也不能设置了才对,到底为什么呢?
--------------------编程问答-------------------- vb.net你最好发在vb.net版(我是以vb6的方式回的)
vb.net你如下处理就可以了:
--------------------编程问答-------------------- 支持猴哥!!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If One Is Nothing OrElse One.IsDisposed Then
One = New Form2
One.Text = "One"
One.Show()
Else
One.Activate()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Two Is Nothing OrElse One.IsDisposed Then
Two = New Form2
Two.Text = "Two"
Two.Show()
Else
Two.Activate()
End If
End Sub
End Class
----
Module Module1
Public One, Two As Form
End Module
补充:VB , 基础类