Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make user control display outside of form boundry

Tags:

c#

winforms

I've decided to reimplement the datetime picker, as a standard datetime picker isn't nullable. The user wants to start with a blank field and type (not select) the date.

I've created a user control to do just that, but if the user control is near the edge of the form, it will be cut off on the form boundry. The standard datetime picker doesn't suffer from this problem.

Here is a picture showing the problem. My user control is on the left, the standard datetimepicker is on the right:

alt text http://img50.imageshack.us/img50/9104/datetimepickervu6.jpg

As you can see, the standard control will display over the form AND application boundry. How do I get the month picker in my control to do the same thing?

Thanks!

like image 929
Danny Frencham Avatar asked Nov 11 '08 13:11

Danny Frencham


1 Answers

The ToolStripDropDown control has this functionallity so by inheriting from it we can make a simple PopupWindow.

/// <summary>
/// A simple popup window that can host any System.Windows.Forms.Control
/// </summary>
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);
    }
}

Usage:

PopupWindow popup = new PopupWindow(MyControlToHost);
popup.Show(new Point(100,100));
...
popup.Close();
like image 186
Jesper Palm Avatar answered Oct 06 '22 00:10

Jesper Palm