Enter Key simulate click not working for checkboxes

可紊 提交于 2019-12-31 02:42:08

问题


I have copied and slightly modified the code found at the bottom of this post to allow tabbing between form controls on a worksheet form I have created. Here is the segment relating to the KeyCode = vbKeyEnter:

Select Case KeyCode
Case vbKeyTab
    Dim Backward As Boolean
    Backward = (Shift And fmShiftMask)

    Call MoveFocus(Cntrl:=Cntrl, TabIndex:=TabIndex, Backward:=Backward)

Case vbKeyEnter
    Select Case TypeName(Cntrl)
    Case TxtBoxType
        If Not (Cntrl.EnterKeyBehavior And Cntrl.MultiLine) Then
            Call MoveFocus(Cntrl:=Cntrl, TabIndex:=TabIndex)
        End If
    Case Else
        On Error Resume Next
        Application.Run Sheet1.CodeName & "." & Cntrl.name & "_Click"
        On Error GoTo 0
    End Select
End Select

The code works fine for tabbing forward and backward between controls. However, I'm having troubles using the enter key to check and uncheck check box controls. It works for option buttons, but does not work check boxes. If I add CheckBox1.Value = True to the CheckBox1_Click event it works to check it true, but naturally won't allow it to uncheck to false. I've tried adding the following IF statement to set it to true or false depending on current value, but then nothing happens.

If CheckBox1.Value = True Then
    CheckBox1.Value = False
Else
    CheckBox1.Value = True
End If

Another note: When I hit enter and nothing happens to the form control it moves the active cell down the active column. Any suggestions?


回答1:


This may or may not be an answer to the programming question; I will let the voters decide.
Q. Enter Key simulate click not working for checkboxes

Why would you do such a thing? You are spending time and effort to intentionally 'break' a working system. The spacebar has long been the accepted keystroke for toggling a checkbox on and off, Tab and Shift+Tab are used for navigating between form controls and Enter↵ is the default for OK.

By 'working system' I mean the whole Mac/Windows/Linux GUI system and the millions of people that have trained to work within long defined standard practises. You can take the mouse away from any competent office worker and their productivity will not suffer. They can navigate through the system and applications to perform their work because everything in the system from setting the video resolution to typing workers' timecard data into a custom form works the same way.

Take a Windows user and stick them in front of their first Mac. They can use the programs and get the job done because the vast majority of operations (and particularly the ones for basic operation of an application) are the same. Take a Mac user and stick them in front of a Linux machine and you will get the same results. The basic day-to-day operation of the machine and its programs are the same.

What is the point of any prospective applicant looking for employment to come with a CV filled with office experience if you are going to create a proprietary system that takes all of their training, practise and experience and throws it out the window?

The use of the spacebar to toggle a checkbox on and off predates Windows 3.0. There is a difference between building a better mousetrap and reinventing the wheel¹.


¹ If you don't think that maintaining GUI standards is something to strive for, consider the backlash that the Office 2007 ribbon created. An entire sub-industry dedicated to producing add-ons that brought back Office 2003 functionality was spawned.



来源:https://stackoverflow.com/questions/35423208/enter-key-simulate-click-not-working-for-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!