建立两个窗体第一个窗体放两个按钮加入如下代码Private Sub Command1_Click() Dim f As New Form2 f.Show End SubPrivate Sub Command2_Click() Dim f As Form For Each f In Forms If f.Caption = "Form2" Then Unload f Next End Sub
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const WM_CLOSE = &H10 Private Sub Command1_Click() Dim hwnd As Long hwnd = FindWindow(vbNullString, "Form2") '此处的form2可以换为你想要关闭的窗口标题 If hwnd <> 0 Then ‘说明找到了该窗口 PostMessage hwnd, WM_CLOSE, 0, 0 ’使该窗口关闭 End If End SubPrivate Sub Command2_Click() Form2.Show End Sub
if (not frmTest is nothing) or frmTest.Visible then unload frmTest load frmTest end if
if (not frmTest is nothing) or frmTest.Visible then unload frmTest load frmTest end if 这语句值得推荐!
Dim f As New Form2
f.Show
End SubPrivate Sub Command2_Click()
Dim f As Form
For Each f In Forms
If f.Caption = "Form2" Then Unload f
Next
End Sub
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub Command1_Click()
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "Form2") '此处的form2可以换为你想要关闭的窗口标题
If hwnd <> 0 Then ‘说明找到了该窗口
PostMessage hwnd, WM_CLOSE, 0, 0 ’使该窗口关闭
End If
End SubPrivate Sub Command2_Click()
Form2.Show
End Sub
if (not frmTest is nothing) or frmTest.Visible then
unload frmTest
load frmTest
end if
unload frmTest
load frmTest
end if
这语句值得推荐!