Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show listbox outside of form (winforms)

Tags:

c#

winforms

Is it any possible to get my listbox to be shown outside of the form's bounds?

One of the solutions is to make the form itself transparent, and add the panel instead of the form for the background.

But is there any other, more delightful way to do that?

UPD: i need to make a custom autocomplete for textbox, to support wildcards. so i want a listbox to be shown below the textbox. my form's size should be about the size of the textbox. so, stretching the form vertically doesn't work in this case.

thx

like image 784
0100110010101 Avatar asked Dec 18 '22 08:12

0100110010101


2 Answers

Actually, its possible. Here's the way:

public class PopupWindow : System.Windows.Forms.ToolStripDropDown
{
    private System.Windows.Forms.Control _content;
    private System.Windows.Forms.ToolStripControlHost _host;

    public PopupWindow(System.Windows.Forms.Control content)
    {
        //Basic setup...
        this.AutoSize = false;
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;

        this._content = content;
        this._host = new System.Windows.Forms.ToolStripControlHost(content);

        //Positioning and Sizing
        this.MinimumSize = content.MinimumSize;
        this.MaximumSize = content.Size;
        this.Size = content.Size;
        content.Location = Point.Empty;

        //Add the host to the list
        this.Items.Add(this._host);
    }
}


popup = new PopupWindow(listbox1);
PopupWindow.show(); 
like image 142
0100110010101 Avatar answered Dec 30 '22 10:12

0100110010101


Could you use a second form that only contains the listbox? You'd need a little code to move it relative to the main form, but it should work...

like image 20
Marc Gravell Avatar answered Dec 30 '22 10:12

Marc Gravell