vb.net 从标题栏 X 按钮关闭 winforms 应用程序
vb.net close winforms application from title bar X button
在 winforms 应用程序上使用 VB.Net 我有两个表单
两种形式都设置为"When Last Form Closes"
两种形式都将 "Key Preview" 设置为 TRUE
frmOne 启动窗体有一个 "Form Border Style" 设置为 FixedToolWindow
以下是使用两种形式执行的事件顺序
加载 frmOne 并单击标题栏窗体上的 X 按钮关闭,开始按钮显示在 IDE
这是所需的行为
第二个事件序列加载应用程序 frmOne 有一个按钮可以导航到 frmTwo,而 frmTwo 有一个按钮可以导航回 frmOne 当我们回到 frmOne 时,我们单击标题栏上的 X 按钮,frmOne 关闭但应用程序仍然 运行 IDE
中没有开始按钮
问题是导航到其他表单后如何关闭应用程序?
下面的代码是我尝试解决这个问题的各种方法
我包含的按键代码没有显示出预期的结果
Private Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
'frmOne Property KeyPreview needs to be set to True
If Asc(e.KeyChar) = 27 Then
'MessageBox.Show("You Pressed " & e.KeyChar)
Me.Close()
End If
End Sub
'Public Sub frmOne_QueryClose(Cancel As Integer, CloseMode As Integer)
'Prevent user from closing with the Close box in the title bar.
'If CloseMode = 1 Then Me.Close()
'If CloseMode = 1 Then Application.Exit()
'End Sub
'Private Sub frmOne_FormClosing(sender As Object, e As FormClosingEventArgs) 'Handles Me.FormClosing
'Application.Exit()
'Me.Close()
'End Sub
Private Sub frmOne_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
'Application.Exit()
End Sub
代码到 frmTwo
Private Sub btnToFormTwo_Click(sender As Object, e As EventArgs) Handles btnToFormTwo.Click
Dim i As Integer
i = txtBoxOne.Text.Length
If i = 0 Then
MsgBox("Enter Data")
txtBoxOne.Select()
Return
End If
Dim OBJ As New frmTwo
OBJ.SPass = txtBoxOne.Text
OBJ.Show()
lblFormOne.Text = " "
txtBoxOne.Clear()
Me.Hide()
'Me.Close()'R Click project PassVar Set Start Up Form
'Best Solution is to have Splash Form as Start Up Form
End Sub
返回 frmOne 的代码
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
frmOne.txtBoxOne.Text = "Back from 2"
Me.Hide()
frmOne.Show()
End Sub
您已将 vb 项目设置为退出 "when last form closes" - 从您的启动窗体打开第二个窗体意味着当您关闭启动窗体时,还有另一个窗体处于打开状态,因此应用程序确实不放弃。将设置更改为 "when startup form closes" 使应用按预期运行
您需要关闭表单才能结束应用程序。我在一个项目中使用 "When last form closes" 可能有十几种形式,但效果很好。
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
frmOne.txtBoxOne.Text = "Back from 2"
'Me.Hide() 'frmTwo is still running, To close a form use .Close()
Close()
frmOne.Show()
End Sub
在 winforms 应用程序上使用 VB.Net 我有两个表单
两种形式都设置为"When Last Form Closes"
两种形式都将 "Key Preview" 设置为 TRUE
frmOne 启动窗体有一个 "Form Border Style" 设置为 FixedToolWindow
以下是使用两种形式执行的事件顺序
加载 frmOne 并单击标题栏窗体上的 X 按钮关闭,开始按钮显示在 IDE
这是所需的行为
第二个事件序列加载应用程序 frmOne 有一个按钮可以导航到 frmTwo,而 frmTwo 有一个按钮可以导航回 frmOne 当我们回到 frmOne 时,我们单击标题栏上的 X 按钮,frmOne 关闭但应用程序仍然 运行 IDE
问题是导航到其他表单后如何关闭应用程序?
下面的代码是我尝试解决这个问题的各种方法
我包含的按键代码没有显示出预期的结果
Private Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
'frmOne Property KeyPreview needs to be set to True
If Asc(e.KeyChar) = 27 Then
'MessageBox.Show("You Pressed " & e.KeyChar)
Me.Close()
End If
End Sub
'Public Sub frmOne_QueryClose(Cancel As Integer, CloseMode As Integer)
'Prevent user from closing with the Close box in the title bar.
'If CloseMode = 1 Then Me.Close()
'If CloseMode = 1 Then Application.Exit()
'End Sub
'Private Sub frmOne_FormClosing(sender As Object, e As FormClosingEventArgs) 'Handles Me.FormClosing
'Application.Exit()
'Me.Close()
'End Sub
Private Sub frmOne_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
'Application.Exit()
End Sub
代码到 frmTwo
Private Sub btnToFormTwo_Click(sender As Object, e As EventArgs) Handles btnToFormTwo.Click
Dim i As Integer
i = txtBoxOne.Text.Length
If i = 0 Then
MsgBox("Enter Data")
txtBoxOne.Select()
Return
End If
Dim OBJ As New frmTwo
OBJ.SPass = txtBoxOne.Text
OBJ.Show()
lblFormOne.Text = " "
txtBoxOne.Clear()
Me.Hide()
'Me.Close()'R Click project PassVar Set Start Up Form
'Best Solution is to have Splash Form as Start Up Form
End Sub
返回 frmOne 的代码
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
frmOne.txtBoxOne.Text = "Back from 2"
Me.Hide()
frmOne.Show()
End Sub
您已将 vb 项目设置为退出 "when last form closes" - 从您的启动窗体打开第二个窗体意味着当您关闭启动窗体时,还有另一个窗体处于打开状态,因此应用程序确实不放弃。将设置更改为 "when startup form closes" 使应用按预期运行
您需要关闭表单才能结束应用程序。我在一个项目中使用 "When last form closes" 可能有十几种形式,但效果很好。
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
frmOne.txtBoxOne.Text = "Back from 2"
'Me.Hide() 'frmTwo is still running, To close a form use .Close()
Close()
frmOne.Show()
End Sub