locked
How do I Attach a form to a button control (simulating a popup form)? RRS feed

  • Question

  • I have a windows form attached to a button control such that when the button is clicked a form is shown as if it was attached to the button, my code is..

    SetParent(FormColorDialog.Handle, Parent.Handle);
    FormColorDialog.Size = new Size(258, 258);
    FormColorDialog.Location = new System.Drawing.Point((Location.X + Width) - FormColorDialog.Width, Location.Y + Height);
    FormColorDialog.Show();

    and this is what I get

    Form attached to customized combobox cuttoff at main form border.

    Notice the form is cut off at the main form border. This is not intended. It should not be restricted by the border. What is the appropriate method to attach the form?


    innovoCAD

    Monday, June 13, 2016 7:41 PM

Answers

  • Hi innovoCAD,

    Perhaps you could try to use below control(custom 'ToolStripDropDown' control):

        /// <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 = true;
                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);
            }
        }

    Use:

    PopupWindow popup = new PopupWindow(your control);
    popup.Show(new point);
    //close
    popup.Close();

    Reference link:

    Make user control display outside of form boundry

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by innovoCAD Monday, June 27, 2016 12:58 AM
    Tuesday, June 14, 2016 9:01 AM

All replies

  • Monday, June 13, 2016 7:54 PM
  • The video shows how to create a modal form using .ShowDialog() when a button is clicked. That is not the question. As shown in the image the form needs to popup or show when the button is clicked at the location shown relative to the button. The code I provided does this. However, as can be seen in the image, the displayed form is cut off at the border of the main form. I either need to use my current method with modification or an entirely new method to display the form. The form in this case is a customized color picker as shown (although irrelevant to the question.)

    Thank you for you feedback. I appreciate it.


    innovoCAD

    Monday, June 13, 2016 8:37 PM
  • The video shows how to create a modal form using .ShowDialog() when a button is clicked. That is not the question. As shown in the image the form needs to popup or show when the button is clicked at the location shown relative to the button.

    Well, you need to know some calculations about where the button is relative to the main form and adjust the popup screen location and dimensions.

    Is it worth all of that?

    Or is it you just use a modal forum and tell it to center itself over the main forum?

    It may be much to do about nothing. And if you see other applications using the modal forum approach, the modal form is being told to center itself.

    Monday, June 13, 2016 9:00 PM
  • Rather than setting the size in code (probably the wrong size), create the form first in the designer and call it as frm.ShowDialog().  If the form shows up, but still not the correct size, just keep adjusting the size in the designer until you have the desired size.  THEN, if you have the correct looking form, use those dimensions in the code (if you must do this in code).  This is the "Trial and Error" method  (the Thomas Edison method -- or whoever it was that did the actual work -- they did it by Trial and Error).

    Rich P

    Monday, June 13, 2016 9:14 PM
  • Perhaps a alternative as shown in the link below. Yes, it's not exactly the same so it may not work for your needs but worth perhaps looking at it.

    http://www.codeproject.com/Articles/34332/Color-Picker-Combo-Box


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, June 13, 2016 10:27 PM
  • What happens if you remove the call to SetParent()? Seems like the child form is being cropped to region of the parent window.
    Monday, June 13, 2016 11:25 PM
  • I thought of this method, but is not consistent with what I would like to achieve. Although not ruled out this would be a last resort.

    Thank you for your feedback.


    innovoCAD

    Tuesday, June 14, 2016 2:46 AM
  • I believe this is the case. I need the new form to be attached to its parent form at the location of the button. If I remove the SetParent(...), the application will not work properly at this point. Unless there is another method other than SetParent(...) to achieve this end.

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    Thanks for you feedback.


    innovoCAD



    • Edited by innovoCAD Tuesday, June 14, 2016 2:54 AM
    Tuesday, June 14, 2016 2:53 AM
  • If this was an app that I am developing for me or for a typical user base, I would customize the application for the colorpicker to best fit within a fixed size form. However, this is a developer application in which other developers will use the control. And as developers go they like customization and some flexibility. Keeping my customers in mind, I have no idea what size of form they will create for their app and where they will place the control. I need to keep this in mind when designing control.

    Thank you for your feedback.


    innovoCAD

    Tuesday, June 14, 2016 3:09 AM
  • Rich,

    Please refer to my above comment to DA924x. The same applies here....

    This is a developer application in which other developers will use the control. And as developers go they like customization and some flexibility. Keeping my customers in mind, I have no idea what size of form they will create for their app and where they will place the control. I need to keep this in mind when designing control.

    Thank you for your feedback.


    innovoCAD

    Tuesday, June 14, 2016 3:12 AM
  • Hi innovoCAD,

    This forum is about the C# programming, as your issues is more related to the windows form application, we help you move it to the windows form general forum for better support.

    Thank you for your understanding.

    Best Regards,

    Albert Zhang

    Tuesday, June 14, 2016 5:23 AM
  • Hi innovoCAD,

    Perhaps you could try to use below control(custom 'ToolStripDropDown' control):

        /// <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 = true;
                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);
            }
        }

    Use:

    PopupWindow popup = new PopupWindow(your control);
    popup.Show(new point);
    //close
    popup.Close();

    Reference link:

    Make user control display outside of form boundry

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by innovoCAD Monday, June 27, 2016 12:58 AM
    Tuesday, June 14, 2016 9:01 AM
  • Albert Zhang,

    Please note the header above and tagged with C#:

    Windows Forms Windows Form General


    innovoCAD


    • Edited by innovoCAD Thursday, June 23, 2016 12:36 PM
    Thursday, June 23, 2016 12:33 PM
  • Moonlight, you get the gold star....

    I used your approach and solution. I had to do some additional research, testing, and after making adjustments here is an image of the final result.

    Thank you Moonlight. I appreciate your feedback and solution.


    innovoCAD


    • Edited by innovoCAD Monday, June 27, 2016 1:00 AM
    Monday, June 27, 2016 12:57 AM