读书人

关于me,该怎么解决

发布时间: 2012-03-13 11:21:12 作者: rapoo

关于me
我的程序中有几个窗体需要互相切换,我用如下代码
frm1.show
me.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
[解决办法]
忘记说了,启动项为 sub Main
[解决办法]
原因是:
启动窗口不能关闭,如果关闭,应用程序随之关闭。
所以最开始的引导窗口要保留,在其它非启动窗口间可以切换。

读书人网 >VB Dotnet

热点推荐