Csharp/C Sharp/GUI Windows Form/Docking — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:33, 26 мая 2010

Control Docking

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace Docking
{
  /// <summary>
  /// Summary description for FormDocking.
  /// </summary>
  public class FormDocking : System.Windows.Forms.Form
  {
    private System.Windows.Forms.StatusBar statusBar1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public FormDocking()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (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.statusBar1 = new System.Windows.Forms.StatusBar();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // statusBar1
      // 
      this.statusBar1.Location = new System.Drawing.Point(0, 253);
      this.statusBar1.Name = "statusBar1";
      this.statusBar1.Size = new System.Drawing.Size(292, 20);
      this.statusBar1.TabIndex = 9;
      this.statusBar1.Text = "statusBar1";
      // 
      // textBox1
      // 
      this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(292, 273);
      this.textBox1.TabIndex = 2;
      this.textBox1.Text = "Top Bottom Left Right";
      // 
      // label1
      // 
      this.label1.Dock = System.Windows.Forms.DockStyle.Top;
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(292, 16);
      this.label1.TabIndex = 10;
      this.label1.Text = "Docking Sample";
      this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      // 
      // FormDocking
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label1,
                                      this.statusBar1,
                                      this.textBox1});
      this.Name = "FormDocking";
      this.Text = "FormDocking";
      this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FormDocking());
    }
  }
}


Docking Demo

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace Docking
{
    /// <summary>
    /// Summary description for Docking.
    /// </summary>
    public class Docking : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.GroupBox GroupBox1;
        internal System.Windows.Forms.Button cmdUpdate;
        internal System.Windows.Forms.NumericUpDown udDockPaddingForm;
        internal System.Windows.Forms.NumericUpDown udDockPaddingPanel;
        internal System.Windows.Forms.ruboBox lstDockPanel;
        internal System.Windows.Forms.Label Label3;
        internal System.Windows.Forms.Label Label4;
        internal System.Windows.Forms.ruboBox lstDockTextBox;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.Panel pnlDock;
        internal System.Windows.Forms.TextBox txtDock;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public Docking()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (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.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.cmdUpdate = new System.Windows.Forms.Button();
            this.udDockPaddingForm = new System.Windows.Forms.NumericUpDown();
            this.udDockPaddingPanel = new System.Windows.Forms.NumericUpDown();
            this.lstDockPanel = new System.Windows.Forms.ruboBox();
            this.Label3 = new System.Windows.Forms.Label();
            this.Label4 = new System.Windows.Forms.Label();
            this.lstDockTextBox = new System.Windows.Forms.ruboBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.pnlDock = new System.Windows.Forms.Panel();
            this.txtDock = new System.Windows.Forms.TextBox();
            this.GroupBox1.SuspendLayout();
            ((System.ruponentModel.ISupportInitialize)(this.udDockPaddingForm)).BeginInit();
            ((System.ruponentModel.ISupportInitialize)(this.udDockPaddingPanel)).BeginInit();
            this.pnlDock.SuspendLayout();
            this.SuspendLayout();
            // 
            // GroupBox1
            // 
            this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.cmdUpdate,
                                                                                    this.udDockPaddingForm,
                                                                                    this.udDockPaddingPanel,
                                                                                    this.lstDockPanel,
                                                                                    this.Label3,
                                                                                    this.Label4,
                                                                                    this.lstDockTextBox,
                                                                                    this.Label2,
                                                                                    this.Label1});
            this.GroupBox1.Location = new System.Drawing.Point(192, 20);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(284, 224);
            this.GroupBox1.TabIndex = 12;
            this.GroupBox1.TabStop = false;
            this.GroupBox1.Text = "Configure";
            // 
            // cmdUpdate
            // 
            this.cmdUpdate.Location = new System.Drawing.Point(160, 180);
            this.cmdUpdate.Name = "cmdUpdate";
            this.cmdUpdate.Size = new System.Drawing.Size(84, 24);
            this.cmdUpdate.TabIndex = 10;
            this.cmdUpdate.Text = "Update";
            this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
            // 
            // udDockPaddingForm
            // 
            this.udDockPaddingForm.Increment = new System.Decimal(new int[] {
                                                                                5,
                                                                                0,
                                                                                0,
                                                                                0});
            this.udDockPaddingForm.Location = new System.Drawing.Point(160, 32);
            this.udDockPaddingForm.Name = "udDockPaddingForm";
            this.udDockPaddingForm.Size = new System.Drawing.Size(52, 21);
            this.udDockPaddingForm.TabIndex = 4;
            // 
            // udDockPaddingPanel
            // 
            this.udDockPaddingPanel.Increment = new System.Decimal(new int[] {
                                                                                 5,
                                                                                 0,
                                                                                 0,
                                                                                 0});
            this.udDockPaddingPanel.Location = new System.Drawing.Point(160, 56);
            this.udDockPaddingPanel.Name = "udDockPaddingPanel";
            this.udDockPaddingPanel.Size = new System.Drawing.Size(52, 21);
            this.udDockPaddingPanel.TabIndex = 5;
            this.udDockPaddingPanel.Value = new System.Decimal(new int[] {
                                                                             20,
                                                                             0,
                                                                             0,
                                                                             0});
            // 
            // lstDockPanel
            // 
            this.lstDockPanel.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList;
            this.lstDockPanel.Location = new System.Drawing.Point(156, 100);
            this.lstDockPanel.Name = "lstDockPanel";
            this.lstDockPanel.Size = new System.Drawing.Size(92, 21);
            this.lstDockPanel.TabIndex = 8;
            // 
            // Label3
            // 
            this.Label3.Location = new System.Drawing.Point(16, 104);
            this.Label3.Name = "Label3";
            this.Label3.Size = new System.Drawing.Size(136, 20);
            this.Label3.TabIndex = 6;
            this.Label3.Text = "Dock Panel To:";
            // 
            // Label4
            // 
            this.Label4.Location = new System.Drawing.Point(16, 128);
            this.Label4.Name = "Label4";
            this.Label4.Size = new System.Drawing.Size(136, 20);
            this.Label4.TabIndex = 7;
            this.Label4.Text = "Dock TextBox To:";
            // 
            // lstDockTextBox
            // 
            this.lstDockTextBox.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList;
            this.lstDockTextBox.Location = new System.Drawing.Point(156, 124);
            this.lstDockTextBox.Name = "lstDockTextBox";
            this.lstDockTextBox.Size = new System.Drawing.Size(92, 21);
            this.lstDockTextBox.TabIndex = 9;
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(16, 60);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(136, 20);
            this.Label2.TabIndex = 3;
            this.Label2.Text = "Set Panel\"s DockPadding:";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(16, 36);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(136, 20);
            this.Label1.TabIndex = 2;
            this.Label1.Text = "Set Form\"s DockPadding:";
            // 
            // pnlDock
            // 
            this.pnlDock.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                  this.txtDock});
            this.pnlDock.Dock = System.Windows.Forms.DockStyle.Left;
            this.pnlDock.DockPadding.All = 20;
            this.pnlDock.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.pnlDock.Name = "pnlDock";
            this.pnlDock.Size = new System.Drawing.Size(224, 314);
            this.pnlDock.TabIndex = 11;
            // 
            // txtDock
            // 
            this.txtDock.Dock = System.Windows.Forms.DockStyle.Left;
            this.txtDock.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.txtDock.Location = new System.Drawing.Point(20, 20);
            this.txtDock.Multiline = true;
            this.txtDock.Name = "txtDock";
            this.txtDock.Size = new System.Drawing.Size(108, 274);
            this.txtDock.TabIndex = 0;
            this.txtDock.Text = "I\"m docked to the edge of this Panel.\r\n\r\nThe Panel is docked to the edge of the f" +
                "orm.\r\n\r\nThe Panel\"s DockPadding gives the necessary room to breathe.";
            // 
            // Docking
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(496, 314);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.GroupBox1,
                                                                          this.pnlDock});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "Docking";
            this.Text = "Docking At Work";
            this.Load += new System.EventHandler(this.Docking_Load);
            this.GroupBox1.ResumeLayout(false);
            ((System.ruponentModel.ISupportInitialize)(this.udDockPaddingForm)).EndInit();
            ((System.ruponentModel.ISupportInitialize)(this.udDockPaddingPanel)).EndInit();
            this.pnlDock.ResumeLayout(false);
            this.ResumeLayout(false);
        }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Docking());
        }
        private void Docking_Load(object sender, System.EventArgs e)
        {
            lstDockPanel.Items.AddRange(Enum.GetNames(Dock.GetType()));
            lstDockTextBox.Items.AddRange(Enum.GetNames(Dock.GetType()));
        }
        private void cmdUpdate_Click(object sender, System.EventArgs e)
        {
            this.DockPadding.All = (int)udDockPaddingForm.Value;
            pnlDock.DockPadding.All = (int)udDockPaddingPanel.Value;
            
            // Now we use some rather unusual code to translate the string
            //  in the listbox into an enumeration object that can be used
            //  to set the Dock property.
            // This looks quite strange, but is actually just one more
            //  part of the shared class library.
            
            // First we get the converter that can do the job.
            TypeConverter converter;
            converter = TypeDescriptor.GetConverter(Dock.GetType());
            
            // Then we use it to convert the string.
            pnlDock.Dock = (DockStyle)converter.ConvertFromString(lstDockPanel.Text);
            txtDock.Dock = (DockStyle)converter.ConvertFromString(lstDockTextBox.Text);
        }
    }
}


Dock Style: Bottom

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
            //dock Bottom
        
      button1.Dock = DockStyle.Bottom;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + 
        "\nDock: " + button1.Dock.ToString();
    }
    static void Main() 
    {
      Application.Run(new AnchorForm());
    }
  }


Dock style: Fill

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
            // dock Fill
      button1.Dock = DockStyle.Fill;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + 
        "\nDock: " + button1.Dock.ToString();
    }
    static void Main() 
    {
      Application.Run(new AnchorForm());
    }
  }


Dock Style: Left

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
            // dock Left
      button1.Dock = DockStyle.Left;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + 
        "\nDock: " + button1.Dock.ToString();
    }
    static void Main() 
    {
      Application.Run(new AnchorForm());
    }
  }


Dock Style: None

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
      
            // dock None
      button1.Dock = DockStyle.None;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + "\nDock: " + button1.Dock.ToString();
      
    }
    static void Main() 
    {
      Application.Run(new AnchorForm());
    }
  }


Dock Style: Right

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
            //dock Right
            button1.Dock = DockStyle.Right;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + 
        "\nDock: " + button1.Dock.ToString();
    }
    static void Main() 
    {
      Application.Run(new AnchorForm());
    }
  }


Dock Style: Top

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
            // dock Top
        
      button1.Dock = DockStyle.Top;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + 
        "\nDock: " + button1.Dock.ToString();

    }
    static void Main() 
    {
      Application.Run(new AnchorForm());
    }
  }


Dock Two Buttons

 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class TwoButtonsDock: Form
{
     public static void Main()
     {
          Application.Run(new TwoButtonsDock());
     }
     public TwoButtonsDock()
     {
          ResizeRedraw = true;
   
          Button btn = new Button();
          btn.Parent = this;
          btn.Text   = "&Larger";
          btn.Height = 2 * Font.Height;
          btn.Dock   = DockStyle.Top;
          btn.Click += new EventHandler(ButtonLargerOnClick);
          
          btn = new Button();
          btn.Parent = this;
          btn.Text   = "&Smaller";
          btn.Height = 2 * Font.Height;
          btn.Dock   = DockStyle.Bottom;
          btn.Click += new EventHandler(ButtonSmallerOnClick);
     }
     void ButtonLargerOnClick(object obj, EventArgs ea)
     {
          Console.WriteLine("large");
     }
     void ButtonSmallerOnClick(object obj, EventArgs ea)
     {
          Console.WriteLine("small");
     }
}