Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the form active control?

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.

like image 330
PabloG Avatar asked May 28 '10 13:05

PabloG


People also ask

How to get the current active control of a 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.

What does the activecontrol property do?

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,...

What is the use of activecontrol in a container?

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.

How do I enable the form controls in Excel 2010?

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.


2 Answers

This example displays the name of the currently selected Windows Forms control in a Label control.

private void DisplayNameOfActiveControl()
{
    label1.Text = this.ActiveControl.Name;
}
like image 156
Sherif Hassaneen Avatar answered Sep 19 '22 12:09

Sherif Hassaneen


You can iterate over all the controls in the form and check which one is focused.

Example: Getting ActiveControl in Compact Framework

like image 24
Itay Karo Avatar answered Sep 21 '22 12:09

Itay Karo