none
Panel Autosizing ?

    Question

  • Okay,

    So, I've used the TableLayoutPanel, and it works wonderfully as I've gotten better at it.  I've even made some enhancements with descendent classes, and the greatest feature about it is the AutoSizing. It takes some getting used to, but with a modicum of work you can have all the controls you want inside of it, autosized to perfection.  But, of course, there comes a time when you don't really want everything in a layout control, you want something more user friendly.

    So,  I wanted to use a SplitContainer to have a combo box on the left and a text box on the right.  Both set to DockStyle.Fill, and then have the SplitContainer inside a TableLayoutPanel.  The Layout RowStyle was set to AutoSize, and suddenly i discover the SplitContainer doesn't have an AutoSize property (or more appropriate that it hides the Default Control.AutoSize property). 

    So, I figured I'd use a Panel, with a Splitter.  Same effect, just different path.  So, I add the Panel to the Form, add the ComboBox, dock it to the left, add a splitter, dock it to the left, add the text box, docked to fill, and then set the Panel's AutoSize property to true, and the mode to GrowAndShrink.

    Not a good idea, as the Panel then shrinks to 3 pixels tall.  What is that?  Doesn't the autosize pay attention to the child controls?  the ComboBox was still at height 21 and the textbox at height 20, but they were completely obfuscated by the panel that shrunk to a height of 1.  Is this just a flaw that we are screwed into having to deal with, or is there something I'm missing here?

    Thanks

    Jaeden "Sifo Dyas" al'Raec Ruiner


    "Never Trust a computer. Your brain is smarter than any micro-chip."
    PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
    Thursday, October 06, 2011 2:49 PM

Answers

  • Hi,

    set the MinimumSize for the Controls. Here's a demo UserControl.Designer-File that shrinks to the width of textbox.width + splitter.width + combobox.width and a height of about 20...

    namespace WindowsFormsApplication1
    {
        partial class UserControl1
        {
            /// <summary> 
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary> 
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Component Designer generated code
    
            /// <summary> 
            /// Required method for Designer support - do not modify 
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.comboBox1 = new System.Windows.Forms.ComboBox();
                this.splitter1 = new System.Windows.Forms.Splitter();
                this.panel2 = new System.Windows.Forms.Panel();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.panel1.SuspendLayout();
                this.panel2.SuspendLayout();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.Controls.Add(this.comboBox1);
                this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
                this.panel1.Location = new System.Drawing.Point(0, 0);
                this.panel1.MinimumSize = new System.Drawing.Size(100, 0);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(151, 190);
                this.panel1.TabIndex = 0;
                // 
                // comboBox1
                // 
                this.comboBox1.Dock = System.Windows.Forms.DockStyle.Top;
                this.comboBox1.FormattingEnabled = true;
                this.comboBox1.Items.AddRange(new object[] {
                "sfadd"});
                this.comboBox1.Location = new System.Drawing.Point(0, 0);
                this.comboBox1.MinimumSize = new System.Drawing.Size(100, 0);
                this.comboBox1.Name = "comboBox1";
                this.comboBox1.Size = new System.Drawing.Size(151, 21);
                this.comboBox1.TabIndex = 0;
                // 
                // splitter1
                // 
                this.splitter1.Location = new System.Drawing.Point(151, 0);
                this.splitter1.Name = "splitter1";
                this.splitter1.Size = new System.Drawing.Size(3, 190);
                this.splitter1.TabIndex = 1;
                this.splitter1.TabStop = false;
                // 
                // panel2
                // 
                this.panel2.AutoSize = true;
                this.panel2.Controls.Add(this.textBox1);
                this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
                this.panel2.Location = new System.Drawing.Point(154, 0);
                this.panel2.MinimumSize = new System.Drawing.Size(100, 0);
                this.panel2.Name = "panel2";
                this.panel2.Size = new System.Drawing.Size(223, 190);
                this.panel2.TabIndex = 2;
                // 
                // textBox1
                // 
                this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
                this.textBox1.Location = new System.Drawing.Point(0, 0);
                this.textBox1.MinimumSize = new System.Drawing.Size(100, 20);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(223, 20);
                this.textBox1.TabIndex = 0;
                // 
                // UserControl1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.Controls.Add(this.panel2);
                this.Controls.Add(this.splitter1);
                this.Controls.Add(this.panel1);
                this.Name = "UserControl1";
                this.Size = new System.Drawing.Size(377, 190);
                this.panel1.ResumeLayout(false);
                this.panel2.ResumeLayout(false);
                this.panel2.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            public System.Windows.Forms.Panel panel1;
            public System.Windows.Forms.ComboBox comboBox1;
            public System.Windows.Forms.Splitter splitter1;
            public System.Windows.Forms.Panel panel2;
            public System.Windows.Forms.TextBox textBox1;
    
        }
    }
    
    

    Regards,

      Thorsten

    Friday, October 07, 2011 9:12 PM

All replies

  • Can you post code or upload your custom split container ?
    Thursday, October 06, 2011 6:52 PM
  • It isn't a custom anything. It's a .Net Panel with a ComboBox, Splitter, and TextBox.

    Create a WinApp, Add a Panel to the form, add a combobox, set dock to left. add a splitter with dock to the left, add a textbox with dock to fill, then set the panel to autosize=true, autosizemode = growandshrink and you'll see what i'm talking abou.

    thanks

    J"SD"a'RR


    "Never Trust a computer. Your brain is smarter than any micro-chip."
    PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
    Friday, October 07, 2011 6:55 PM
  • GrowAndShrink mode is not designed for docked controls.

    If you have child control docked in container with GrowAndShrink size mode, child control tries to size to the parent and the parent tries to size to the child.

     

    Friday, October 07, 2011 7:32 PM
  • Hi,

    set the MinimumSize for the Controls. Here's a demo UserControl.Designer-File that shrinks to the width of textbox.width + splitter.width + combobox.width and a height of about 20...

    namespace WindowsFormsApplication1
    {
        partial class UserControl1
        {
            /// <summary> 
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary> 
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Component Designer generated code
    
            /// <summary> 
            /// Required method for Designer support - do not modify 
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.comboBox1 = new System.Windows.Forms.ComboBox();
                this.splitter1 = new System.Windows.Forms.Splitter();
                this.panel2 = new System.Windows.Forms.Panel();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.panel1.SuspendLayout();
                this.panel2.SuspendLayout();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.Controls.Add(this.comboBox1);
                this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
                this.panel1.Location = new System.Drawing.Point(0, 0);
                this.panel1.MinimumSize = new System.Drawing.Size(100, 0);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(151, 190);
                this.panel1.TabIndex = 0;
                // 
                // comboBox1
                // 
                this.comboBox1.Dock = System.Windows.Forms.DockStyle.Top;
                this.comboBox1.FormattingEnabled = true;
                this.comboBox1.Items.AddRange(new object[] {
                "sfadd"});
                this.comboBox1.Location = new System.Drawing.Point(0, 0);
                this.comboBox1.MinimumSize = new System.Drawing.Size(100, 0);
                this.comboBox1.Name = "comboBox1";
                this.comboBox1.Size = new System.Drawing.Size(151, 21);
                this.comboBox1.TabIndex = 0;
                // 
                // splitter1
                // 
                this.splitter1.Location = new System.Drawing.Point(151, 0);
                this.splitter1.Name = "splitter1";
                this.splitter1.Size = new System.Drawing.Size(3, 190);
                this.splitter1.TabIndex = 1;
                this.splitter1.TabStop = false;
                // 
                // panel2
                // 
                this.panel2.AutoSize = true;
                this.panel2.Controls.Add(this.textBox1);
                this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
                this.panel2.Location = new System.Drawing.Point(154, 0);
                this.panel2.MinimumSize = new System.Drawing.Size(100, 0);
                this.panel2.Name = "panel2";
                this.panel2.Size = new System.Drawing.Size(223, 190);
                this.panel2.TabIndex = 2;
                // 
                // textBox1
                // 
                this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
                this.textBox1.Location = new System.Drawing.Point(0, 0);
                this.textBox1.MinimumSize = new System.Drawing.Size(100, 20);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(223, 20);
                this.textBox1.TabIndex = 0;
                // 
                // UserControl1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.Controls.Add(this.panel2);
                this.Controls.Add(this.splitter1);
                this.Controls.Add(this.panel1);
                this.Name = "UserControl1";
                this.Size = new System.Drawing.Size(377, 190);
                this.panel1.ResumeLayout(false);
                this.panel2.ResumeLayout(false);
                this.panel2.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            public System.Windows.Forms.Panel panel1;
            public System.Windows.Forms.ComboBox comboBox1;
            public System.Windows.Forms.Splitter splitter1;
            public System.Windows.Forms.Panel panel2;
            public System.Windows.Forms.TextBox textBox1;
    
        }
    }
    
    

    Regards,

      Thorsten

    Friday, October 07, 2011 9:12 PM
  • Well,

    This would seem to the case, except for the fact that neither the textbox, nor combobox are set to "AutoSize" themselves.  This would indicate that the TextBox/ComboBox would control that the child-parent resize issue.  As well, the Panel behaves in this way regardless of whether it is docked or not, only autosizing based upon the docking behavior of the child controls.  This is in opposition to the behavior of the TableLayoutPanel.

    The TableLayoutPanel contains a Docked TextBox, with RowStyle set to AutoSize, and the TableLayoutPanel set to GrowAndShrink in it's AutoSizeMode, and the TableLayoutPanel does not shrink itself to 2 pixels tall, it expands to contain all the controls with their appropriate margins etc, and displays correctly, even when those child controls are set to DockStyle.Fill. Why the TableLayoutPanel will autosize correctly and the panel does not is the question.

    Setting the minimum size somewhat defeats the purpose.  It appears to only be an issue with the Panel.  I have done similar things with nested TableLayoutPanel's, but for this one circumstance I wanted to provide user cusomization of the width of the controls (combo and textbox) without affecting the height, which cannot be done with a TableLayoutPanel.  With Thorsten's suggestion, this would work, if I set the combobox's MinimumSize to { 0, 21 }, as the combobox is defaultly slightly bigger vertically than the TextBox, but still seems like an added step with other container controls do not require this to affect the same behavior.

    J"SD"a'RR


    "Never Trust a computer. Your brain is smarter than any micro-chip."
    PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
    Monday, October 10, 2011 5:20 PM