简体   繁体   中英

C# winforms, How can I make a Combobox open upwards / Is there one that does?

In C#, a winforms application,

How can I make a Combobox that opens upwards / Is there one that opens upwards?

I see some questions like this but they refer to a WPF application rather than a winforms one. I'm asking about a winforms application.

This is not easy when working with WinForms, but I could give you my idea.

You should know that even when the combobox visible property is set to false, its' dropdown menu can still be displayed programmatically. So the idea is that creating a user control that contains 2 comboboxs:

Example of my user control

The upper combobox is invisible. When user clicks the main, below one, the upper one will be relocated and drop down.

        private void cbb_main_Click(object sender, EventArgs e)
    {
        cbb_2.Location = new Point(cbb_main.Location.X, cbb_main.Location.Y - cbb_2.Size.Height - (cbb_2.ItemHeight *cbb_2.Items.Count));
        cbb_main.DroppedDown = false;
        cbb_2.DroppedDown = true;
    }

Finally, add your custom control to the form and give it a try! Result image here!

I hope this is what you're looking for.

If there is enough in your combobox, and your form is low enough, that the contents of the combobox hits as far as very near the bottom of the screen, then it will open upwards.

Downwards

在此处输入图片说明


Now i'll drag the form down a little bit. (Or I could add some elements to the combobox) And you see it opens upwards.

Upwards

在此处输入图片说明


Sure it would be better if it popped upwards when it covered a bit of taskbar(or if the combobox took priority in z-order, which it doesn't seem to). An annoying thing is if it's in a limbo where it is covering the taskbar 'cos the contents aren't falling low enough for it to pop upwards. Then when you select something low in the combobox you also hover over something in the taskbar, then windows 7 can open eg an explorer window as you hover over a group of them in the taskbar. (And btw that issue exists whatever size your taskbar is). But in that scenario, as this answer says, you could drag the form a bit lower to get around that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM