Skip to content

Instantly share code, notes, and snippets.

@mrgarita
Last active November 21, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgarita/7519eb9d1d13d78d9730f701df926f62 to your computer and use it in GitHub Desktop.
Save mrgarita/7519eb9d1d13d78d9730f701df926f62 to your computer and use it in GitHub Desktop.
英単語アプリ3:メニュー項目追加(終了、ABC順にソート、登録順にソート)
/*
* 英単語アプリ3
* メニュー項目を追加
* ・終了メニュー
* ・リストボックス内の単語をソートするメニューを追加
* ABC順にソート
* 登録順にソート
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace 英単語アプリ3
{
public partial class Form1 : Form
{
// 単語保存リスト
private List<string> tangoList = new List<string>();
// データファイル
const string DATAFILE = "tango.txt";
public Form1()
{
InitializeComponent();
}
// 単語データを読み込むメソッド
private void LoadData()
{
try
{
// ファイルを読み込み形式で開く
StreamReader file = new StreamReader(DATAFILE, Encoding.UTF8);
// 読み取ったデータをリストボックスに設定
string line = "";
while((line = file.ReadLine()) != null)
{
tangoList.Add(line);
listBox1.Items.Add(line);
}
// ファイルを閉じる
file.Close();
} catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
// 単語データを保存するメソッド
private void SaveData()
{
try
{
// ファイルを書き込み形式で開く
StreamWriter file = new StreamWriter(DATAFILE, false, Encoding.UTF8);
// 単語リスト1つを1行毎に書き込みをする
for (int i = 0; i < tangoList.Count; i++)
{
file.WriteLine(tangoList[i].ToString());
}
// フィアルを閉じる
file.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
// アプリ起動時の処理
private void Form1_Load(object sender, EventArgs e)
{
// フォーム表示初期設定
this.Text = Application.ProductName;
label1.Text = "英単語";
label2.Text = "日本語";
button1.Text = "追 加";
toolStripStatusLabel1.Text = "起動しました";
this.ActiveControl = this.textBox1; // textBox1.Focus()はフォームロード時は使えない
登録順ToolStripMenuItem.Checked = true; // ソートメニューのチェック表示
// データ読み込み
LoadData();
}
// 単語追加処理
private void button1_Click(object sender, EventArgs e)
{
// 未入力なら何もしない
if (textBox1.Text == "" || textBox2.Text == "") return;
// 英語と日本語をタブ区切り文字列にする
string en = textBox1.Text;
string ja = textBox2.Text;
string tango = en + "\t" + ja;
// リストに追加
tangoList.Add(tango);
// リストボックスに追加
listBox1.Items.Add(tango);
// 追加後テキストボックスをクリア
textBox1.Text = textBox2.Text = "";
// フォーカスを移動
textBox1.Focus();
}
// アプリ終了時の処理
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// データを保存して終了
SaveData();
}
// リストボックスをクリックしたときの処理(特になにもしていない)
private void listBox1_Click(object sender, EventArgs e)
{
// 選択されているリストボックスのアイテム番号を取得(0~)
int index = listBox1.SelectedIndex;
// 選択アイテムを表示
string[] items = listBox1.Items[index].ToString().Split('\t');
toolStripStatusLabel1.Text = items[0] + "&" + items[1];
}
// 終了メニュー
private void 終了XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
// ソートメニュー(ABC順にソート)
private void aBC順ToolStripMenuItem_Click(object sender, EventArgs e)
{
// tangoListの中身を値渡しでtangoListWorkにコピー
List<string> tangoListWork = new List<string>(tangoList);
// ABC順にソート
tangoListWork.Sort();
// リストボックスをクリア
listBox1.Items.Clear();
// ソートした単語リストをリストボックスに反映させる
foreach(string tango in tangoListWork)
{
listBox1.Items.Add(tango);
}
// メニューのチェック表示を変更
登録順ToolStripMenuItem.Checked = false;
aBC順ToolStripMenuItem.Checked = true;
}
// ソートメニュー(登録順にソート)
private void 登録順ToolStripMenuItem_Click(object sender, EventArgs e)
{
// リストボックスクリア
listBox1.Items.Clear();
// 単語リストの順に再表示
foreach (string tango in tangoList)
{
listBox1.Items.Add(tango);
}
// メニューのチェック表示を変更
aBC順ToolStripMenuItem.Checked = false;
登録順ToolStripMenuItem.Checked = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment