tags:

views:

93

answers:

4

I am writing a UserControl that will act kinda like a drop down list, basically a textbox with a listbox that pops up underneath it...

If the control is at the bottom of its parent form, I do not want the listbox to be clipped by the forms bottom, so from what I gather I cant just have this listbox as a child of the parent form.. I need to create a new borderless form containing the listbox and display this in the right position.

I can do this fine, the problem comes with sorting out the z-order of the forms. I need this new form to appear above the parent form at all times. But I dont want this window to appear above any windows that are above the parent form... which is what setting form.TopMost would give me.

Is there any other way to do this? How do ComboBoxes manage to achieve this behaviour?

Thanks

+3  A: 

When you call form.Show(), pass in the user control's owner (the current form) as the owner parameter. This code isn't perfect, but you'll get the idea.

class MyControl : UserControl
{
    private DropDownForm form = new DropDownForm();

    public MyControl()
    {
        form.FormClosed += dropdownform_closed;
    }

    private void MethodThatShowsDropdown()
    {
        form.AddData(GetTheData());
        form.Show(this.Owner); // Or is it "this.Parent"?  I can never remember...
    }

    private void dropdownform_closed(object sender, EventArgs e)
    {
        DoSomething(form.SelectedValue);
    }
}
Neil Barnwell
Additionally, you can set the Owner property on a form in case it was not shown by your code directly.
Marek
When I run this code, the form just appears and disappears. Why not just `(new DropDownForm()).Show(this)`?
jheddings
Eek - oops. The form is being disposed (and therefore closed) immediately. I'll change the example...
Neil Barnwell
Yep thats sorted it out nicely, thanks!!
Mongus Pong
A: 

I'm not sure if it would work for your application, but using ShowDialog() to display your "list form" would keep it in the right place. The disadvantage is that it will not allow you to return to the parent form while the list is open.

jheddings
A: 

You should only need to provide the parent form when calling ShowModalDialog or Show. When you do this the form is a child of the first and will alway overlap it in the z-order.

csharptest.net
A: 

It sounds like you're making this a little more complex than it needs to be.

You should just display the listbox and have the same form that parents the textbox be the paretn of the listbox. No need to use another form to hold the listbox control. That parenting should give you your proper z-order. To avoid clipping, just place the listbox above the textbox (or to left/right as necessary) to make sure it's always displayed. The math for this is fairly simple.

All you should have to do then is handle the enter/leave focus events of the text box to show/hide the listbox. That's pretty much what the combobox does. (i'm assuming that you want to mimic combo's behavior.)

Paul Sasik
The clipping issue was where the listbox needs to not be clipped by the boundry of the form. If you put a combo box right at the bottom of a form, and drop it down, you'll see the drop-list extends below the form, because it's actually another form in itself.
Neil Barnwell