Differentiate pressing F2 on active TAB

I have a complicated Winform. I use a lot of tabs to reduce complexity, but there is a small problem that I do not know how to solve.

Suppose I have a winform screen called "Example.cs". I have a lot of TABLES on the screen. In each tab, I have a button "F2 - Save." When the user presses the F2 button, I shoot and do 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 should I find the button click event that the user intends to trigger, since there are many save buttons in the same WinForm?

Thanks.

+4
source share
1 answer

If you have a set of attached documents in TabControl , this does not mean that there should be a save button for each tab. Here you should have one save button and draw the current active tab when you click the save button. Then you can get the object that you need to save on this tab. You can select the active control from the active tab using a property of type

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

Also, do not simulate a click event to complete your work. Create a method that requires a job and call it from your code. You should also use this method inside event handlers.

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1491395/


All Articles