0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その551

Last updated at Posted at 2025-02-06

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

csvを一行ずつ処理せよ。

方針

  • datatable使う。

写真

image.png

サンプルコード

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Text;
using System.IO;

class form1: Form {
	TextBox bo0;
	TextBox bo1;
	TextBox bo2;
	TextBox bo3;
	DataTable dt;
	int n = 0;
	form1() {
		Text = "くだもの";
		ClientSize = new Size(700, 250);
		Label la0 = new Label();
		la0.Location = new Point(50, 10);
		la0.Text = "index";
		bo0 = new TextBox();
		bo0.Location = new Point(150, 10);
		bo0.Text = "id";
		Label la1 = new Label();
		la1.Location = new Point(50, 40);
		la1.Text = "";
		bo1 = new TextBox();
		bo1.Location = new Point(150, 40);
		bo1.Text = "";
		Label la2 = new Label();
		la2.Location = new Point(50, 70);
		la2.Text = "色";
		bo2 = new TextBox();
		bo2.Location = new Point(150, 70);
		bo2.Text = "";
		Label la3 = new Label();
		la3.Location = new Point(50, 100);
		la3.Text = "くだもの";
		bo3 = new TextBox();
		bo3.Location = new Point(150, 100);
		bo3.Text = "";
		Button btn1 = new Button();
		btn1.Location = new Point(150, 130);
		btn1.Text = "up";
		btn1.Click += btn1_Click;
		Button btn2 = new Button();
		btn2.Location = new Point(350, 130);
		btn2.Text = "down";
		btn2.Click += btn2_Click;
		Button btn3 = new Button();
		btn3.Location = new Point(500, 130);
		btn3.Text = "更新";
		btn3.Click += btn3_Click;
		Controls.AddRange(new Control[] {
			la0,
			bo0,
			la1,
			bo1,
			la2,
			bo2,
			la3,
			bo3,
			btn1,
			btn2,
			btn3
		});
		this.Load += new System.EventHandler(this.form1_Load);
	}
	private void form1_Load(object sender, System.EventArgs e) {
		dt = new DataTable();
		dt.Columns.Add("No", typeof(int));
		dt.Columns.Add("Iro", typeof(string));
		dt.Columns.Add("Name", typeof(string));
		Encoding enc = Encoding.GetEncoding("Shift_JIS");
		using (StreamReader sr = new StreamReader("dt8.csv", enc))
		{
			while (sr.EndOfStream == false)
			{
				string line = sr.ReadLine();
				string[] fields = line.Split(',');
				dt.Rows.Add(fields[0], fields[1], fields[2]);
			}
		}
		for (int i = 0; i < dt.Rows.Count; i++)
		{
			DataRow row = dt.Rows[i];
			Console.WriteLine("No: {0}, Iro: {1}, Name: {2}", row["No"], row["Iro"], row["Name"]);
		}
	}
	void btn1_Click(object sender, System.EventArgs e) {
		n++;
		if (n > dt.Rows.Count - 1) 
			n--;
		DataRow row = dt.Rows[n];
		bo0.Text = n.ToString();
		bo1.Text = row["No"].ToString();
		bo2.Text = row["Iro"].ToString();
		bo3.Text = row["Name"].ToString();
	}
	void btn2_Click(object sender, System.EventArgs e) {
		n--;
		if (n < 0) 
			n++;
		DataRow row = dt.Rows[n];
		bo0.Text = n.ToString();
		bo1.Text = row["No"].ToString();
		bo2.Text = row["Iro"].ToString();
		bo3.Text = row["Name"].ToString();
	}
	void btn3_Click(object sender, System.EventArgs e) {
	}
	[STAThread]
	public static void Main() {
		Application.EnableVisualStyles();
		Application.SetCompatibleTextRenderingDefault(false);
		Application.Run(new form1());
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?