LoadFile SaveFile : RichTextBox « GUI Windows Forms « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


public class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void MenuItemShowHelpMenu_Click(object sender, EventArgs e) {
        ToolStripMenuItem item = (ToolStripMenuItem)sender;
        MenuItemHelp.Visible = item.Checked;
    }

    private void MenuItemNew_Click(object sender, EventArgs e) {
        richTextBoxText.Text = "";
    }

    private void MenuItemOpen_Click(object sender, EventArgs e) {
        richTextBoxText.LoadFile(@".\Example.rtf");
    }

    private void MenuItemSave_Click(object sender, EventArgs e) {
        richTextBoxText.SaveFile(@".\Example.rtf");
    }
    private void InitializeComponent() {
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.MenuItemNew = new System.Windows.Forms.ToolStripMenuItem();
        this.MenuItemOpen = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
        this.MenuItemSave = new System.Windows.Forms.ToolStripMenuItem();
        this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
        this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripSeparator();
        this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.formatToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.MenuItemShowHelpMenu = new System.Windows.Forms.ToolStripMenuItem();
        this.MenuItemHelp = new System.Windows.Forms.ToolStripMenuItem();
        this.contentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.indexToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.searchToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripSeparator();
        this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.richTextBoxText = new System.Windows.Forms.RichTextBox();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();

        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.formatToolStripMenuItem,
            this.MenuItemHelp});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Name = "menuStrip1";
        this.menuStrip1.Size = new System.Drawing.Size(602, 24);
        this.menuStrip1.TabIndex = 0;
        this.menuStrip1.Text = "menuStrip1";

        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.MenuItemNew,
            this.MenuItemOpen,
            this.toolStripMenuItem1,
            this.MenuItemSave,
            this.saveAsToolStripMenuItem,
            this.toolStripSeparator1,
            this.printToolStripMenuItem,
            this.toolStripMenuItem4,
            this.toolStripMenuItem2,
            this.exitToolStripMenuItem});
        this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
        this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
        this.fileToolStripMenuItem.Text = "&File";

        this.MenuItemNew.Name = "MenuItemNew";
        this.MenuItemNew.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
        this.MenuItemNew.Size = new System.Drawing.Size(151, 22);
        this.MenuItemNew.Text = "&New";
        this.MenuItemNew.Click += new System.EventHandler(this.MenuItemNew_Click);

        this.MenuItemOpen.Name = "MenuItemOpen";
        this.MenuItemOpen.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
        this.MenuItemOpen.Size = new System.Drawing.Size(151, 22);
        this.MenuItemOpen.Text = "&Open";
        this.MenuItemOpen.Click += new System.EventHandler(this.MenuItemOpen_Click);

        this.toolStripMenuItem1.Name = "toolStripMenuItem1";
        this.toolStripMenuItem1.Size = new System.Drawing.Size(148, 6);

        this.MenuItemSave.Name = "MenuItemSave";
        this.MenuItemSave.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
        this.MenuItemSave.Size = new System.Drawing.Size(151, 22);
        this.MenuItemSave.Text = "&Save";
        this.MenuItemSave.Click += new System.EventHandler(this.MenuItemSave_Click);

        this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem";
        this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
        this.saveAsToolStripMenuItem.Text = "Save &As";

        this.toolStripSeparator1.Name = "toolStripSeparator1";
        this.toolStripSeparator1.Size = new System.Drawing.Size(148, 6);

        this.printToolStripMenuItem.Name = "printToolStripMenuItem";
        this.printToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.P)));
        this.printToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
        this.printToolStripMenuItem.Text = "&Print";

        this.toolStripMenuItem4.Name = "toolStripMenuItem4";
        this.toolStripMenuItem4.Size = new System.Drawing.Size(151, 22);
        this.toolStripMenuItem4.Text = "Print Preview";

        this.toolStripMenuItem2.Name = "toolStripMenuItem2";
        this.toolStripMenuItem2.Size = new System.Drawing.Size(148, 6);

        this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
        this.exitToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
        this.exitToolStripMenuItem.Text = "E&xit";

        this.formatToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.MenuItemShowHelpMenu});
        this.formatToolStripMenuItem.Name = "formatToolStripMenuItem";
        this.formatToolStripMenuItem.Size = new System.Drawing.Size(53, 20);
        this.formatToolStripMenuItem.Text = "Format";

        this.MenuItemShowHelpMenu.Checked = true;
        this.MenuItemShowHelpMenu.CheckOnClick = true;
        this.MenuItemShowHelpMenu.CheckState = System.Windows.Forms.CheckState.Checked;
        this.MenuItemShowHelpMenu.Name = "MenuItemShowHelpMenu";
        this.MenuItemShowHelpMenu.Size = new System.Drawing.Size(164, 22);
        this.MenuItemShowHelpMenu.Text = "Show Help Menu";
        this.MenuItemShowHelpMenu.Click += new System.EventHandler(this.MenuItemShowHelpMenu_Click);

        this.MenuItemHelp.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.contentsToolStripMenuItem,
            this.indexToolStripMenuItem,
            this.searchToolStripMenuItem,
            this.toolStripMenuItem3,
            this.aboutToolStripMenuItem});
        this.MenuItemHelp.Name = "MenuItemHelp";
        this.MenuItemHelp.Size = new System.Drawing.Size(40, 20);
        this.MenuItemHelp.Text = "&Help";

        this.contentsToolStripMenuItem.Name = "contentsToolStripMenuItem";
        this.contentsToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
        this.contentsToolStripMenuItem.Text = "Contents";

        this.indexToolStripMenuItem.Name = "indexToolStripMenuItem";
        this.indexToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
        this.indexToolStripMenuItem.Text = "Index";

        this.searchToolStripMenuItem.Name = "searchToolStripMenuItem";
        this.searchToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
        this.searchToolStripMenuItem.Text = "Search";

        this.toolStripMenuItem3.Name = "toolStripMenuItem3";
        this.toolStripMenuItem3.Size = new System.Drawing.Size(126, 6);

        this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
        this.aboutToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
        this.aboutToolStripMenuItem.Text = "About";

        this.richTextBoxText.Dock = System.Windows.Forms.DockStyle.Fill;
        this.richTextBoxText.Location = new System.Drawing.Point(0, 24);
        this.richTextBoxText.Name = "richTextBoxText";
        this.richTextBoxText.Size = new System.Drawing.Size(602, 242);
        this.richTextBoxText.TabIndex = 1;
        this.richTextBoxText.Text = "";

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(602, 266);
        this.Controls.Add(this.richTextBoxText);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.menuStrip1.ResumeLayout(false);
        this.menuStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem MenuItemNew;
    private System.Windows.Forms.ToolStripMenuItem MenuItemOpen;
    private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
    private System.Windows.Forms.ToolStripMenuItem MenuItemSave;
    private System.Windows.Forms.ToolStripMenuItem saveAsToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem printToolStripMenuItem;
    private System.Windows.Forms.ToolStripSeparator toolStripMenuItem2;
    private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem MenuItemHelp;
    private System.Windows.Forms.ToolStripMenuItem contentsToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem indexToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem searchToolStripMenuItem;
    private System.Windows.Forms.ToolStripSeparator toolStripMenuItem3;
    private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
    private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem4;
    private System.Windows.Forms.ToolStripMenuItem formatToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem MenuItemShowHelpMenu;
    private System.Windows.Forms.RichTextBox richTextBoxText;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

}








23.20.RichTextBox
23.20.1.LoadFile SaveFile
23.20.2.RichTextBox: font
23.20.3.Editor based on RichTextBoxEditor based on RichTextBox
23.20.4.Build Menu for a RichTextBox editorBuild Menu for a RichTextBox editor
23.20.5.RichTextBox based Text Editor