![VB.NET源码-156个实用实例哦……](http://www.zhzx123.com/VB.NET_03.gif)
关于me
我的程序中有几个窗体需要互相切换,我用如下代码
frm1.show
me.close
出现的情况是窗体一出现就迅速关闭请问这是什么问题
__________________________________________________________________________
用 this.close
__________________________________________________________________________
使用 hide 或是 ?
忘了,前几天有人说过的!
__________________________________________________________________________
你把创建对象的窗体给关闭了,对象也被销毁了
个你段可用的代码,才调试过
Module1.vb
Imports System.Threading
Module Module1
Public Sub Main()
InsCtrlForms.CreateForm( Form1 )
End Sub
Private curIdx As Integer = -1
Public InsCtrlForms As CtrlForms = New CtrlForms()
Public Class CtrlForms
Private arrForms As ArrayList = New ArrayList()
Private thForm1 As Thread
Private thForm2 As Thread
Private isWorking As Boolean = False
Public Function GetForm(ByVal idx As Integer) As Form
If ((idx < 0) Or (idx > = Me.arrForms.Count)) Then
Return (Nothing)
End If
Return (Me.arrForms(idx))
End Function
Public Function CreateForm(ByVal frmName As String) As Form
Dim retVal As Form = Nothing
While (isWorking)
Application.DoEvents()
End While
isWorking = True
Select Case frmName
Case Form1
arrForms.Add(New Form1)
retVal = arrForms(arrForms.Count - 1)
curIdx = arrForms.Count - 1
thForm1 = New Thread(AddressOf thShowForm1)
thForm1.Start()
Thread.Sleep(10)
Case Form2
arrForms.Add(New Form2)
retVal = arrForms(arrForms.Count - 1)
curIdx = arrForms.Count - 1
thForm2 = New Thread(AddressOf thShowForm2)
thForm2.Start()
Thread.Sleep(10)
Case Else
End Select
isWorking = False
Return (retVal)
End Function
Public Function DeleteForm(ByVal frmSrc As Form, ByVal frmName As String) As Boolean
While (isWorking)
Application.DoEvents()
End While
Dim idx As Integer = Me.SeekFormIdx(frmSrc)
isWorking = True
Dim retVal As Boolean = False
If (idx > -1) Then
Dim tmpFrm As Form = arrForms(idx)
tmpFrm.Close()
tmpFrm.Dispose()
arrForms.RemoveAt(idx)
arrForms.TrimToSize()
Select Case frmName
Case Form1
isWorking = False
thForm1.Abort()
Case Form2
isWorking = False
thForm2.Abort()
Case Else
End Select
End If
isWorking = False
Return (retVal)
End Function
Private Function SeekFormIdx(ByVal frmSrc As Form) As Integer
While (isWorking)
Application.DoEvents()
End While
isWorking = True
Dim retVal As Integer = -1
Dim nLoop As Integer = 0
While (nLoop < arrForms.Count)
If (arrForms(nLoop).Equals(frmSrc)) Then
Exit While
End If
nLoop = nLoop + 1
End While
If (nLoop < arrForms.Count) Then
retVal = nLoop
End If
isWorking = False
Return (retVal)
End Function
End Class
Private Sub thShowForm1()
Dim tmp As Form = InsCtrlForms.GetForm(curIdx)
If (tmp IsNot Nothing) Then
tmp.ShowDialog()
End If
End Sub
Private Sub thShowForm2()
Dim tmp As Form = InsCtrlForms.GetForm(curIdx)
If (tmp IsNot Nothing) Then
tmp.ShowDialog()
End If
End Sub
End Module
Form1.vb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
InsCtrlForms.CreateForm( Form2 )
InsCtrlForms.DeleteForm(Me, Form1 )
End Sub
End Class
‘ Form2.vb
Public Class Form2
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
InsCtrlForms.CreateForm( Form1 )
InsCtrlForms.DeleteForm(Me, Form2 )
End Sub
End Class
__________________________________________________________________________
Form1 上添加个 Button, Form2 上添加个 PictureBox
__________________________________________________________________________
忘记说了,启动项为 sub Main
__________________________________________________________________________
忘记说了,启动项为 sub Main
__________________________________________________________________________
感谢楼上的帮助,不过我有一点看不懂的样子
__________________________________________________________________________
你先按我给你的代码建个 Project,然后用 F10 跟一遍就知道了,我中午写的匆忙,可能还有些问题,你也看看吧,我晚上回家有时间再看
__________________________________________________________________________
汗。。。是 F11 跟一遍。。。
__________________________________________________________________________
好的
__________________________________________________________________________
我还是想请你给个简单的解决方法
__________________________________________________________________________
me.hide()
__________________________________________________________________________
原因是:
启动窗口不能关闭,如果关闭,应用程序随之关闭。
所以最开始的引导窗口要保留,在其它非启动窗口间可以切换。
__________________________________________________________________________
这样对于大一点的程序不就不能起到一个释放空间的作用了吗?
__________________________________________________________________________
我已经会了,请管理员结贴。
在工程属性中,选择“最后一个窗体关闭时结束程序”而不是“启动窗体关闭时结束程序”就可以了。
__________________________________________________________________________
是自己结贴!
__________________________________________________________________________