【发布时间】:2015-03-18 15:56:12
【问题描述】:
我有一个包含三个面板的表单,每个面板中都有不同的按钮。根据显示表单之前的一些设置,三个面板中的两个被隐藏(宽度设置为 0)。在相同的设置代码中,我也一直在尝试调整用户按 Enter 键时“单击”的按钮,使其成为面板中未隐藏但不起作用的按钮之一。当我在测试时按 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 行,但 OkBtn 始终是在按下 Enter 时“点击”的内容。最后一点,对于相关表单的 Load 或 Shown 事件没有代码,因此没有任何与上述代码相矛盾的地方。任何关于我所缺少的指导将不胜感激
【问题讨论】:
-
禁用其他按钮(IE:MWin.EWin.ExitBtn.Enabled = False/True 等等,具体取决于上下文)
-
明白了,谢谢史蒂夫!