概要
cscの作法、調べてみた。
練習問題、やってみた。
練習問題
小遣い帳アプリを作れ。
方針
- .NETFramework2.0
- datagridview使う。
- データの永続化は、csv
- 項目は、5つ
日付,内訳,貰ったお金,使ったお金,残金 - 日付入力でカレンダー表示
- 日付でソート
- ボタンは、4つ
load save sort calc
写真
サンプルコード
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data.OleDb;
using System.Data;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
class form1: Form {
MonthCalendar cal1;
DataGridView gd0;
int row;
int coloum;
form1() {
Text = "Kozu";
ClientSize = new Size(580, 400);
gd0 = new DataGridView();
gd0.Location = new Point(50, 20);
gd0.Width = 500;
gd0.Height = 300;
Controls.AddRange(new Control[] {
gd0
});
Button btn1 = new Button();
btn1.Location = new Point(50, 330);
btn1.Text = "load";
btn1.Click += btn1_Click;
Controls.AddRange(new Control[] {
btn1
});
Button btn2 = new Button();
btn2.Location = new Point(150, 330);
btn2.Text = "save";
btn2.Click += btn2_Click;
Controls.AddRange(new Control[] {
btn2
});
Button btn3 = new Button();
btn3.Location = new Point(250, 330);
btn3.Text = "sort";
btn3.Click += btn3_Click;
Controls.AddRange(new Control[] {
btn3
});
Button btn4 = new Button();
btn4.Location = new Point(350, 330);
btn4.Text = "calc";
btn4.Click += btn4_Click;
Controls.AddRange(new Control[] {
btn4
});
gd0.CellClick += new DataGridViewCellEventHandler(OnChanged);
}
private void OnChanged(object sender, DataGridViewCellEventArgs e) {
int ri = e.RowIndex;
int ci = e.ColumnIndex;
if (ci == 0)
{
this.cal1 = new System.Windows.Forms.MonthCalendar();
this.cal1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.cal1_DateSelected);
gd0.Controls.Add(cal1);
cal1.Show();
coloum = e.ColumnIndex;
row = e.RowIndex;
}
}
private void cal1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e) {
gd0[coloum, row].Value = e.Start.ToString("yyyy/MM/dd");
(sender as MonthCalendar).Dispose();
}
void btn1_Click(object sender, System.EventArgs e) {
Encoding enc = Encoding.GetEncoding("UTF-8");
string file = "kozu.csv";
StreamReader reader = new StreamReader(file, enc);
string temp = reader.ReadToEnd();
reader.Close();
string[] lines = temp.Split(new string[] {"\r\n"}, StringSplitOptions.None);
string[] spLine = lines[0].Split(new char[] {','}, StringSplitOptions.None);
gd0.ColumnCount = 5;
gd0.Columns[0].HeaderText = "日付";
gd0.Columns[1].HeaderText = "内訳";
gd0.Columns[2].HeaderText = "貰ったお金";
gd0.Columns[3].HeaderText = "使ったお金";
gd0.Columns[4].HeaderText = "残金";
DataGridViewRow[] rows = new DataGridViewRow[lines.Length - 2];
for (int i = 1; i < lines.Length - 1; i++)
{
spLine = lines[i].Split(new char[] {','}, StringSplitOptions.None);
for (int j = 0; j < spLine.Length; j++)
{
spLine[j] = spLine[j].Trim('"');
}
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(gd0);
row.SetValues(spLine);
rows[i - 1] = row;
}
gd0.Rows.AddRange(rows);
}
void btn2_Click(object sender, System.EventArgs e) {
StreamWriter sw = new StreamWriter("kozu.csv", false);
sw.WriteLine("日付,内訳,貰ったお金,使ったお金,残金");
int rowCount = gd0.Rows.Count;
for (int i = 0; i < rowCount - 1; i++)
{
List<String> strList = new List<String>();
for (int j = 0; j < gd0.Columns.Count; j++)
{
String s2 = Convert.ToString(gd0[j, i].Value);
strList.Add(s2);
}
String[] strArray = strList.ToArray();
String strCsvData = String.Join(",", strArray);
sw.WriteLine(strCsvData);
}
sw.Close();
}
void btn3_Click(object sender, System.EventArgs e) {
ListSortDirection sortDirection = ListSortDirection.Ascending;
gd0.Sort(gd0.Columns[0], sortDirection);
}
void btn4_Click(object sender, System.EventArgs e) {
int rowCount = gd0.Rows.Count;
int pre = 0;
for (int i = 0; i < rowCount - 1; i++)
{
int inp = 0;
int oup = 0;
int zan = 0;
String s2 = Convert.ToString(gd0[2, i].Value);
if (s2 != "")
{
inp = Convert.ToInt32(s2);
}
String s3 = Convert.ToString(gd0[3, i].Value);
if (s3 != "")
{
oup = Convert.ToInt32(s3);
}
String s4 = Convert.ToString(gd0[4, i].Value);
if (s4 != "")
{
zan = Convert.ToInt32(s4);
}
pre = inp - oup + zan + pre;
gd0[4, i].Value = pre;
}
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。