FileClass

FileClass を MultiEditor に対応して再編集します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

FileClass

  1. FileClass を MultiEditor に対応して再編集します。
    複数ファイルを扱うには、今まで1個ですんだ物が複数必要になります。
    また class Form1 も大きくなり、ファイルの切り替えによるトラベルも発生します。
    そこで RitchTextBox 関係のメソッドを FileClass に集約して整理します。
  2. FileClass の領域定義です。
    TEXT 更新フラグもクラスの中で定義します。
    RichTextBox のハンドルを定義して、関係するメソッドを Form1 から移動します。
    WordSelect() の移動に伴い sepa の定義も移動します。
    ファイル入出力 の FileClass と比べてみて下さい。
        public class FileClass
        {   public string   path;       //ファイルパス
            public string   file;       //ファイル名
            public string   code;       //TEXTコード
            public string   str;        //編集 TEXT
            public bool     flag;       //更新フラグ
    
            public string   lstr;       //現在行の文字列
            public int      cursor;     //現在のカーソル位置
            public int      line;       //現在の行番号
            public int      st, nx;     //現在行と次の行の先頭Index
            public int      lt;         //次の行の有効文字
    
            static StreamReader   reader;
            static StreamWriter   writer;
            static string   sepa = "\n\\\t ,.;:<>(){}[]'\"+-*/=。、";
            public static RichTextBox    textbox;
    
  3. Constructor では TEXT 表示するカーソル位置を追加します。
    特に必要なければ、ゼロを指定して下さい。
            // Constructor
            public FileClass(string tpath, string tcode, int cur)
            {
                path = tpath;
                code = tcode;
                cursor = cur;
                str = string.Empty;
                flag = false;
                SetFile();
            }
    
  4. FileClass に移動した static メソッドです。
    static 領域の参照やメソッドの呼び出しは FileClass で修飾します。
    詳細は static メソッドを定義 Static を参照して下さい。
        // Word を選択状態に設定  pos:Word選択位置
        static public int WordSelect(int pos, Color cor)
        {
            ・・・
        }
    
        // RichTextBox の表示開始行を設定
        static public void SetLine(int cur)
        {   int pos, wk;
    
            pos = cur;
            if (pos<0)  pos = 0;
            wk = textbox.GetLineFromCharIndex(pos);
            wk = textbox.GetFirstCharIndexFromLine(wk);
            textbox.Select(wk,0);
            textbox.Focus();
            textbox.ScrollToCaret();
        }
    
  5. FileClass に移動した RichTextBox 関係のメソッドです。
        public void Next_srh(string word)
        {
            int wp;
            if (word == string.Empty) return;
            wp = textbox.SelectionStart + word.Length;
            wp = textbox.Find(word, wp, -1, RichTextBoxFinds.None);
            if (wp < 0)
            {
                MessageBox.Show("終端に達しました");
                return;
            }
            SetCurPos(wp);
            textbox.Select(wp, word.Length);
            textbox.SelectionColor = Color.DarkMagenta;
            textbox.Select(wp, 0);     //選択状態の解除
        }
        public void Back_srh(string word)
        {
            int wp;
            if (word == string.Empty) return;
            wp = textbox.SelectionStart;
            wp = textbox.Find(word, 0, wp, RichTextBoxFinds.Reverse);
            if (wp < 0)
            {
                MessageBox.Show("終端に達しました");
                return;
            }
            SetCurPos(wp);
            textbox.Select(wp, word.Length);
            textbox.SelectionColor = Color.DarkMagenta;
            textbox.Select(wp, 0);     //選択状態の解除
        }
    
        // cur の3行前から表示
        public void SetCurPos(int cur)
        {   int pos, wk;
    
            if (cur>=0) cursor = cur;
            pos = cursor;
            wk = textbox.GetLineFromCharIndex(pos) - 3;
            if (wk < 0) wk = 0;
            wk = textbox.GetFirstCharIndexFromLine(wk);
            textbox.Select(wk,0);
            textbox.Focus();
            textbox.ScrollToCaret();
        }
    
        // 現在行を取得
        public void LineEdit()
        {
            cursor = textbox.SelectionStart;
            st = textbox.GetFirstCharIndexOfCurrentLine();
            line = textbox.GetLineFromCharIndex(st);
            if (line < (textbox.Lines.Length-1))
            {
                nx = textbox.GetFirstCharIndexFromLine(line + 1);
                lstr = textbox.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;
            }
        }
    
  6. RichTextBox のハンドルを直接 FileClass で定義したので、Form の Constructor から設定します。
        public static RichTextBox    textbox;
    
        FileClass.textbox = richTextBox1;
    
    また textbox を使うのでメソッドの引数で渡される RichTextBox は削除して下さい。
        // Text File に保存
        public void WriteFile()
        {
            TextWrite(path, code);
        }
        public void WriteFile(string tpath, string tcode)
        {
            TextWrite(tpath, tcode);
            path = tpath;
            code = tcode;
            SetFile();
        }
        // RichTextBox のデータをファイル(tpath)に出力
        private void TextWrite(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();
        }
            ・・・
    
  7. これで class Form1 から幾つかのメソッドが無くなり、すっきりします。
    その分 class FileClass のメソッドが増えるのですが。 (^_^;)
    FileClass に RitchTextBox 関係のメソッドを集約することは必ずしも必要ではありません。
    面倒な方は、従来のまま続けていただいても差支えありません。
  8. Constructor にカーソル位置を追加した関係で LoadFile() にも int cur を追加します。
        // Constructor
        public FileClass(string tpath, string tcode, int cur)
        {
            ・・・
        }
    
        private void LoadFile(string tpath, string tcode, int cur)
        {
            ・・・
            m_fclass = new FileClass(tpath, tcode, cur);
    

【NOTE】

TabPage で切り替えると、何故かコピー&ペーストがうまく出来ません。 (^_^;)
RichTextBox のメソッドは Single Thread Apartment/Multi Thread Apartment に関係するらしく注意が必要です。
ClipPaste() を次のように書き換えて下さい。
    private void ClipPaste()
    {
        IDataObject data = Clipboard.GetDataObject();
        if (data != null && data.GetDataPresent(DataFormats.Text) == true)
        {
            int pos = richTextBox1.SelectionStart;
            richTextBox1.Select(pos, 0);
            richTextBox1.SelectedText = Clipboard.GetText();
        }
        m_fclass.flag = true;
    }

[Next Chapter ↓] Multi File Editor
[Previous Chapter ↑] TabPage Chenge

超初心者のプログラム入門(C# Frame Work)