Layout

ツールボックスから Tool を貼り付けて Layout を整えます。

前田稔の超初心者のプログラム入門

Tool の配置

  1. 私は空のプロジェクトをテンプレートに使いましたが、自動生成のプロジェクトでも同じ要領です。
    CSForm.cs(自動生成のときは Form1.cs) のデザイン画面を表示してツールボックスから貼り付けます。
    form をドラッグしてサイズを 1200*600 に引き伸ばします。
  2. ツールボックスから MenuStrip, ToolStrip の順に貼り付けます。
    ページ先頭のタイトルバーに続く次の2行が、これに当たります。
    SplitContainer を上3行を除いて画面一杯に貼り付けます。
  3. SplitContainer の左側に TreeView を一杯に貼り付けます。
    SplitContainer の右側に RichTextBox を一杯に貼り付けます。
    ToolStrip(3行目)の右側に TextBox を右端まで貼り付けます。
    TextBox の名前は Title に設定して下さい。
    レイアウトはページ先頭の画像を参照して下さい。
  4. 画面を拡大/縮小したときにレイアウトが保たれるように次のコードを追加して下さい。
        this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
    
  5. MyForm() の Constructor を修正します。
    this.Resize はウインドウサイズが変更されたときの処理です。
        public MyForm()
        {
            InitializeComponent();
            this.Resize += new System.EventHandler(this.FormResize);
        }
    
  6. InitializeComponent() 関数の splitContainer1.Panel2 に MouseMove のイベントを設定します。
        // splitContainer1.Panel2
        this.splitContainer1.Panel2.Controls.Add(this.richTextBox1);
        this.splitContainer1.Size = new System.Drawing.Size(1178, 495);
        this.splitContainer1.SplitterDistance = 255;
        this.splitContainer1.TabIndex = 2;
        this.splitContainer1.MouseMove += new MouseEventHandler(this.splitContainer1_MouseMove);
    
  7. FormResize() と splitContainer1_MouseMove() ではウインドウの変更による TextBox の配置を設定し直します。
        private void FormResize(object sender, EventArgs e)
        {
            Title.Left = this.splitContainer1.Panel2.Left;
            Title.Width = this.splitContainer1.Panel2.Width;
        }
        private void splitContainer1_MouseMove(object sender, EventArgs e)
        {
            Title.Left = this.splitContainer1.Panel2.Left;
            Title.Width = this.splitContainer1.Panel2.Width;
        }
    
  8. マウスのドラッグでウインドウサイズを変えたり、SplitContainer の境界をドラッグしてツールが正常に配置されることを確認して下さい。
    完成した InitializeComponent() のソースコードを掲載します。
    MyForm では Title が隠れないように this.ClientSize = に続いて定義して下さい。
        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.Title = new System.Windows.Forms.TextBox();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(1178, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // toolStrip1
            // 
            this.toolStrip1.Location = new System.Drawing.Point(0, 24);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(1178, 25);
            this.toolStrip1.TabIndex = 1;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 49);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.treeView1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.richTextBox1);
            this.splitContainer1.Size = new System.Drawing.Size(1178, 495);
            this.splitContainer1.SplitterDistance = 255;
            this.splitContainer1.TabIndex = 2;
            this.splitContainer1.MouseMove +=
                new System.Windows.Forms.MouseEventHandler(this.splitContainer1_MouseMove);
            // 
            // treeView1
            // 
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(255, 495);
            this.treeView1.TabIndex = 0;
            // 
            // richTextBox1
            // 
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 0);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(919, 495);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // Title
            // 
            this.Title.Location = new System.Drawing.Point(257, 25);
            this.Title.Name = "Title";
            this.Title.Size = new System.Drawing.Size(935, 25);
            this.Title.TabIndex = 3;
            // 
            // MyForm
            // 
            this.ClientSize = new System.Drawing.Size(1178, 544);
            this.Controls.Add(this.Title);
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.toolStrip1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "MyForm";
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();
        }
    

[Next Chapter ↓] Memo Tree View
[Previous Chapter ↑] memo2 Read File

超初心者のプログラム入門(C# Frame Work)