前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
//★ Text File Class
public class FileClass
{
public string path; //ファイルパス
public string file; //ファイル名
public string code; //TEXTコード
public string str; //編集 TEXT
public int cursor; //現在のカーソル位置
public int line; //現在の行番号
public string lstr; //現在行の文字列
public int st, nx; //現在行と次の行の先頭Index
public int lt; //次の行の有効文字
static StreamReader reader;
static StreamWriter writer;
// Constructor
public FileClass(string tpath, string tcode)
{
path = tpath;
code = tcode;
str = string.Empty;
SetFile();
}
// Text File を入力
public void ReadFile(string tcode)
{
code = tcode;
ReadFile();
}
public void ReadFile()
{
if (code==string.Empty)
reader = new StreamReader(path);
else
reader = new StreamReader(path, Encoding.GetEncoding(code));
str = reader.ReadToEnd();
reader.Close();
}
// Text File に保存
public void WriteFile(RichTextBox textbox)
{
TextWrite(textbox, path, code);
}
public void WriteFile(RichTextBox textbox, string tpath, string tcode)
{
TextWrite(textbox, tpath, tcode);
path = tpath;
code = tcode;
SetFile();
}
// RichTextBox のデータをファイル(tpath)に出力
private void TextWrite(RichTextBox textbox, string tpath, string tcode)
{
if (tcode==string.Empty)
writer = new StreamWriter(tpath, false);
else
writer = new StreamWriter(tpath, false, Encoding.GetEncoding(tcode));
writer.Write(textbox.Text);
writer.Close();
}
private void SetFile()
{
int i;
i = path.LastIndexOf('\\');
if (i > 0) file = path.Substring(i + 1);
else file = path;
}
// RichTextBox ← 編集データ(str)
public void Load(RichTextBox textbox)
{
textbox.Clear();
textbox.Text = str;
}
// 編集データ(str) ← RichTextBox
public void Save(RichTextBox textbox)
{
str = string.Empty;
str = textbox.Text;
}
// 現在行を取得
public void LineEdit(RichTextBox txtbox)
{
cursor = txtbox.SelectionStart;
st = txtbox.GetFirstCharIndexOfCurrentLine();
line = txtbox.GetLineFromCharIndex(st);
if (line < (txtbox.Lines.Length-1))
{
nx = txtbox.GetFirstCharIndexFromLine(line + 1);
lstr = txtbox.Text.Substring(st, nx - st);
}
else
{
nx = st;
lstr = "*LineEdit EndLine";
}
for (lt=0; lt<lstr.Length; lt++)
{
if (lstr[lt] == ' ') continue;
if (lstr[lt] == '\t') continue;
break;
}
}
}
|
![]()
| グループ | メニュー | メソッド |
|---|---|---|
| File(&F) | UniOpen(&O) | UniOpen メソッド |
| JisOpen(&P) | JisOpen メソッド | |
| EucOpen(&Q) | EucOpen メソッド | |
| OverWrite(&W) | OverWrite メソッド | |
| Ut8Save(&8) | Ut8Save メソッド | |
| UniSave(&U) | UniSave メソッド | |
| JisSave(&J) | JisSave メソッド | |
| EucSave(&E) | EucSave メソッド | |
| FileClose(&C) | FileClose メソッド | |
| Exit(&X) | Exit メソッド | |
| Edit(&E) | Copy(&C) | Copy メソッド |
| Cut(&T) | Cut メソッド | |
| Paste(&P) | Paste メソッド | |
| Undo(&U) | Undo メソッド | |
| Redo(&R) | Redo メソッド | |
| Tool(&T) | KeySearch(&K) | KeySearch メソッド |
| Replace(&R) | Replace メソッド | |
| View(&V) | Exec(&E) | Exec メソッド |
| SetFont(&F) | SetFont メソッド | |
| TextCode(&T) | Ut8Load(&8) | Ut8Load メソッド |
| UniLoad(&U) | UniLoad メソッド | |
| JisLoad(&J) | JisLoad メソッド | |
| EucLoad(&E) | EucLoad メソッド | |
| Help(&H) | Version(&A) | HelpAbout メソッド |
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text; // for Encoding
using System.IO; // for File, StreamReader
public class Form1 : Form
{
FileClass m_fclass; // 編集中の File Class
bool m_flag; // Text 更新フラグ
string m_szDir = @"C:\tmp"; // 検索フォルダの初期値
|
// Open Menu
private void UniOpen(object sender, EventArgs e)
{
OpenFile(string.Empty);
}
private void JisOpen(object sender, EventArgs e)
{
OpenFile("shift_jis");
}
private void EucOpen(object sender, EventArgs e)
{
OpenFile("euc-jp");
}
private void OpenFile(string encode)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "ファイルを選択してください";
openFileDialog1.Filter = "TEXT File|*.txt;*.htm;*.html;*.cs;*.cpp|ALL File|*.*";
openFileDialog1.RestoreDirectory = false; //true;
openFileDialog1.ShowReadOnly = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.InitialDirectory = m_szDir;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
LoadFile(openFileDialog1.FileName, encode);
}
openFileDialog1.Dispose();
}
private void LoadFile(string tpath, string tcode)
{
if (!File.Exists(tpath))
{
Console.WriteLine("File NotFound: " + tpath);
return;
}
m_szDir = tpath;
this.Text = m_szDir;
m_fclass = new FileClass(tpath,tcode);
m_fclass.ReadFile();
m_fclass.Load(richTextBox1);
tabControl1.GetControl(0).Text = m_fclass.file;
tabControl1.SelectTab(0);
}
|
private void OverWrite(object sender, EventArgs e)
{
if (m_fclass != null)
{
m_fclass.WriteFile(richTextBox1);
m_fclass.Save(richTextBox1);
}
}
private void Ut8Save(object sender, EventArgs e)
{
SaveFile("utf-8");
}
private void UniSave(object sender, EventArgs e)
{
SaveFile("utf-16");
}
private void JisSave(object sender, EventArgs e)
{
SaveFile("shift_jis");
}
private void EucSave(object sender, EventArgs e)
{
SaveFile("euc-jp");
}
private void SaveFile(string encode)
{
if (m_fclass == null) return;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "保存するファイルを選択してください";
saveFileDialog1.Filter = "TEXT File|*.txt;*.log|ALL File|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
m_fclass.WriteFile(richTextBox1, saveFileDialog1.FileName, encode);
tabControl1.GetControl(0).Text = m_fclass.file;
m_fclass.Save(richTextBox1);
}
saveFileDialog1.Dispose();
m_flag = false;
}
|
private void FileClose(object sender, EventArgs e)
{
if (Cancel_Check()) return;
m_fclass.file = string.Empty;
m_fclass.str = string.Empty;
richTextBox1.Text = string.Empty;
}
private bool Cancel_Check()
{
DialogResult rc;
if (m_flag)
{
rc = MessageBox.Show("保存しないで実行しますか", "選択",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (rc == DialogResult.No) return true;
}
m_flag = false;
return false;
}
|
![]()
| FFFE |
![]()
[Next Chapter ↓] Contex Menu
[Previous Chapter ↑] Text Editor の開発