Is there a way to get the form active control?
I was checking in the help for the "Support.GetActiveControl"
method but it isn't supported :) for the Compact Framework.
I suppose that I can track the focus by adding a custom GotFocus
event handler to all the form controls, but I'm looking for a more generic way that can be implemented for any form.
Every ContainerControl (including Form) has an ActiveControl property. You can use this to get the currently active control. You may have to hook into either the Leave or LostFocus events of any controls you're interested in and save the last active control in a local variable to be able to revert to the previous control.
Remarks. The ActiveControl property activates or retrieves the active control on the container control. In order to receive a valid value from this property, the object that calls it must either contain or be contained in the control it is calling. If one form tries to call another form's ActiveControl properties,...
The ActiveControl property activates or retrieves the active control on the container control. In order to receive a valid value from this property, the object that calls it must either contain or be contained in the control it is calling.
To use the form controls in Excel 2010 and later versions, you have to enable the Developer tab. To do this, follow these steps: Click File, and then click Options. Click Customize Ribbon in the left pane. Select the Developer check box under Main Tabs on the right, and then click OK.
This example displays the name of the currently selected Windows Forms control in a Label control.
private void DisplayNameOfActiveControl()
{
label1.Text = this.ActiveControl.Name;
}
You can iterate over all the controls in the form and check which one is focused.
Example: Getting ActiveControl in Compact Framework
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With