VB编程中同一个按钮 点一下运行程序 再点一下关闭该程序 如何编写其代码

2024-12-17 10:17:51
推荐回答(2个)
回答1:

这个应该是可以有很多种方法来做到的,像上面的兄弟说的通过caption来判断也可以,不过看上去你这个按钮caption是不会变的,比如你可以定义一个全局变量来做嘛。或者嫌变量多用按钮的Tag属性来记录点东西来区别是打开还是关闭动作也是可以的,希望能帮到你……
private m_Action as long '这个变量来标示是什么打开或者关闭动作
Private Sub Command1_Click()
if m_Action=0 then
'm_Action=0 标示打开
'-----你的打开代码
else
'm_Action=1 标示关闭
'-----你的关闭代码
end if
end Sub

回答2:

以记事本为例:

Private Sub Command1_Click()
    Static task As Object
    If task Is Nothing Then
        Dim WshShell
        Set WshShell = CreateObject("Wscript.Shell")
        Set task = WshShell.Exec("notepad")
    Else
        task.Terminate
        Set task = Nothing
    End If
End Sub