设置与Enter键关联的按钮

时间:2015-03-18 15:56:12

标签: vb.net winforms button

我的表格有三个面板,每个面板上有不同的按钮。根据显示表单之前的某些设置,隐藏三个面板中的两个(宽度设置为0)。在同样的设置代码中,我也一直在尝试调整点击的按钮"当用户点击进入时,它是面板中未隐藏的按钮之一,但它不起作用。当我在测试时按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始终是&#34; s&#34;点击&#34;当Enter被击中时。最后一点,没有相关表格的加载或显示事件的代码,因此没有任何内容与上述代码相矛盾。任何关于我失踪的指导都会非常感激

1 个答案:

答案 0 :(得分:1)

只需禁用其他按钮,如果它们被隐藏,则无法接收回车键

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

可能最好直接启用/禁用按钮的容器(IE组框),以确保隐藏容器中的所有内容都不会干扰您的逻辑(例如,当用户在使用TAB键控制)