与 Enter 键关联的设置按钮
Setting button associated with Enter key
我有一个包含三个面板的表单,每个面板中都有不同的按钮。根据显示表单之前的一些设置,三个面板中的两个被隐藏(宽度设置为 0)。在相同的设置代码中,我也一直在尝试调整 "clicked" 按钮,当用户按下回车键时,它是面板中未隐藏的按钮之一,但它不起作用。当我在测试时按 Enter 键时,总是使用相同的按钮。
作为参考,基于 Why is my basic default .acceptbutton is not working?,我已经拥有隐藏面板的代码,将 Focus() 和 AcceptButton 设置为我想要使用的按钮,但无论如何,当在表单显示使用了错误的按钮后,我按下了 Enter。这里的参考是设置代码:
''' <summary>
''' Displays a custom error prompt with the indicated button(s) displayed, and returns
''' the user's response
''' </summary>
''' <param name="txt">The text to display in the prompt</param>
''' <param name="btns">The button(s) to use</param>
''' <param name="MWin">The MainWin instance holding the ErrWin instance that
'''' all this information impacts</param>
''' <returns>A Boolean on whether to continue the application or exit it </returns>
''' <remarks></remarks>
Public Function ErrBox(ByVal txt As String, ByVal btns As String, _
ByVal MWin As MainWin) As Boolean
'
Dim ret As Boolean = True 'default to true as we continue the application in most
'cases
Dim pt As New Point
Try
'Setup the error window
MWin.EWin.FullLog.Add(Now() & " - " & txt) 'add to the full log for dump to
'admin notice email if needed
MWin.EWin.Prompt.Text = txt
Select LCase(btns)
Case "yn"
'Resize the yn panel correctly & adjust location
pt.X = 195
pt.Y = 415
MWin.EWin.YNPanel.Location = pt
MWin.EWin.YNPanel.Width = 191
MWin.EWin.YNPanel.Height = 30
'Make sure okpanel & exitpanel are hidden
pt.X = 0
pt.Y = 415
MWin.EWin.OkPanel.Location = pt
MWin.EWin.OkPanel.Width = 0
pt.X = 0
pt.Y = 385
MWin.EWin.ExPanel.Location = pt
MWin.EWin.ExPanel.Width = 0
'Put focus on Yes
MWin.EWin.YBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.YBtn
Case "ok"
'Resize the ok panel correctly & adjust location
pt.X = 240
pt.Y = 415
MWin.EWin.OkPanel.Location = pt
MWin.EWin.OkPanel.Width = 86
MWin.EWin.OkPanel.Height = 30
'Make sure ynpanel & exitpanel are hidden
pt.X = 0
pt.Y = 415
MWin.EWin.YNPanel.Location = pt
MWin.EWin.YNPanel.Width = 0
pt.X = 0
pt.Y = 385
MWin.EWin.ExPanel.Location = pt
MWin.EWin.ExPanel.Width = 0
'Give focus to ok
MWin.EWin.OkBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.OkBtn
Case "exit"
'Resize the exit panel correctly & adjust location
pt.X = 240
pt.Y = 415
MWin.EWin.ExPanel.Location = pt
MWin.EWin.ExPanel.Width = 191
MWin.EWin.ExPanel.Height = 30
'Make sure okpanel & exitpanel are hidden
pt.X = 0
pt.Y = 415
MWin.EWin.OkPanel.Location = pt
MWin.EWin.OkPanel.Width = 0
pt.X = 0
pt.Y = 385
MWin.EWin.YNPanel.Location = pt
MWin.EWin.YNPanel.Width = 0
'Give focus to Exit
MWin.EWin.Abutton = MWin.EWin.ExitBtn
MWin.EWin.ExitBtn.Focus()
Case Else
'Bad value, log the issue and then notify the user
MWin.EWin.FullLog.Add(Now() & " - Inproper value provided for btns." _
& " Limited to 'YN', 'Ok', or 'Exit'. " & btns & " was provided.")
MsgBox("An error occured while attempting to report an error. The " _
& "application will attempt to continue to function, but the " & _
"action immediately prior" & _
" to this prompt appearing will not be able to successfully " & _
"complete.", vbOKOnly, "Error in Error Handling")
'Set ret to True
ret = True
Return ret
Exit Function
End Select
Catch ex As Exception
MWin.EWin.FullLog.Add(Now() & " - Error while trying to setup the ErrWin." & _
" Details: " & ex.Message)
MsgBox("An error occured while attempting to report an error. The " & _
"application will attempt to continue to function, but the action " & _
"immediately prior" & _
" to this prompt appearing will not be able to successfully complete.", _
vbOKOnly, "Error in Error Handling")
'Set ret to True
ret = True
Return ret
Exit Function
End Try
Try
MWin.EWin.Btns = btns
'Show the error window
MWin.EWin.ShowDialog()
'Capture the return
ret = MWin.EWin.Ret
'Clear btns and ret on ewin
MWin.EWin.Btns = ""
MWin.EWin.Ret = Nothing
Catch ex As Exception
MWin.EWin.FullLog.Add(Now() & " - Error while showing ErrWin, reading its " _
& "response, or clearing its variables. Details: " & ex.Message)
MsgBox("An error occured while attempting to report an error. The application" -
& " will attempt to continue to function, but the action immediately " & _
"prior" & _
" to this prompt appearing will not be able to successfully complete.", _
vbOKOnly, "Error in Error Handling")
'Set ret to True
ret = True
End Try
Return ret
End Function
尽管有明确的 Focus 和 AcceptButton 行,但当按下 Enter 时,OkBtn 始终是 "clicked"。最后请注意,没有用于相关表单的 Load 或 Shown 事件的代码,因此没有任何内容与上述代码相矛盾。任何关于我所缺少的东西的指导将不胜感激
只需禁用其他按钮,即使它们被隐藏也无法接收回车键
Select LCase(btns)
Case "yn"
.....
MWin.EWin.ExitBtn.Enabled = False
MWin.EWin.YBtn.Enabled = True
MWin.EWin.OkBtn.Enabled = False
'Put focus on Yes
MWin.EWin.YBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.YBtn
Case "ok"
.....
MWin.EWin.ExitBtn.Enabled = False
MWin.EWin.YBtn.Enabled = False
MWin.EWin.OkBtn.Enabled = True
'Give focus to ok
MWin.EWin.OkBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.OkBtn
Case "exit"
.......
'Give focus to Exit
MWin.EWin.ExitBtn.Enabled = True
MWin.EWin.YBtn.Enabled = False
MWin.EWin.OkBtn.Enabled = False
MWin.EWin.Abutton = MWin.EWin.ExitBtn
MWin.EWin.ExitBtn.Focus()
Case Else
....
End Select
可能 enable/disable 直接使用按钮的容器(即分组框)可能更可取,只是为了确保隐藏容器中的所有内容都不会干扰您的逻辑(例如,当用户使用 TAB 键在控件之间移动)
我有一个包含三个面板的表单,每个面板中都有不同的按钮。根据显示表单之前的一些设置,三个面板中的两个被隐藏(宽度设置为 0)。在相同的设置代码中,我也一直在尝试调整 "clicked" 按钮,当用户按下回车键时,它是面板中未隐藏的按钮之一,但它不起作用。当我在测试时按 Enter 键时,总是使用相同的按钮。
作为参考,基于 Why is my basic default .acceptbutton is not working?,我已经拥有隐藏面板的代码,将 Focus() 和 AcceptButton 设置为我想要使用的按钮,但无论如何,当在表单显示使用了错误的按钮后,我按下了 Enter。这里的参考是设置代码:
''' <summary>
''' Displays a custom error prompt with the indicated button(s) displayed, and returns
''' the user's response
''' </summary>
''' <param name="txt">The text to display in the prompt</param>
''' <param name="btns">The button(s) to use</param>
''' <param name="MWin">The MainWin instance holding the ErrWin instance that
'''' all this information impacts</param>
''' <returns>A Boolean on whether to continue the application or exit it </returns>
''' <remarks></remarks>
Public Function ErrBox(ByVal txt As String, ByVal btns As String, _
ByVal MWin As MainWin) As Boolean
'
Dim ret As Boolean = True 'default to true as we continue the application in most
'cases
Dim pt As New Point
Try
'Setup the error window
MWin.EWin.FullLog.Add(Now() & " - " & txt) 'add to the full log for dump to
'admin notice email if needed
MWin.EWin.Prompt.Text = txt
Select LCase(btns)
Case "yn"
'Resize the yn panel correctly & adjust location
pt.X = 195
pt.Y = 415
MWin.EWin.YNPanel.Location = pt
MWin.EWin.YNPanel.Width = 191
MWin.EWin.YNPanel.Height = 30
'Make sure okpanel & exitpanel are hidden
pt.X = 0
pt.Y = 415
MWin.EWin.OkPanel.Location = pt
MWin.EWin.OkPanel.Width = 0
pt.X = 0
pt.Y = 385
MWin.EWin.ExPanel.Location = pt
MWin.EWin.ExPanel.Width = 0
'Put focus on Yes
MWin.EWin.YBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.YBtn
Case "ok"
'Resize the ok panel correctly & adjust location
pt.X = 240
pt.Y = 415
MWin.EWin.OkPanel.Location = pt
MWin.EWin.OkPanel.Width = 86
MWin.EWin.OkPanel.Height = 30
'Make sure ynpanel & exitpanel are hidden
pt.X = 0
pt.Y = 415
MWin.EWin.YNPanel.Location = pt
MWin.EWin.YNPanel.Width = 0
pt.X = 0
pt.Y = 385
MWin.EWin.ExPanel.Location = pt
MWin.EWin.ExPanel.Width = 0
'Give focus to ok
MWin.EWin.OkBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.OkBtn
Case "exit"
'Resize the exit panel correctly & adjust location
pt.X = 240
pt.Y = 415
MWin.EWin.ExPanel.Location = pt
MWin.EWin.ExPanel.Width = 191
MWin.EWin.ExPanel.Height = 30
'Make sure okpanel & exitpanel are hidden
pt.X = 0
pt.Y = 415
MWin.EWin.OkPanel.Location = pt
MWin.EWin.OkPanel.Width = 0
pt.X = 0
pt.Y = 385
MWin.EWin.YNPanel.Location = pt
MWin.EWin.YNPanel.Width = 0
'Give focus to Exit
MWin.EWin.Abutton = MWin.EWin.ExitBtn
MWin.EWin.ExitBtn.Focus()
Case Else
'Bad value, log the issue and then notify the user
MWin.EWin.FullLog.Add(Now() & " - Inproper value provided for btns." _
& " Limited to 'YN', 'Ok', or 'Exit'. " & btns & " was provided.")
MsgBox("An error occured while attempting to report an error. The " _
& "application will attempt to continue to function, but the " & _
"action immediately prior" & _
" to this prompt appearing will not be able to successfully " & _
"complete.", vbOKOnly, "Error in Error Handling")
'Set ret to True
ret = True
Return ret
Exit Function
End Select
Catch ex As Exception
MWin.EWin.FullLog.Add(Now() & " - Error while trying to setup the ErrWin." & _
" Details: " & ex.Message)
MsgBox("An error occured while attempting to report an error. The " & _
"application will attempt to continue to function, but the action " & _
"immediately prior" & _
" to this prompt appearing will not be able to successfully complete.", _
vbOKOnly, "Error in Error Handling")
'Set ret to True
ret = True
Return ret
Exit Function
End Try
Try
MWin.EWin.Btns = btns
'Show the error window
MWin.EWin.ShowDialog()
'Capture the return
ret = MWin.EWin.Ret
'Clear btns and ret on ewin
MWin.EWin.Btns = ""
MWin.EWin.Ret = Nothing
Catch ex As Exception
MWin.EWin.FullLog.Add(Now() & " - Error while showing ErrWin, reading its " _
& "response, or clearing its variables. Details: " & ex.Message)
MsgBox("An error occured while attempting to report an error. The application" -
& " will attempt to continue to function, but the action immediately " & _
"prior" & _
" to this prompt appearing will not be able to successfully complete.", _
vbOKOnly, "Error in Error Handling")
'Set ret to True
ret = True
End Try
Return ret
End Function
尽管有明确的 Focus 和 AcceptButton 行,但当按下 Enter 时,OkBtn 始终是 "clicked"。最后请注意,没有用于相关表单的 Load 或 Shown 事件的代码,因此没有任何内容与上述代码相矛盾。任何关于我所缺少的东西的指导将不胜感激
只需禁用其他按钮,即使它们被隐藏也无法接收回车键
Select LCase(btns)
Case "yn"
.....
MWin.EWin.ExitBtn.Enabled = False
MWin.EWin.YBtn.Enabled = True
MWin.EWin.OkBtn.Enabled = False
'Put focus on Yes
MWin.EWin.YBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.YBtn
Case "ok"
.....
MWin.EWin.ExitBtn.Enabled = False
MWin.EWin.YBtn.Enabled = False
MWin.EWin.OkBtn.Enabled = True
'Give focus to ok
MWin.EWin.OkBtn.Focus()
MWin.EWin.Abutton = MWin.EWin.OkBtn
Case "exit"
.......
'Give focus to Exit
MWin.EWin.ExitBtn.Enabled = True
MWin.EWin.YBtn.Enabled = False
MWin.EWin.OkBtn.Enabled = False
MWin.EWin.Abutton = MWin.EWin.ExitBtn
MWin.EWin.ExitBtn.Focus()
Case Else
....
End Select
可能 enable/disable 直接使用按钮的容器(即分组框)可能更可取,只是为了确保隐藏容器中的所有内容都不会干扰您的逻辑(例如,当用户使用 TAB 键在控件之间移动)