Click here to Skip to main content
15,891,930 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I wonder if there is a way to turn checkbox on and off with only mouse click, not spacebar.

Could anyone tell me about it?

Thanks in advance!

What I have tried:

-----------------------------------------------------------------
Posted
Updated 10-Jun-17 22:51pm
Comments
You need to handle the keyup event and check spacebar key code in that to restrict.
MinYoung Lee 11-Jun-17 2:50am    
Thank you, Tadit Dash! I solved it.
Kornfeld Eliyahu Peter 11-Jun-17 4:04am    
Why it is technically possible to block different input types and favor others, it is very unwise in most cases... By blocking input devices you block users with only that type of device (in most case these are people with disabilities)...

1 solution

Already answered by Tadit Dash, but here is my code. Don't ask me why it also works with checked or unchecked state :)
C#
private bool checkBoxKeyFlag;

        // Prevent CheckBox to be set by keypress.
        private void checkBox1_KeyDown(object sender, KeyEventArgs e)
        {
            this.checkBoxKeyFlag = true;
            this.checkBox1.Checked = false;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxKeyFlag)
            {
                this.checkBox1.Checked = false;
                this.checkBoxKeyFlag = false;
            }
        }
 
Share this answer
 
v2
Comments
MinYoung Lee 15-Jun-17 1:50am    
Sorry for late response. Thank you, RickZeeland!
RickZeeland 2-Aug-17 12:28pm    
I forgive you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900