Windows Form : Control Focus function not working

This post is related to Windows Form : Control Focus function not working issue.

Sometimes there is a case when you want to set the focus on a particular control in your windows form. To do this you have a function focus for the controls. By using this you can set the focus on a particular control but you need to remember some limitations to use this function. Which is a control can be selected and receive input focus if all the following are true: the Selectable value of ControlStyles is set to true, it is contained in another control, and all its parent controls are both visible and enabled.

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms

control.Select();

The following code snippet sets focus on the specified Control, if it satisfy the requirements to set focus on it:

public void ControlSetFocus(Control control)
{
  // Set focus to the control, if it can receive focus.
  if(control.CanFocus)
  {
    control.Focus();
  }
}

You can read the complete guidelines on this topic using below microsoft link:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx

If you like this post comment on this. So that I can post more useful information. If you have any suggestions on how to improve this blog please suggest.

Deepak Arora

Deepak Arora is a Software Developer and working continuously in this field from 2011. He work mainly with Microsoft Technology. He holds a Master's in Computer Application and he is MCTS certified person.

Leave a Reply

Your email address will not be published. Required fields are marked *