We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to put a combo box into a Popup and prevent the dropdown from closing when clicked on it?

 

Normally, when you put a combo box into a popup, the popup will be hidden when you click on the combo box drop-down. This is because the popup framework expects the windows you activate to be .NET control based, otherwise it just gives up.

To work around this problem, use a custom PopupControlContainer, as follows. Check out the inline comments.

C#

public class CustomPopupControlContainer : PopupControlContainer

{

  public CustomPopupControlContainer()

  {

  }

  public CustomPopupControlContainer(IContainer container):this()

  {

   container.Add(this);

  }

  public override bool IsRelatedControl ( System.Windows.Forms.Control control , System.Boolean askPopupParent )

  {

   bool isrelated = base.IsRelatedControl(control, askPopupParent);

   // This will be the case when native windows without dotnet wrappers are encountered

   // like the combo box dropdown.

   if(control == null && !isrelated)

   {

    // If so, then go ahead and get a dotnet parent control.

    IntPtr hwnd = GetFocus();

    // This method goes up the hierarchy looking for a dotnet parent.

    Control dotnetParent = PopupUtils.GetADotNetParentControl(hwnd);

    if(dotnetParent != null)

    // if found then check if that is a related control, instead.

    isrelated |= base.IsRelatedControl(dotnetParent, askPopupParent);

   }

   return isrelated;

  }

[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi)]

public static extern IntPtr GetFocus();

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]

public static extern IntPtr GetParent(IntPtr hwnd);}

Article ID: Published Date: Last Revised Date: Platform: Control:
1077 04/30/2011 07/10/2014 Windows Forms PopupControlContainer
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.