Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>ActiveControl on a Form or Container <em>will</em> return that entity's active control no matter how deeply it might be nested inside other containers.</p> <p><strong>In your example if the TextBox has Focus : then : for Form, TableLayoutPanel, and FlowLayoutPanel : the 'ActiveControl property <em>of all of them</em> will be the TextBox !</strong></p> <p>Some, but not all, "genuine" ContainerControl types ... like Form and UserControl ... expose Key Events (in the case of Form : only if Form.KeyPreview == true can they be used) . </p> <p>Other controls which, by design, contain other controls like TableLayOutPanel, GroupBox, Panel, FlowLayoutPanel, etc. are <strong>not</strong> type ContainerControl, and they do not expose KeyEvents.</p> <p>Any attempt to cast <em>instances</em> of objects like TextBox, FlowLayoutPanel, TableLayoutPanel <em>directly</em> to ContainerControl will not compile : they are not type ContainerControl.</p> <p>The code in the accepted answer, and in the next answer that corrects the first answer's spelling errors, will compile/accept instances of the above as parameters because you are "downcasting" them to type 'Control by making the parameter type 'Control</p> <p>But in each case the cast to ControlContainer will return null, and the passed in instance will be returned (downcasted) : essentially a no-op.</p> <p>And, yes, the modified answer code will work if you pass it a "genuine" ControlContainer, like a Form instance, which is in the parent inheritance path of the ActiveControl, but you are still just wasting time duplicating the function of 'ActiveControl.</p> <p>So what are "genuine" ContainerControls : check them out : <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol(VS.100).aspx?queryresult=true" rel="noreferrer">MS docs for ContainerControl</a></p> <p>Only the answer by Peter really answers the explicit question, but that answer carries the price of using interop, and 'ActiveControl will give you what you need.</p> <p>Also note that every Control (container or non-container) has a Controls Collection that is never null, and that a lot of (I've never tried all of them : why would I ?) the basic WinForms control let you do "crazy stuff" like adding Controls to the ControlCollection of 'simple' controls like Button without an error.</p> <p><strong>Now if the <em>real intent</em> of your question was to ask how you find the outermost ContainerControl</strong> ... <em>that is not on the Form itself</em> ... of a <em>regular non-container</em> Control nested some arbitrary levels deep ... you can use some of the <em>ideas</em> in the answer : but the code can be greatly simplified.</p> <p>Regular Controls, ContainerControls, UserControls, etc. (but not Form !) all have a 'Container property you can access to get their immediate container, but making sure you have the 'final Container in their inhertance path that's not a Form requires some code to "walk-up" the inheritance tree, which is demonstrated here.</p> <p>You may also wish to check out the 'HasChildren property of 'Control which is usually useful in dealing with issues of Focus, ActiveControl, and Select in WinForms. Reviewing the difference between Select and Focus can be valuable here, and SO has some good resources on that.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload