Make user control display outside of form boundry

18,221

Solution 1

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();

Solution 2

The screenshots looks like a Windows Forms applications, so my answer is for winforms.

I guess the best solution would be to create a customcontrol that itself uses the datetime picker that already has the behavior.

Show a empty textbox until it gets clicked, then display the datetimepicker.

That would save you a bunch of code..

Solution 3

I ran into this when trying to implement a custom control and discovered that it's a remarkably hard problem. There's no built-in functionality within the Windows.Forms model to support controls whose display area extends outside the client area of their container.

You basically have to either use the Windows API or draw your controls inside a Form with AlwaysOnTop set. Both approaches are harder than they should be. I ended up redesigning my control so that instead of displaying its expanded contents in a dropdown it used a modal dialog. This was a pretty unsatisfying solution, but I spent a couple of weeks trying other approaches and could never get anything that worked consistently across all use cases (like disappearing when the application loses focus).

Solution 4

I'm not 100% sure, but a quick look at the DateTimePicker class on Reflector takes me to the SafeNativeMethods.SetWindowPos internal class.

You can override the SetBoundsCore from the base Control class or, like Tigraine stated, create a custom control based on the DateTimePicker.

Hope it helps, Bruno Figueiredo

Share:
18,221

Related videos on Youtube

Danny Frencham
Author by

Danny Frencham

Contract programmer.

Updated on April 16, 2022

Comments

  • Danny Frencham
    Danny Frencham about 2 years

    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!

  • Danny Frencham
    Danny Frencham over 15 years
    That's a good suggestion, I will give that a try tomorrow. I'm still keen to know if there is a way to answer my original question though! :)
  • Danny Frencham
    Danny Frencham over 15 years
  • Danny Frencham
    Danny Frencham over 15 years
    Holy crap, this code is the most awesome code ever. I'm integrating this into my user control, thanks.
  • Danny Frencham
    Danny Frencham over 15 years
    It turned out I couldn't go down this path. I started with the control above, but there is no way to set the control up so that the user can tab to it and start typing - an important feature for the user I am writing for.
  • jblaske
    jblaske about 14 years
    I've implemented this code, but the popup keeps stealing focus. Is there a way to prevent this?
  • Jordan
    Jordan over 12 years
    Have tried this with a ListBox as the content control but it always displays at its smallest possible size. Anyone else seen this? Likewise with jblaske, the popup steals focus which is not wanted.
  • Adam Butler
    Adam Butler over 11 years
    Just found an lgpl implementation using this technique here: codeproject.com/Articles/17502/Simple-Popup-Control