views:

204

answers:

3

I want to develop a dialog using windows forms in C# that resembles the "outlook 2007 send/receive progress" in behavior. I developed most of the functionality but am having trouble when I need to implement the "Details" button function.

Essentially, the dialog has a progress bar and tabbed panel below the progress bar. on the right of the progress bar, there is a button which shows "Details". Essentially, when you click on this button, it either shows or hides the tab control (which contains status messages) AND re-sizes the form to fit the remaining controls (i.e. shrink or grow)

I have used a basic form, default layout manager, on which I have a progress bar and a button etc just like the outlook one. when the user clicks on the "Details" button, I make the tab control visible property false. I expected the form to resize but it doesn't. If I use the autosize and autosizemode it works but with those properties set I can't anchor the tab control.

what I need is: - For the form to shrink when the tab control is invisible or hidden - But when it is shown, and form is resized, I want the tab control to grow with the form.

Is there a way to achieve this? I tried flowlayout and tablelayout but they don't seem to help...I also tried to remove the tabcontrol from the form's Controls collection and then call PerformLayout () but none of that works.

any pointers? thanks sb

+3  A: 

You also need to code the resizing of your form. You should not care for the tab's anchoring since you are hiding it. Just add a code to resize the form every time the show\hide details button is clicked.

Jojo Sardez
yes, I thought of that but was wondering if there was a simpler way of doing this w/o writing my own resize code. Thanks though.
sb
A: 

Have you tried setting Dock = System.Windows.Forms.DockStyle.Fill; on your tab control? It should resize with the form in that case.

You can add your other controls to two other panels that you set Dock.Top and Dock.Bottom on to have controls over and under your tab control. If you are working with Visual Studio's visual designer, don't forget that you can right click on a control and click "Bring to front" or "Bring to back" if the controls lay out under each other.

Patrick
A: 

It's quite simple, just try this one:

Form1.Designer.cs

namespace DetailsSample
{
    partial class Form1
    {
        /// <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 Windows Form 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.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(12, 12);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(268, 62);
            this.textBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.Location = new System.Drawing.Point(205, 80);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox2
            // 
            this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox2.Location = new System.Drawing.Point(12, 109);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(268, 152);
            this.textBox2.TabIndex = 2;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox2;
    }
}

Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DetailsSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Height > 0)
            {
                this.Size = new Size(this.Size.Width, button1.Location.Y +
                                                      button1.Height +
                                                      button1.Margin.Bottom +
                                                      27);

                button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                textBox1.Anchor |= AnchorStyles.Bottom;
                textBox2.Anchor ^= AnchorStyles.Top;
            }
            else
            {
                button1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
                textBox1.Anchor ^= AnchorStyles.Bottom;
                textBox2.Anchor |= AnchorStyles.Top;

                this.Size = new Size(this.Size.Width, textBox2.Location.Y +
                                                      160);
            }
        }
    }
}

It's note quite perfect, but it gives you a good starting point, by what you can achieve with just setting the Anchors and through this implicit the height to zero.

Also the hardcoded values for the height (27 and 160) are a little bit ugly. For the 160 you should save somewhere the last height of your bottom control and for the 27 you can use some kind of PointToScreen calculation to find out how much space the title bar occupied.

Oliver
Yes, this works but I was trying to avoid having to write my own resize code. Thanks though.
sb