FormWords.cs

using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EnglishWords
{
public partial class FormWords : System.Windows.Forms.Form
{
public FormWords()
{
InitializeComponent();
}
private int rowIndex = -1;
private readonly string filePath = @"EnglishWords.text";

//------------------------------------------------------------------------------------------
//<自作メソッド>

private void TextClear()
{
textBoxEnglish.Text = "";
textBoxJapanese.Text = "";
textBoxExample.Text = "";
checkBoxJug.Checked = false;
textBoxEnglish.Focus();
}

//</自作メソッド>
//-------------------------------------------------------------------------------------------

private void ButtonRegister_Click(object sender, EventArgs e)
{
if (textBoxEnglish.Text == "" ||
textBoxJapanese.Text == "")
{
MessageBox.Show("「英語」「日本語」は省略できません","注意",0, MessageBoxIcon.Warning, 0);
textBoxEnglish.Focus();
return;
}
else if (textBoxEnglish.Text.Contains('~') ||
textBoxJapanese.Text.Contains("~") ||
textBoxExample.Text.Contains("~"))
{
MessageBox.Show("テキストに~(チルダ)を含むことはできません", "注意", 0, MessageBoxIcon.Warning, 0);
return;
}
else
{
dataGridView1.Rows.Add(textBoxEnglish.Text,
textBoxJapanese.Text,
textBoxExample.Text,
checkBoxJug.Checked,
DateTime.Now);
TextClear();
}
}


private void ButtonClear_Click(object sender, EventArgs e)
{
TextClear();
}

private void FormMain_Load(object sender, EventArgs e)
{

if (File.Exists(filePath))
{
var index = 0;
var lines = File.ReadLines(filePath, Encoding.UTF8);
foreach (var line in lines)
{
if (line != null)
{
string[] words = line.Split('~');

dataGridView1.Rows.Add(words[index],
words[index + 1],
words[index + 2],
bool.Parse(words[index + 3]),
DateTime.Parse(words[index + 4]));
}
}
}}

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
using (var writer = new StreamWriter(filePath))
{
for (var i = 0; i < dataGridView1.RowCount; i++)
{
writer.WriteLine(dataGridView1.Rows[i].Cells[0].Value.ToString() + "~"
+ dataGridView1.Rows[i].Cells[1].Value.ToString() + "~"
+ dataGridView1.Rows[i].Cells[2].Value.ToString() + "~"
+ dataGridView1.Rows[i].Cells[3].Value.ToString() + "~"
+ dataGridView1.Rows[i].Cells[4].Value.ToString());
}
}
}

private void ToolStripMenuItemDelete_Click(object sender, EventArgs e)
{
rowIndex = dataGridView1.CurrentRow.Index;
if (DialogResult.Yes == MessageBox.Show($"[{dataGridView1.Rows[rowIndex].Cells[0].Value.ToString()}]削除しますか?",
"確認",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question))
{
dataGridView1.Rows.RemoveAt(rowIndex);
TextClear();
rowIndex = -1;
}
}

private void ToolStripMenuItemEdit_Click(object sender, EventArgs e)
{
rowIndex = dataGridView1.CurrentRow.Index;
buttonClear.Enabled = false;
buttonRegister.Enabled = false;
buttonEdit.Enabled = true;
dataGridView1.Enabled = false;
textBoxEnglish.Text = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString();
textBoxJapanese.Text = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
textBoxExample.Text = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
checkBoxJug.Checked = bool.Parse( dataGridView1.Rows[rowIndex].Cells[3].Value.ToString());
}

private void ButtonEdit_Click(object sender, EventArgs e)
{
dataGridView1.Rows[rowIndex].Cells[0].Value = textBoxEnglish.Text;
dataGridView1.Rows[rowIndex].Cells[1].Value = textBoxJapanese.Text;
dataGridView1.Rows[rowIndex].Cells[2].Value = textBoxExample.Text;
dataGridView1.Rows[rowIndex].Cells[3].Value = checkBoxJug.Checked;
dataGridView1.Rows[rowIndex].Cells[4].Value = DateTime.Now;
rowIndex = -1;
buttonClear.Enabled = true;
buttonRegister.Enabled = true;
buttonEdit.Enabled = false;
dataGridView1.Enabled = true;
TextClear();
}
}
}


無料でホームページを作成しよう! このサイトはWebnodeで作成されました。 あなたも無料で自分で作成してみませんか? さあ、はじめよう