新しいクラスを作成したら、usingを追加します。
using System.IO;を追記するとストリームリーダが使用できるようになります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; //追記using System.Threading.Tasks;
namespace hatenaForm
{
internalclassCSVRead
{
}
}
では、ストリームリーダを使用できるようになりましたので実装していきます。
まずはメソッドを追加します。
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; //追記using System.Threading.Tasks;
namespace hatenaForm
{
internalclassCSVRead
{
publicvoid CsvRead01()
{
}
}
}
他のクラスから呼び出す(参照できるようにする)ため、privateではなくpublicにします。
ストリームリーダで読み込みと書いてある内容をすべて変数に入れるようにwhile文でループします。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; //追記using System.Threading.Tasks;
namespace hatenaForm
{
internalclassCSVRead
{
publicvoid CsvRead01()
{
//リスト型の初期化と宣言
List<string[]> str =new List<string[]>();
//CSVを読み込みモードで開く,文字種はsjis,※代表的なものでいうとUTF-8
StreamReader ls =new StreamReader(@".\Formchange.csv", Encoding.GetEncoding("sjis"));
while (!ls.EndOfStream)
{
// CSVファイルの一行を読み込むstring ln = ls.ReadLine();
// 読み込んだ一行をカンマ毎に分けて配列に格納するstring[] lnvalues = ln.Split(',');
//strに格納
str.Add(lnvalues);
}
//CSVファイルを閉じる
ls.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace hatenaForm
{
publicpartialclassForm2 : Form
{
public Form2()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
this.Visible =false;
Form1 form1 =new Form1();
form1.ShowDialog();
this.Close();
}
privatevoid Form2_Shown(object sender, EventArgs e)
{
Form4 form4 =new Form4();
form4.ShowDialog();
}
}
}
Form4.cs クリックイベントで閉じるだけ。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace hatenaForm
{
publicpartialclassForm4 : Form
{
public Form4()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace hatenaForm
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
this.Visible =false;
Form2 form2 =new Form2();
form2.ShowDialog();
this.Close();
}
}
}
同じく、Form2に書き込みます。
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace hatenaForm
{
publicpartialclassForm2 : Form
{
public Form2()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
this.Visible =false;
Form1 form1 =new Form1();
form1.ShowDialog();
this.Close();
}
}
}