Make user control display outside of form boundry

后端 未结 5 1837
慢半拍i
慢半拍i 2020-12-24 09:46

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.

相关标签:
5条回答
  • 2020-12-24 10:25

    The reason that your control gets chopped off is because it is a child control of the form that you reside on. Any control on the form must be contained by the form, hence it gets chopped off.

    I haven't done this in .Net, but had a similar problem in VB6. The solution then was to set the parent of the popup window (the calendar in your case) to be the desktop. This will allow it to extend beyond the boundaries of your form. You'll have to do some P/Invoke magic to find the hWnd of the popup, and another P/Invoke to set the parent.

    0 讨论(0)
  • 2020-12-24 10:43

    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();
    
    0 讨论(0)
  • 2020-12-24 10:45

    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).

    0 讨论(0)
  • 2020-12-24 10:45

    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

    0 讨论(0)
  • 2020-12-24 10:52

    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..

    0 讨论(0)
提交回复
热议问题