ListBox に TEXT を表示する

ソースファイルを入力して ListBox に表示します。

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

プロジェクトの設定

  1. TextBox は一連の文章を扱うのに対して、ListBox は「アイテム(行)」を単位として扱います。
    ListBox は、名簿の一覧や商品名の一覧など、アイテムの一覧を表示するのに適しています。
  2. 空のプロジェクトを作成して下さい。
    詳細は Form を作成する を参照して下さい。
  3. [デザイン] を表示して、[ツールボックス] から [ListBox] をフォームに貼り付けます。
    ソースファイルの幅に合わせて、ウインドウの幅と ListBox の幅を調整して下さい。
  4. MyForm の Constructor から InitializeComponent() を呼び出します。
    これだけで ListBox が表示されます。
        public MyForm()
        {   InitializeComponent();
        }
        
  5. ソースファイルを入力して ListBox に表示してみましょう。
    TEXT ファイルを扱うので System.IO と System.Text を定義します。
    "..\\..\\CSForm.cs" が ListBox に表示するソースファイル(このプログラム自身)です。
        using System.IO;            // for File, StreamReader
        using System.Text;          // for Encoding
    
        private ListBox listBox1;
        public  string file_name = "..\\..\\CSForm.cs";
        
  6. Load をデリゲート(委譲)して、イベントハンドラ(MyForm_Load)を設定して下さい。
        public MyForm()
        {
            InitializeComponent();
            Load += new System.EventHandler(MyForm_Load);
        }
        
  7. MyForm_Load() でソースファイルを入力して ListBox に登録します。
    reader.ReadLine() でソースファイルから一行ずつ入力します。
    listBox1.Items.Add() で入力した行を ListBox に登録します。
    ListBox のアイテムが溢れると、自動的にスクロールバーが設定されます。
    TEXT File ファイルの入力は TEXT File の入力 を参照して下さい。
        private void MyForm_Load(object sender, System.EventArgs e)
        {   string  str;
    
            if (!File.Exists(file_name))
            {   Console.WriteLine("ファイルが見つかりません {0}", file_name);
                return;
            }
    
            StreamReader reader = new StreamReader(file_name,Encoding.GetEncoding("Shift_JIS"));
            while((str=reader.ReadLine()) != null)
            {
                listBox1.Items.Add(str);
            }
            reader.Close();
        }
        
  8. CLI の環境で実行可能な完成したプログラムです。
    ソースファイル("..\\..\\CSForm.cs") がオープン出来ないときは、適当なファイルをフルパスで指定して下さい。
    /********************************************************/
    /*★ Source File を入力して ListBox に表示    前田 稔 ★*/
    /********************************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;            // for File, StreamReader
    using System.Text;          // for Encoding
    
    public class MyForm : Form
    {
        private ListBox listBox1;
        //public  string file_name = "..\\..\\CSForm.cs";
        public  string file_name = "ListBoxFile.cs";
    
        public MyForm()
        {
            InitializeComponent();
            Load += new System.EventHandler(MyForm_Load);
        }
    
        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 15;
            this.listBox1.Location = new System.Drawing.Point(12, 12);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(869, 229);
            this.listBox1.TabIndex = 0;
            // 
            // MyForm
            // 
            this.ClientSize = new System.Drawing.Size(893, 256);
            this.Controls.Add(this.listBox1);
            this.Name = "MyForm";
            this.ResumeLayout(false);
    
        }
    
        private void MyForm_Load(object sender, System.EventArgs e)
        {   string  str;
    
            if (!File.Exists(file_name))
            {   Console.WriteLine("ファイルが見つかりません {0}", file_name);
                return;
            }
    
            StreamReader reader = new StreamReader(file_name,Encoding.GetEncoding("Shift_JIS"));
            while((str=reader.ReadLine()) != null)
            {
                listBox1.Items.Add(str);
            }
            reader.Close();
        }
    }
    
    class form01
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    

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