Last active
November 21, 2017 01:59
-
-
Save mrgarita/f759f896b98d206b0c52137e965cc621 to your computer and use it in GitHub Desktop.
英単語アプリ4:修正機能を追加
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 英単語アプリ4 | |
* 修正機能を追加 | |
* 単語追加用のテキストボックスを利用して修正する機能を作成 | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace 英単語アプリ4 | |
{ | |
public partial class Form1 : Form | |
{ | |
// 単語保存リスト | |
private List<string> tangoList = new List<string>(); | |
// データファイル | |
const string DATAFILE = "tango.txt"; | |
// アプリのモード 0: 単語追加モード 1: 単語修正モード | |
int mode = 0; | |
// 単語修正用 | |
string editKey = ""; // 検索キー | |
int editIndex = 0; // 修正するインデックス番号 | |
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(); | |
} | |
// 単語の追加処理(mode = 0)と修正処理(mode = 1) | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
// 未入力なら何もしない | |
if (textBox1.Text == "" || textBox2.Text == "") return; | |
// 単語の追加または修正を行う | |
if (mode == 0) // 追加モード | |
{ | |
// 英語と日本語をタブ区切り文字列にする | |
string en = textBox1.Text; | |
string ja = textBox2.Text; | |
string tango = en + "\t" + ja; | |
// リストに追加 | |
tangoList.Add(tango); | |
// リストボックスに追加 | |
listBox1.Items.Add(tango); | |
} | |
else if(mode == 1) // 修正モード | |
{ | |
// 単語リストデータを更新 | |
int index = tangoList.IndexOf(editKey); // 検索キーから単語リストのインデックス番号を取得 | |
tangoList.RemoveAt(index); // 取得したインデックス番号のリストを消去 | |
tangoList.Insert(index, textBox1.Text + "\t" + textBox2.Text); // 修正データを単語リストに挿入 | |
// リストボックスに修正結果を反映させる | |
listBox1.Items[editIndex] = textBox1.Text + "\t" + textBox2.Text; | |
// 追加モードに戻す | |
button1.Text = "追 加"; | |
toolStripStatusLabel1.Text = textBox1.Text + " を修正しました"; | |
mode = 0; | |
} | |
// テキストボックスをクリア | |
textBox1.Text = textBox2.Text = ""; | |
// フォーカスを移動 | |
textBox1.Focus(); | |
} | |
// アプリ終了時の処理 | |
private void Form1_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
// データを保存して終了 | |
SaveData(); | |
} | |
// リストボックスをクリックしたときの処理 | |
private void listBox1_Click(object sender, EventArgs e) | |
{ | |
toolStripStatusLabel1.Text = "単語をダブルクリックで修正できます"; | |
} | |
// 終了メニュー | |
private void 終了XToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
this.Close(); | |
} | |
// ソートメニュー(ABC順にソート) | |
private void aBC順ToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
// 修正モードなら何もしない | |
if (mode == 1) return; | |
// 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) | |
{ | |
// 修正モードなら何もしない | |
if (mode == 1) return; | |
// リストボックスクリア | |
listBox1.Items.Clear(); | |
// 単語リストの順に再表示 | |
foreach (string tango in tangoList) | |
{ | |
listBox1.Items.Add(tango); | |
} | |
// メニューのチェック表示を変更 | |
aBC順ToolStripMenuItem.Checked = false; | |
登録順ToolStripMenuItem.Checked = true; | |
} | |
// リストボックスをダブルクリックした時、単語修正モードにする | |
private void listBox1_DoubleClick(object sender, EventArgs e) | |
{ | |
// 修正モードに変更 | |
mode = 1; | |
button1.Text = "修 正"; | |
// ダブルクリックしたリストボックスのインデックス番号を取得 | |
editIndex = listBox1.SelectedIndex; | |
// リストボックスの文字列を検索キーに設定 | |
editKey = listBox1.Items[editIndex].ToString() ; | |
// テキストボックスに修正する単語を表示 | |
string[] items = listBox1.Items[editIndex].ToString().Split('\t'); | |
textBox1.Text = items[0]; // 英語 | |
textBox2.Text = items[1]; // 日本語 | |
// ステータスラベルに表示 | |
toolStripStatusLabel1.Text = items[0] + " を修正中"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment