title: Detecting which control is active
body: <p>I have a setup with a textbox and a listbox in which I want the listbox to be visible only when either the textbox or the listbox is focused. To begin with the listbox will be invisible. Then as the textbox is focused the listbox should be visible. Only when either looses focus and neither receives it should the listbox become invisible again. So what I want to achieve is the following to be true at all times:</p>
  
  <p><code>listBox.Visible = textBox.Focused || listBox.Focused</code></p>
  
  <p>In my solution the textbox and/or the listbox can be a specialization of the standard type (if this simplifies the solution).</p>
  
  <p>I've tried listening on both controls LostFocus and GotFocus events and setting the visibility of the listbox there. However the GotFocus event is never called on the listbox since I turn it invisible when the textbox looses focus.</p>
  
  <p>Does anyone have suggestions for how to achieve this? My backup plan (which I dislike) is to have a timer start when one looses focus and stop if the other receives focus. So a "cleaner" way of achieving it would be greatly appreciated ;)</p>
  
  <p><strong>Edit:</strong> I am developing this to be used as a .NET WinForms custom control.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p><strong>It seems that I haven't explained myself quite good enough. So I'll try to explain what I want to achieve more explicitly.</strong></p>
  
  <p>What I am creating a new type of TextBox (inheriting an existing type of TextBox I build some years ago). The TextBox is currently functioning as a generic collection (including a subtype that implements IList) of objects. The TextBox defines a ListBox which displays a subset of the elements in the collection based on a set of criteria. As the TextBox is added to a parent control, so is the ListBox. Thus the two work as a search-for-element-in-a-collection kind of setup. Sort of the same thing as when your browser makes suggestions as you start typing.</p>
  
  <p>I am using this setup instead of a UserControl containing both controls because the latter would result in either the usercontrol being the size of both controls or (when the size matches the TextBox) the ListBox being invisible.</p>
  
  <p>I realize that I could opt to handle GotFocus or Enter events on all controls in the form to which this control is added. This is a quite straightforward approach which I have thought of, but I would greatly prefer to write a control which does not listen on each and every other control in the form.</p>
  
  <p>I hope this clarifies.</p>