Head First C#
頭とからだで覚えるC#の基本
(オライリージャパン)
Andrew Stellman (著), Jennifer Green (著)
佐藤 嘉一 (監修), 木下 哲也 (翻訳)
開発環境
- Microsoft Windows 8.1 Pro (VMware Fusion 6, OS X Mavericks - Apple) (OS)
- C# (プログラミング言語)
- Microsoft Visual Studio Express 2013 for Windows Desktop (統合開発環境, IDE)
Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の8章(例外処理: 消化は時代遅れ)、エクササイズ(p.373)を解いてみる。
エクササイズ(p.373)
コード
Program1.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;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Excuse currentExcuse = new Excuse();
bool formChanged = false;
Random random = new Random();
string selectedFolder = "";
public Form1()
{
InitializeComponent();
}
private void folder_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = selectedFolder;
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
selectedFolder = folderBrowserDialog1.SelectedPath;
save.Enabled = true;
open.Enabled = true;
select.Enabled = true;
}
}
private bool isChanged()
{
if (formChanged)
{
DialogResult result = MessageBox.Show("この口実は保存されません。よろしいですか?",
"警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
return false;
}
}
return true;
}
private void updateForm(bool changed)
{
if (!changed)
{
this.description.Text = currentExcuse.Description;
this.results.Text = currentExcuse.Results;
this.lastUsed.Value = currentExcuse.LastUsed;
if(!String.IsNullOrEmpty(currentExcuse.ExcusePath))
{
fileDate.Text = File.GetLastWriteTime(currentExcuse.ExcusePath).ToString();
}
this.Text = "口実マネージャ";
}
else
{
this.Text = "口実マネージャ*";
}
this.formChanged = changed;
}
private void save_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(description.Text) || String.IsNullOrEmpty(results.Text))
{
MessageBox.Show("口実とその結果を入力してください。", "保存できません");
}
else
{
saveFileDialog1.InitialDirectory = selectedFolder;
saveFileDialog1.Filter = "excuseファイル (*.excuse)|*.excuse|すべてのファイル (*.*)|*.*";
saveFileDialog1.FileName = description.Text + ".excuse";
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
currentExcuse.save(saveFileDialog1.FileName);
updateForm(false);
MessageBox.Show("口実を保存しました", "保存完了");
}
}
}
private void open_Click(object sender, EventArgs e)
{
if(isChanged())
{
openFileDialog1.InitialDirectory = selectedFolder;
openFileDialog1.Filter = "excuseファイル (*.excuse)|*.excuse|すべてのファイル (*.*)|*.*";
openFileDialog1.FileName = description.Text + ".excuse";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
bool clearForm = false;
try
{
currentExcuse = new Excuse(openFileDialog1.FileName);
try
{
MessageBox.Show("最終使用日が設定されていません。");
updateForm(false);
}
catch
{
clearForm = true;
}
}
catch (Exception ex)
{
MessageBox.Show("次の口実ファイルを開くときにエラーが発生しました'" + openFileDialog1.FileName +
"'\nパスを Null にすることはできません。\nパラメータ名: path");
clearForm = true;
}
finally
{
if (clearForm)
{
description.Text = "";
results.Text = "";
lastUsed.Value = DateTime.Now;
}
}
}
}
}
private void select_Click(object sender, EventArgs e)
{
if(isChanged())
{
currentExcuse = new Excuse(random, selectedFolder);
updateForm(false);
}
}
private void description_TextChanged(object sender, EventArgs e)
{
currentExcuse.Description = description.Text;
updateForm(true);
}
private void results_TextChanged(object sender, EventArgs e)
{
currentExcuse.Results = results.Text;
updateForm(true);
}
private void lastUsed_ValueChanged(object sender, EventArgs e)
{
currentExcuse.LastUsed = lastUsed.Value;
updateForm(true);
}
}
[Serializable]
class Excuse
{
private string description;
private string results;
private DateTime lastUsed;
private string excusePath;
public Excuse()
{
excusePath = "";
}
public Excuse(string name)
{
openFile(name);
}
public Excuse(Random random, string folder)
{
string[] files = Directory.GetFiles(folder, "*.excuse");
openFile(files[random.Next(files.Length)]);
}
public string Description
{
get { return description; }
set { this.description = value; }
}
public string Results
{
get { return results; }
set { this.results = value; }
}
public DateTime LastUsed
{
get { return lastUsed; }
set { this.lastUsed = value; }
}
public string ExcusePath
{
get { return excusePath; }
set { this.excusePath = value; }
}
public void openFile(string path)
{
this.excusePath = path;
BinaryFormatter formatter = new BinaryFormatter();
Excuse temp;
using (Stream input = File.OpenRead(path))
{
temp = (Excuse)formatter.Deserialize(input);
}
description = temp.description;
results = temp.results;
lastUsed = temp.lastUsed;
}
public void save(string path)
{
BinaryFormatter formatter = new BinaryFormatter();
Stream output = File.Create(path);
formatter.Serialize(output, this);
output.Close();
}
}
}
0 コメント:
コメントを投稿