differentiate F2 keypress on active TAB

c#, winforms

Solution

If you have a set of tabbed documents in a `TabControl` this does not mean you have to have a save button for every tab. Here you should have one save button and picup the currently active tab upon the save button click. You will then be able to pick up the object you need to save from that tab. You can pick up the active control from the active tab using a property like

public SomeControlToSave ActiveControl
{
    get
    {
        if (tabControl.TabPages.Count == 0)
            return null;
        return tabControl.SelectedTab.Controls.OfType<SomeControlToSave>().FirstOrDefault();
    }
}

Also, don't simulate a click event to do your work. Create a method that does the requires job and call that from your code behind. You should also use that method inside your event handlers.

I hope this helps.

Problem

I have a complex Winform. I am using many tabs to decrease the complexity but there is a small problem that I dont know how to solve. Lets say I have winform screen called "Example.cs". I have many TABS on the screen. In each Tab, I have button called "F2 - Save". When the user presses F2 button, I capture and do the below ``` protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.F2)) { btn_save.PerformClick(); return true; } return base.ProcessCmdKey(ref msg, keyData); } ``` how am I supposed to find the click event of a button which the user intend to trigger, since there are many "save buttons" on the same WinForm? Thanks.

Original source