Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
If I call other forms then the current form control loses focus.

So I'll have to give the focus back to the original control.

But I don't know the previously focused control.

How to get the currently focused control?
Posted
Updated 9-Jan-11 8:23am
v2
Comments
Sergey Alexandrovich Kryukov 9-Jan-11 13:31pm    
Forms, WPF, ASP.NET?!
Dalek Dave 9-Jan-11 14:23pm    
Edited for Grammar.

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.
 
Share this answer
 
Comments
Roger Wright 9-Jan-11 12:03pm    
Good call!
Sergey Alexandrovich Kryukov 9-Jan-11 13:32pm    
All correct if OP needs Forms. How do you know.
DaveyM69 9-Jan-11 13:56pm    
From the original question:
"If i call other forms then current form control loses focus."
Most likely Forms :-)
Dalek Dave 9-Jan-11 14:24pm    
Good Answer.
Make use of the tag property(All control's have this property including the form) .You can store the currently focused controls name in the Tag property.Something like this.

C#
btnCall.Tag = txtNotes;


When you are back to the form,check for tag property which you have set & bring back the focus on that control.
 
Share this answer
 
Comments
Dalek Dave 9-Jan-11 14:24pm    
Good Call.
Sandeep Mewara 10-Jan-11 0:43am    
Good Answer.
Anupama Roy 10-Jan-11 13:04pm    
Thanks Dalek & Sandeep!
The .Net Framework doesn't appear to have any function to do this, but I found this article[^] which provides code to implement a Win32 API function to do so. Interesting reading...
 
Share this answer
 
Comments
DaveyM69 9-Jan-11 11:03am    
Nice find Roger - could be useful, for the OPs problem ContainerControl.ActiveControl will get him what he needs.
Dalek Dave 9-Jan-11 14:24pm    
Excellent Link.
if you use the validation also then if u like the focus to control use the below code.


document.GetElementById("txt1").focus();
 
Share this answer
 
// 1) Save the current index control
foreach (Control obj in this.Controls)
{
    int StoredIndex = 0;
    if (obj.Focused)
    {
        StoredIndex = this.Controls.Indexof(obj);
        break;
    }
}
//
// 2) Instance the other form here
// OtherForm.ShowDialog()

// 3) Restore the control focused
this.Controls[StoredIndex].Focus();
 
 I hope help you
 Mauricio de Sousa Coelho
 Software Engineer
 
Share this answer
 
Comments
Osman Bur 14-Jan-20 9:51am    
public static Control FindFocusedControl(Control control)
{
ContainerControl container = control as ContainerControl;
while (container != null)
{
control = container.ActiveControl;
container = control as ContainerControl;
}
return control;
}
//Burada da hangi kontrol Focus edilmiş ise onu verir.
var focused = FindFocusedControl(this);
Aleshin Slawa 3-Mar-23 4:00am    
Mauricio Coelho, great method, thank you very much!

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