Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All

I have coded a small calculator to do basic + , - , * , /. Now I want this calculator form to be called ( by a hot key say F3 ) from each control eg textbox or datagridview cell and the value of calculator should fill in that textbox/datagridview cell from which it is called
Please Help..
Thanks in Advance

What I have tried:

I an able to call it from a specific textbox..but I want a general code so that it can be called from any textbox control without having to write code for each textbox
Posted
Updated 29-Sep-17 0:04am
Comments
CHill60 29-Sep-17 6:00am    
Put the code into a handler and attach that to each textbox.

1 solution

Override the Form.ProcessCmdKey method, and check for the key there:
C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    Keys keyOnly = keyData & ~Keys.Modifiers;
    Keys modifiersOnly = Control.ModifierKeys & (Keys.Shift | Keys.Control | Keys.Alt);
    if (modifiersOnly == 0)
        {
        // Key alone
        switch (keyOnly)
            {
            case Keys.F3:
                ...
                return true;
            }
        }
    return false;
    }
This lets your form handle the key before it gets to any controls.
You then need to find the focussed control, but that's covered here: .net - What is the preferred way to find focused control in WinForms app? - Stack Overflow[^]
And you can then find the right cell for a DataGridView using the SelectedIndex and SelectedCells properties.
 
Share this answer
 

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