SplitContainer

C# の SplitContainer でウインドウを分割します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

ウインドウを分割

  1. C# にはウインドウを分割する便利なツール(SplitContainer)が用意されています。
    これを使ってウインドを左右に分割してみましょう。
    空のプロジェクトを作成して下さい。
    詳細は Form を作成する を参照して下さい。
  2. CSForm.cs の[デザイン]を表示して、ウインドウの幅を広げて下さい。
    [ツールボックス] から [SplitContainer] をフォームに貼り付けます。
    SplitContainer は Form サイズいっぱいに配置され、左が Panel1 右が Panel2 になります。
    ソースプログラム(CSForm.cs)に InitializeComponent() のメソッドが追加されます。
  3. この状態で実行してみましょう。
    MyForm の Constructor から InitializeComponent() を呼び出します。
    SplitContainer の区切りをドラッグすると Panel1 と Panel2 のサイズが変わります。
    /*★ SplitContainer で左右に分ける    前田 稔 ★*/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        private SplitContainer splitContainer1;
    
        public MyForm()
        {   InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Size = new System.Drawing.Size(600, 400);
            this.splitContainer1.SplitterDistance = 300;
            // 
            // MyForm
            // 
            this.ClientSize = new System.Drawing.Size(600, 400);
            this.Controls.Add(this.splitContainer1);
            this.Name = "MyForm";
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);
        }
    }
    
    class form01
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    

プログラムの説明

  1. [デザイン]を表示して Panel1 に ListBox を Panel2 に TextBox を貼り付けます。
    TextBox の既定値は一行だけなので、Multiline を True に設定します。
    SplitContainer の枠一杯に配置して下さい。
  2. ListBox と TextBox に背景色と Dock を設定します。
    InitializeComponent() のメソッドに次のコードを追加して下さい。
        this.listBox1.BackColor = System.Drawing.SystemColors.ControlDark;
        this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.textBox1.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        
  3. SplitContainer の区切りをドラッグすると左右のサイズが変わります。
    ウインドウのドラッグでサイズを拡大・縮小すると ListBox と TextBox も連動します。

テキストを設定

  1. ListBox と TextBox にテキストを表示します。
    MyForm() から MyForm_Load() を呼び出します。
        public MyForm()
        {
            InitializeComponent();
            Load += new System.EventHandler(MyForm_Load);
        }
        
  2. MyForm_Load() で ListBox と TextBox にテキストを設定します。
        private void MyForm_Load(object sender, System.EventArgs e)
        {
            listBox1.Items.Add("0123456789");
            listBox1.Items.Add("abcdegfg");
            listBox1.Items.Add("HIJKLMN");
            textBox1.Text = "Text Box\r\n0123456\r\nTestMessage\r\nMaedaMinoru";
        }
        
  3. Windows(C++) でも同様のプログラムを作成しています。
    超初心者のプログラム入門(Windows)から Window を左右に分割して表示するウインドウの境界をドラッグする を参照して下さい。
    /**********************************************/
    /*★ ListBox と TextBox を並べる    前田 稔 ★*/
    /**********************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        private SplitContainer splitContainer1;
        private ListBox listBox1;
        private TextBox textBox1;
    
        public MyForm()
        {
            InitializeComponent();
            Load += new System.EventHandler(MyForm_Load);
        }
    
        private void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.listBox1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.textBox1);
            this.splitContainer1.Size = new System.Drawing.Size(638, 245);
            this.splitContainer1.SplitterDistance = 364;
            this.splitContainer1.TabIndex = 0;
            // 
            // listBox1
            // 
            this.listBox1.BackColor = System.Drawing.SystemColors.ControlDark;
            this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 18;
            this.listBox1.Location = new System.Drawing.Point(0, 0);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(364, 238);
            this.listBox1.TabIndex = 0;
            // 
            // textBox1
            // 
            this.textBox1.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(270, 245);
            this.textBox1.TabIndex = 0;
            // 
            // MyForm
            // 
            this.ClientSize = new System.Drawing.Size(638, 245);
            this.Controls.Add(this.splitContainer1);
            this.Name = "MyForm";
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.Panel2.PerformLayout();
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);
        }
    
        private void MyForm_Load(object sender, System.EventArgs e)
        {
            listBox1.Items.Add("0123456789");
            listBox1.Items.Add("abcdegfg");
            listBox1.Items.Add("HIJKLMN");
            textBox1.Text = "Text Box\r\n0123456\r\nTestMessage\r\nMaedaMinoru";
        }
    }
    
    class form01
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    

[Next Chapter ↓] FolderView

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