/*★ メニュー選択で、ファイルから入力する    前田 稔 ★*/
//   RichTextBox に ContexMenu を設定しました
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
public class MyForm : Form
{
    private RichTextBox richTextBox1;
    private string TxtFile = "";
    private string m_str = "RichTextBox Test";
    private ContextMenuStrip contextMenuStrip1;
    private ToolStripMenuItem CopyCToolStripMenuItem;
    private ToolStripMenuItem CutTToolStripMenuItem;
    private ToolStripMenuItem PastePToolStripMenuItem;
    private System.ComponentModel.IContainer components;
    public MyForm()
    {   Resize += new System.EventHandler(FormResize);
        InitializeComponent();
    }
    
    private void InitializeComponent()
    {   // メインメニューを生成
        MainMenu menu = new MainMenu();
        // ファイルメニューを生成
        MenuItem item = menu.MenuItems.Add("ファイル(&F)");
        item.MenuItems.Add(new MenuItem("開く(&O)...", new EventHandler(this.FileOpen)));
        item.MenuItems.Add(new MenuItem("保存(&S)...", new EventHandler(this.FileSave)));
        item.MenuItems.Add("-");
        item.MenuItems.Add(new MenuItem("終了(&X)", new EventHandler(this.Exit), Shortcut.CtrlQ));
        // 編集メニューを生成
        item = menu.MenuItems.Add("編集(&E)");
        item.MenuItems.Add(new MenuItem("Copy(&C)...", new EventHandler(this.Copy)));
        item.MenuItems.Add(new MenuItem("Cut(&T)...", new EventHandler(this.Cut)));
        item.MenuItems.Add(new MenuItem("Paste(&P)...", new EventHandler(this.Paste)));
        item.MenuItems.Add(new MenuItem("Undo(&U)...", new EventHandler(this.Undo)));
        item.MenuItems.Add(new MenuItem("Redo(&R)...", new EventHandler(this.Redo)));
        this.components = new System.ComponentModel.Container();
        this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.CopyCToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.CutTToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.PastePToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.contextMenuStrip1.SuspendLayout();
        this.SuspendLayout();
        // フォームのメニューとして設定
        this.Menu = menu;
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.SuspendLayout();
        // richTextBox1
        this.richTextBox1.ContextMenuStrip = contextMenuStrip1;
        this.richTextBox1.Location = new System.Drawing.Point(12, 12);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(1000, 480);
        this.richTextBox1.TabIndex = 0;
        this.richTextBox1.Text = m_str;
        // contextMenuStrip1
        this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
        {   this.CopyCToolStripMenuItem,
            this.CutTToolStripMenuItem,
            this.PastePToolStripMenuItem
        });
        this.contextMenuStrip1.Name = "contextMenuStrip1";
        this.contextMenuStrip1.Size = new System.Drawing.Size(210, 82);
        // CopyCToolStripMenuItem
        this.CopyCToolStripMenuItem.Name = "CopyCToolStripMenuItem";
        this.CopyCToolStripMenuItem.Size = new System.Drawing.Size(209, 28);
        this.CopyCToolStripMenuItem.Text = "Copy(&C)";
        this.CopyCToolStripMenuItem.Click += new System.EventHandler(this.Copy);
        // CutTToolStripMenuItem
        this.CutTToolStripMenuItem.Name = "CutTToolStripMenuItem";
        this.CutTToolStripMenuItem.Size = new System.Drawing.Size(209, 28);
        this.CutTToolStripMenuItem.Text = "Cut(&T)";
        this.CutTToolStripMenuItem.Click += new System.EventHandler(this.Cut);
        // CutTToolStripMenuItem
        this.PastePToolStripMenuItem.Name = "PastePToolStripMenuItem";
        this.PastePToolStripMenuItem.Size = new System.Drawing.Size(209, 28);
        this.PastePToolStripMenuItem.Text = "Paste(&P)";
        this.PastePToolStripMenuItem.Click += new System.EventHandler(this.Paste);
        // MyForm
        this.ClientSize = new System.Drawing.Size(1020, 500);
        this.ContextMenuStrip = this.contextMenuStrip1;
        this.contextMenuStrip1.ResumeLayout(false);
        this.Controls.Add(this.richTextBox1);
        this.Name = "MyForm";
        this.ResumeLayout(false);
    }
    private void FormResize(object sender, EventArgs e)
    {   richTextBox1.Width = this.Width - 30;
        richTextBox1.Height = this.Height - 100;
    }
    // 終了メニューのイベントハンドラ
    private void Exit(object sender, EventArgs e)
    {   this.Close();  }
    // ファイル-開くメニューのイベントハンドラ
    private void FileOpen(object sender, EventArgs e)
    {   OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "Text ファイル|*.txt;*.cs;*.htm|すべてのファイル (*.*)|*.*" ;
        if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
        {   TxtFile = openFileDialog1.FileName;
            StreamReader reader = new StreamReader(TxtFile);
            m_str = reader.ReadToEnd();
            reader.Close();
            richTextBox1.Text = m_str;   
            Invalidate();
        }
    }
    // ファイル-保存メニューのイベントハンドラ
    private void FileSave(object sender, EventArgs e)
    {   SaveFileDialog saveDialog = new SaveFileDialog();
        if (saveDialog.ShowDialog(this) == DialogResult.OK)
        {   TxtFile = saveDialog.FileName;
            StreamWriter writer = new StreamWriter(TxtFile, false);	//false=上書き
            m_str = richTextBox1.Text;
            writer.WriteLine(m_str);
            writer.Close();
        }
    }
    //★ 編集メソッド
    private void Copy(object sender, EventArgs e)
    {   if (richTextBox1.SelectionLength > 0)
        { richTextBox1.Copy(); }
    }
    private void Cut(object sender, EventArgs e)
    {   if (richTextBox1.SelectionLength > 0)
        { richTextBox1.Cut(); }
    }
    private void Paste(object sender, EventArgs e)
    {   ClipPaste();  }
    private void Undo(object sender, EventArgs e)
    {   if (richTextBox1.CanUndo)
        { richTextBox1.Undo(); }
    }
    private void Redo(object sender, EventArgs e)
    {   if (richTextBox1.CanRedo)
        { richTextBox1.Redo(); }
    }
    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();
        }
    }
}
class form01
{   [STAThread]
    public static void Main()
    {   MyForm mf = new MyForm();
        Application.Run(mf);
    }
}
 |