Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I want that when i press Ctrl + S then my check box is checked or unchecked
but when I just Press Ctrl key and before pressing S key KeyDown event is fired
i want that key down event should fired on Ctrl an S Not on Just Ctrl

Here is The Code!!

C#
private void frlLOOKUP_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.ControlKey && e.KeyCode == Keys.S)
        {
            if (check_search.Checked)
            {
                check_search.Checked = false;
            }
            else
            {
               check_search.Checked = true;
            }
        }
    }
Posted
Updated 11-Jun-12 2:52am
v3
Comments
[no name] 11-Jun-12 7:44am    
Only a small hint...for checking/unchecking you dont need "a big" if statement. You can make it simply like this:

check_search.Checked= !check_search.Checked;

C#
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.KeyCode == Keys.S) && (e.Modifiers == Keys.Control))
        checkBoxAck.Checked = !checkBoxAck.Checked;
}


Make sure that KeyPreview of the form is set to true.

Regards
 
Share this answer
 
There are two options for you:

  1. Compare the keyEventArgs property Modifier against Key.Control (not Keys.ControlKey as you did in your sample).

    C#
    private void frlLOOKUP_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.S)
        {
            check_search.Checked = !check_search.Checked;
        }
    
    }
    

  2. If you can live with the short cut being ALT + S you can set the Text property of your CheckBox control to something like "Check&Search".
    check_search.Text = "Check&Search";

    This will allow you to toggle the CheckBox by pressing ALT + S.


Regards,

Manfred
 
Share this answer
 
v4
Comments
VJ Reddy 11-Jun-12 11:16am    
Good answer. 5!

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