Csharp/CSharp Tutorial/GUI Windows Forms/RichTextBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:15, 26 мая 2010
Содержание
Build Menu for a RichTextBox editor
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
public class RichTextBoxes : Form
{
RichTextBox rtxt;
public RichTextBoxes()
{
Size = new Size(400, 500);
rtxt = new RichTextBox();
rtxt.Parent = this;
rtxt.Text = "Enter text here.";
rtxt.Multiline = true;
rtxt.BorderStyle = BorderStyle.Fixed3D;
rtxt.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
rtxt.Dock = DockStyle.Fill;
rtxt.DetectUrls = true;
rtxt.AutoWordSelection = true;
rtxt.BulletIndent = 10;
rtxt.ShowSelectionMargin = true;
MenuItem mnuImport = new MenuItem("&Import",new EventHandler(mnuImport_Click));
MenuItem mnuFile = new MenuItem("&File",new MenuItem[] {mnuImport});
MenuItem mnuDash1 = new MenuItem("-");
MenuItem mnuDash2 = new MenuItem("-");
MenuItem mnuUndo = new MenuItem("&Undo",new EventHandler(mnuUndo_Click),Shortcut.CtrlZ);
MenuItem mnuCut = new MenuItem("Cu&t", new EventHandler(mnuCut_Click), Shortcut.CtrlX);
MenuItem mnuCopy = new MenuItem("&Copy",new EventHandler(mnuCopy_Click),Shortcut.CtrlC);
MenuItem mnuCopyRtf = new MenuItem("Copy &Rtf", new EventHandler(mnuCopyRtf_Click));
MenuItem mnuPaste = new MenuItem("&Paste",new EventHandler(mnuPaste_Click),Shortcut.CtrlV);
MenuItem mnuDelete = new MenuItem("&Delete",new EventHandler(mnuDelete_Click));
MenuItem mnuSelectAll = new MenuItem("Select &All", new EventHandler(mnuSelectAll_Click),Shortcut.CtrlA);
MenuItem mnuSelect5 = new MenuItem("Select First &5",new EventHandler(mnuSelect5_Click),Shortcut.Ctrl5);
MenuItem mnuClear = new MenuItem("Clea&r",new EventHandler(mnuClear_Click));
MenuItem mnuEdit = new MenuItem("&Edit",new MenuItem[] {mnuUndo, mnuDash1,
mnuCut, mnuCopy, mnuCopyRtf, mnuPaste,
mnuDelete, mnuDash2, mnuSelectAll,
mnuSelect5, mnuClear});
MenuItem mnuScrollToCaret = new MenuItem("&Scroll to Caret",new EventHandler(mnuScrollToCaret_Click));
MenuItem mnuView = new MenuItem("&View",new MenuItem[] {mnuScrollToCaret});
MenuItem mnuAlignLeft = new MenuItem("Align&Left",new EventHandler(mnuAlignLeft_Click));
MenuItem mnuAlignRight = new MenuItem("Align&Right",new EventHandler(mnuAlignRight_Click));
MenuItem mnuAlignCenter = new MenuItem("Align&Center",new EventHandler(mnuAlignCenter_Click));
MenuItem mnuBullet = new MenuItem("&Bullet",new EventHandler(mnuBullet_Click));
MenuItem mnuAlign = new MenuItem("&Align",new MenuItem[] {mnuAlignLeft, mnuAlignRight, mnuAlignCenter});
MenuItem mnuRed = new MenuItem("&Red",new EventHandler(mnuRed_Click));
MenuItem mnuBold = new MenuItem("Bo&ld",new EventHandler(mnuBold_Click));
MenuItem mnuHang = new MenuItem("&Hanging Indent",new EventHandler(mnuHang_Click));
MenuItem mnuIndent = new MenuItem("&Indent",new EventHandler(mnuIndent_Click));
MenuItem mnuRightIndent = new MenuItem("&Right Indent", new EventHandler(mnuRightIndent_Click));
MenuItem mnuFormat = new MenuItem("For&mat",new MenuItem[] {mnuBullet, mnuAlign,
mnuRed, mnuBold, mnuHang, mnuIndent,
mnuRightIndent});
Menu = new MainMenu(new MenuItem[] {mnuFile, mnuEdit,
mnuView, mnuFormat});
}
static void Main()
{
Application.Run(new RichTextBoxes());
}
private void mnuImport_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"c:\";
ofd.Filter = "RTF files (*.rtf)|*.rtf|" +
"All files (*.*)|*.*";
ofd.FilterIndex = 1; // 1 based index
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
StreamReader reader = new StreamReader(ofd.FileName);
rtxt.Rtf = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
private void mnuUndo_Click(object sender, EventArgs e)
{
if (rtxt.CanUndo == true)
{
rtxt.Undo();
rtxt.ClearUndo();
}
}
private void mnuCut_Click(object sender, EventArgs e)
{
if (rtxt.SelectedText != "")
rtxt.Cut();
}
private void mnuCopy_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.Copy();
}
private void mnuCopyRtf_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
{
Clipboard.SetDataObject(rtxt.SelectedRtf);
}
}
private void mnuPaste_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
if (rtxt.CanUndo == true)
{
if (rtxt.SelectionLength > 0)
{
if (MessageBox.Show(
"Do you want to overwrite the currently selected text?",
"Cut & Paste", MessageBoxButtons.YesNo) == DialogResult.No)
rtxt.SelectionStart = rtxt.SelectionStart +
rtxt.SelectionLength;
}
rtxt.Paste();
}
}
}
private void mnuDelete_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.SelectedText = "";
}
private void mnuClear_Click(object sender, EventArgs e)
{
rtxt.Clear();
}
private void mnuSelect5_Click(object sender, EventArgs e)
{
if (rtxt.Text.Length >= 5)
{
rtxt.Select(0,5);
}
else
{
rtxt.Select(0,rtxt.Text.Length);
}
}
private void mnuSelectAll_Click(object sender, EventArgs e)
{
rtxt.SelectAll();
}
private void mnuScrollToCaret_Click(object sender, EventArgs e)
{
rtxt.ScrollToCaret();
}
private void mnuBullet_Click(object sender, EventArgs e)
{
rtxt.SelectionBullet = !rtxt.SelectionBullet;
}
private void mnuAlignLeft_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Left;
}
private void mnuAlignRight_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Right;
}
private void mnuAlignCenter_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Center;
}
private void mnuRed_Click(object sender, EventArgs e)
{
if (rtxt.SelectionColor == Color.Red)
rtxt.SelectionColor = Color.Black;
else
rtxt.SelectionColor = Color.Red;
}
private void mnuBold_Click(object sender, EventArgs e)
{
if (rtxt.SelectionFont.Bold )
rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Regular);
else
rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Bold);
}
private void mnuHang_Click(object sender, EventArgs e)
{
if (rtxt.SelectionHangingIndent == 10 )
rtxt.SelectionHangingIndent = 0;
else
rtxt.SelectionHangingIndent = 10;
}
private void mnuIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionIndent == 10 )
rtxt.SelectionIndent = 0;
else
rtxt.SelectionIndent = 10;
}
private void mnuRightIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionRightIndent == 50 )
rtxt.SelectionRightIndent = 0;
else
rtxt.SelectionRightIndent = 50;
}
}
Editor based on RichTextBox
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
public class RichTextBoxes : Form
{
RichTextBox rtxt;
public RichTextBoxes()
{
Size = new Size(400, 500);
rtxt = new RichTextBox();
rtxt.Parent = this;
rtxt.Text = "Enter text here.";
rtxt.Multiline = true;
rtxt.BorderStyle = BorderStyle.Fixed3D;
rtxt.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
rtxt.Dock = DockStyle.Fill;
rtxt.DetectUrls = true;
rtxt.AutoWordSelection = true;
rtxt.BulletIndent = 10;
rtxt.ShowSelectionMargin = true;
MenuItem mnuImport = new MenuItem("&Import",new EventHandler(mnuImport_Click));
MenuItem mnuFile = new MenuItem("&File",new MenuItem[] {mnuImport});
MenuItem mnuDash1 = new MenuItem("-");
MenuItem mnuDash2 = new MenuItem("-");
MenuItem mnuUndo = new MenuItem("&Undo",new EventHandler(mnuUndo_Click),Shortcut.CtrlZ);
MenuItem mnuCut = new MenuItem("Cu&t", new EventHandler(mnuCut_Click), Shortcut.CtrlX);
MenuItem mnuCopy = new MenuItem("&Copy",new EventHandler(mnuCopy_Click),Shortcut.CtrlC);
MenuItem mnuCopyRtf = new MenuItem("Copy &Rtf", new EventHandler(mnuCopyRtf_Click));
MenuItem mnuPaste = new MenuItem("&Paste",new EventHandler(mnuPaste_Click),Shortcut.CtrlV);
MenuItem mnuDelete = new MenuItem("&Delete",new EventHandler(mnuDelete_Click));
MenuItem mnuSelectAll = new MenuItem("Select &All", new EventHandler(mnuSelectAll_Click),Shortcut.CtrlA);
MenuItem mnuSelect5 = new MenuItem("Select First &5",new EventHandler(mnuSelect5_Click),Shortcut.Ctrl5);
MenuItem mnuClear = new MenuItem("Clea&r",new EventHandler(mnuClear_Click));
MenuItem mnuEdit = new MenuItem("&Edit",new MenuItem[] {mnuUndo, mnuDash1,
mnuCut, mnuCopy, mnuCopyRtf, mnuPaste,
mnuDelete, mnuDash2, mnuSelectAll,
mnuSelect5, mnuClear});
MenuItem mnuScrollToCaret = new MenuItem("&Scroll to Caret",new EventHandler(mnuScrollToCaret_Click));
MenuItem mnuView = new MenuItem("&View",new MenuItem[] {mnuScrollToCaret});
MenuItem mnuAlignLeft = new MenuItem("Align&Left",new EventHandler(mnuAlignLeft_Click));
MenuItem mnuAlignRight = new MenuItem("Align&Right",new EventHandler(mnuAlignRight_Click));
MenuItem mnuAlignCenter = new MenuItem("Align&Center",new EventHandler(mnuAlignCenter_Click));
MenuItem mnuBullet = new MenuItem("&Bullet",new EventHandler(mnuBullet_Click));
MenuItem mnuAlign = new MenuItem("&Align",new MenuItem[] {mnuAlignLeft, mnuAlignRight, mnuAlignCenter});
MenuItem mnuRed = new MenuItem("&Red",new EventHandler(mnuRed_Click));
MenuItem mnuBold = new MenuItem("Bo&ld",new EventHandler(mnuBold_Click));
MenuItem mnuHang = new MenuItem("&Hanging Indent",new EventHandler(mnuHang_Click));
MenuItem mnuIndent = new MenuItem("&Indent",new EventHandler(mnuIndent_Click));
MenuItem mnuRightIndent = new MenuItem("&Right Indent", new EventHandler(mnuRightIndent_Click));
MenuItem mnuFormat = new MenuItem("For&mat",new MenuItem[] {mnuBullet, mnuAlign,
mnuRed, mnuBold, mnuHang, mnuIndent,
mnuRightIndent});
Menu = new MainMenu(new MenuItem[] {mnuFile, mnuEdit,
mnuView, mnuFormat});
}
static void Main()
{
Application.Run(new RichTextBoxes());
}
private void mnuImport_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"c:\";
ofd.Filter = "RTF files (*.rtf)|*.rtf|" +
"All files (*.*)|*.*";
ofd.FilterIndex = 1; // 1 based index
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
StreamReader reader = new StreamReader(ofd.FileName);
rtxt.Rtf = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
private void mnuUndo_Click(object sender, EventArgs e)
{
if (rtxt.CanUndo == true)
{
rtxt.Undo();
rtxt.ClearUndo();
}
}
private void mnuCut_Click(object sender, EventArgs e)
{
if (rtxt.SelectedText != "")
rtxt.Cut();
}
private void mnuCopy_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.Copy();
}
private void mnuCopyRtf_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
{
Clipboard.SetDataObject(rtxt.SelectedRtf);
}
}
private void mnuPaste_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
if (rtxt.CanUndo == true)
{
if (rtxt.SelectionLength > 0)
{
if (MessageBox.Show(
"Do you want to overwrite the currently selected text?",
"Cut & Paste", MessageBoxButtons.YesNo) == DialogResult.No)
rtxt.SelectionStart = rtxt.SelectionStart +
rtxt.SelectionLength;
}
rtxt.Paste();
}
}
}
private void mnuDelete_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.SelectedText = "";
}
private void mnuClear_Click(object sender, EventArgs e)
{
rtxt.Clear();
}
private void mnuSelect5_Click(object sender, EventArgs e)
{
if (rtxt.Text.Length >= 5)
{
rtxt.Select(0,5);
}
else
{
rtxt.Select(0,rtxt.Text.Length);
}
}
private void mnuSelectAll_Click(object sender, EventArgs e)
{
rtxt.SelectAll();
}
private void mnuScrollToCaret_Click(object sender, EventArgs e)
{
rtxt.ScrollToCaret();
}
private void mnuBullet_Click(object sender, EventArgs e)
{
rtxt.SelectionBullet = !rtxt.SelectionBullet;
}
private void mnuAlignLeft_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Left;
}
private void mnuAlignRight_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Right;
}
private void mnuAlignCenter_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Center;
}
private void mnuRed_Click(object sender, EventArgs e)
{
if (rtxt.SelectionColor == Color.Red)
rtxt.SelectionColor = Color.Black;
else
rtxt.SelectionColor = Color.Red;
}
private void mnuBold_Click(object sender, EventArgs e)
{
if (rtxt.SelectionFont.Bold )
rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Regular);
else
rtxt.SelectionFont = new Font(rtxt.SelectionFont, FontStyle.Bold);
}
private void mnuHang_Click(object sender, EventArgs e)
{
if (rtxt.SelectionHangingIndent == 10 )
rtxt.SelectionHangingIndent = 0;
else
rtxt.SelectionHangingIndent = 10;
}
private void mnuIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionIndent == 10 )
rtxt.SelectionIndent = 0;
else
rtxt.SelectionIndent = 10;
}
private void mnuRightIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionRightIndent == 50 )
rtxt.SelectionRightIndent = 0;
else
rtxt.SelectionRightIndent = 50;
}
}
LoadFile SaveFile
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
}
}
RichTextBox based Text Editor
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.ToolStripComboBoxFonts.SelectedIndex = 0;
}
private void MenuItemShowHelpMenu_CheckedChanged(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
MenuItemHelp.Visible = item.Checked;
}
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 MenuItemNew_Click(object sender, EventArgs e)
{
richTextBoxText.Text = "";
}
private void ToolStripButtonBold_CheckedChanged(object sender, EventArgs e)
{
Font oldFont;
Font newFont;
bool checkState = ((ToolStripButton)sender).Checked;
oldFont = this.richTextBoxText.SelectionFont;
if (!checkState)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
this.ToolStripMenuItemBold.CheckedChanged -= new EventHandler(ToolStripMenuItemBold_CheckedChanged);
this.ToolStripMenuItemBold.Checked = checkState;
this.ToolStripMenuItemBold.CheckedChanged += new EventHandler(ToolStripMenuItemBold_CheckedChanged);
if (!checkState)
toolStripStatusLabelBold.Font = new Font(toolStripStatusLabelBold.Font, toolStripStatusLabelBold.Font.Style & ~FontStyle.Bold);
else
toolStripStatusLabelBold.Font = new Font(toolStripStatusLabelBold.Font, toolStripStatusLabelBold.Font.Style | FontStyle.Bold);
}
private void ToolStripButtonItalic_CheckedChanged(object sender, EventArgs e)
{
Font oldFont;
Font newFont;
bool checkState = ((ToolStripButton)sender).Checked;
oldFont = this.richTextBoxText.SelectionFont;
if (!checkState)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
this.ToolStripMenuItemItalic.CheckedChanged -= new EventHandler(ToolStripMenuItemItalic_CheckedChanged);
this.ToolStripMenuItemItalic.Checked = checkState;
this.ToolStripMenuItemItalic.CheckedChanged += new EventHandler(ToolStripMenuItemItalic_CheckedChanged);
if (!checkState)
toolStripStatusLabelItalic.Font = new Font(toolStripStatusLabelItalic.Font, toolStripStatusLabelItalic.Font.Style & ~FontStyle.Italic);
else
toolStripStatusLabelItalic.Font = new Font(toolStripStatusLabelItalic.Font, toolStripStatusLabelItalic.Font.Style | FontStyle.Italic);
}
private void ToolStripButtonUnderline_CheckedChanged(object sender, EventArgs e)
{
Font oldFont;
Font newFont;
bool checkState = ((ToolStripButton)sender).Checked;
oldFont = this.richTextBoxText.SelectionFont;
if (!checkState)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
this.ToolStripMenuItemUnderline.CheckedChanged -= new EventHandler(ToolStripMenuItemUnderline_CheckedChanged);
this.ToolStripMenuItemUnderline.Checked = checkState;
this.ToolStripMenuItemUnderline.CheckedChanged += new EventHandler(ToolStripMenuItemUnderline_CheckedChanged);
toolStripStatusLabelUnderline.Enabled = checkState;
if (!checkState)
toolStripStatusLabelUnderline.Font = new Font(toolStripStatusLabelUnderline.Font, toolStripStatusLabelUnderline.Font.Style & ~FontStyle.Underline);
else
toolStripStatusLabelUnderline.Font = new Font(toolStripStatusLabelUnderline.Font, toolStripStatusLabelUnderline.Font.Style | FontStyle.Underline);
}
private void ToolStripMenuItemBold_CheckedChanged(object sender, EventArgs e)
{
this.ToolStripButtonBold.Checked = ToolStripMenuItemBold.Checked;
}
private void ToolStripMenuItemItalic_CheckedChanged(object sender, EventArgs e)
{
this.ToolStripButtonItalic.Checked = ToolStripMenuItemItalic.Checked;
}
private void ToolStripMenuItemUnderline_CheckedChanged(object sender, EventArgs e)
{
this.ToolStripButtonUnderline.Checked = ToolStripMenuItemUnderline.Checked;
}
private void ToolStripComboBoxFonts_SelectedIndexChanged(object sender, EventArgs e)
{
string text = ((ToolStripComboBox)sender).SelectedItem.ToString();
Font newFont = null;
if (richTextBoxText.SelectionFont == null)
newFont = new Font(text, richTextBoxText.Font.Size);
else
newFont = new Font(text, richTextBoxText.SelectionFont.Size,
richTextBoxText.SelectionFont.Style);
richTextBoxText.SelectionFont = newFont;
}
private void richTextBoxText_TextChanged(object sender, EventArgs e)
{
toolStripStatusLabelText.Text = "Number of characters: " + richTextBoxText.Text.Length;
}
private void toolStripStatusLabelBold_Click(object sender, EventArgs e)
{
this.ToolStripButtonBold.Checked = !ToolStripMenuItemBold.Checked;
}
private void toolStripStatusLabelItalic_Click(object sender, EventArgs e)
{
this.ToolStripButtonItalic.Checked = !ToolStripMenuItemItalic.Checked;
}
private void toolStripStatusLabelUnderline_Click(object sender, EventArgs e)
{
this.ToolStripButtonUnderline.Checked = !ToolStripMenuItemUnderline.Checked;
}
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.toolStripMenuItem2 = new System.Windows.Forms.ToolStripSeparator();
this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.printPreviewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripSeparator();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.formatToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItemBold = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItemItalic = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItemUnderline = 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.toolStripMenuItem4 = new System.Windows.Forms.ToolStripSeparator();
this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.richTextBoxText = new System.Windows.Forms.RichTextBox();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.newToolStripButton = new System.Windows.Forms.ToolStripButton();
this.openToolStripButton = new System.Windows.Forms.ToolStripButton();
this.saveToolStripButton = new System.Windows.Forms.ToolStripButton();
this.printToolStripButton = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
this.ToolStripButtonBold = new System.Windows.Forms.ToolStripButton();
this.ToolStripButtonItalic = new System.Windows.Forms.ToolStripButton();
this.ToolStripButtonUnderline = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.ToolStripComboBoxFonts = new System.Windows.Forms.ToolStripComboBox();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.helpToolStripButton = new System.Windows.Forms.ToolStripButton();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelText = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelBold = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelItalic = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelUnderline = new System.Windows.Forms.ToolStripStatusLabel();
this.menuStrip1.SuspendLayout();
this.toolStrip1.SuspendLayout();
this.statusStrip1.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.Size = new System.Drawing.Size(454, 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.toolStripMenuItem2,
this.printToolStripMenuItem,
this.printPreviewToolStripMenuItem,
this.toolStripMenuItem3,
this.exitToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Text = "&File";
//
// MenuItemNew
//
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(146, 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(146, 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(143, 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(146, 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(146, 22);
this.saveAsToolStripMenuItem.Text = "Save &As";
//
// toolStripMenuItem2
//
this.toolStripMenuItem2.Name = "toolStripMenuItem2";
this.toolStripMenuItem2.Size = new System.Drawing.Size(143, 6);
//
// printToolStripMenuItem
//
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(146, 22);
this.printToolStripMenuItem.Text = "&Print";
//
// printPreviewToolStripMenuItem
//
this.printPreviewToolStripMenuItem.Name = "printPreviewToolStripMenuItem";
this.printPreviewToolStripMenuItem.Size = new System.Drawing.Size(146, 22);
this.printPreviewToolStripMenuItem.Text = "Print Preview";
//
// toolStripMenuItem3
//
this.toolStripMenuItem3.Name = "toolStripMenuItem3";
this.toolStripMenuItem3.Size = new System.Drawing.Size(143, 6);
//
// exitToolStripMenuItem
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(146, 22);
this.exitToolStripMenuItem.Text = "E&xit";
//
// formatToolStripMenuItem
//
this.formatToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ToolStripMenuItemBold,
this.ToolStripMenuItemItalic,
this.ToolStripMenuItemUnderline});
this.formatToolStripMenuItem.Name = "formatToolStripMenuItem";
this.formatToolStripMenuItem.Size = new System.Drawing.Size(57, 20);
this.formatToolStripMenuItem.Text = "Format";
//
// ToolStripMenuItemBold
//
this.ToolStripMenuItemBold.CheckOnClick = true;
this.ToolStripMenuItemBold.Name = "ToolStripMenuItemBold";
this.ToolStripMenuItemBold.Size = new System.Drawing.Size(125, 22);
this.ToolStripMenuItemBold.Text = "Bold";
this.ToolStripMenuItemBold.CheckedChanged += new System.EventHandler(this.ToolStripMenuItemBold_CheckedChanged);
//
// ToolStripMenuItemItalic
//
this.ToolStripMenuItemItalic.CheckOnClick = true;
this.ToolStripMenuItemItalic.Name = "ToolStripMenuItemItalic";
this.ToolStripMenuItemItalic.Size = new System.Drawing.Size(125, 22);
this.ToolStripMenuItemItalic.Text = "Italic";
this.ToolStripMenuItemItalic.CheckedChanged += new System.EventHandler(this.ToolStripMenuItemItalic_CheckedChanged);
//
// ToolStripMenuItemUnderline
//
this.ToolStripMenuItemUnderline.CheckOnClick = true;
this.ToolStripMenuItemUnderline.Name = "ToolStripMenuItemUnderline";
this.ToolStripMenuItemUnderline.Size = new System.Drawing.Size(125, 22);
this.ToolStripMenuItemUnderline.Text = "Underline";
this.ToolStripMenuItemUnderline.CheckedChanged += new System.EventHandler(this.ToolStripMenuItemUnderline_CheckedChanged);
//
// MenuItemHelp
//
this.MenuItemHelp.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.contentsToolStripMenuItem,
this.indexToolStripMenuItem,
this.searchToolStripMenuItem,
this.toolStripMenuItem4,
this.aboutToolStripMenuItem});
this.MenuItemHelp.Name = "MenuItemHelp";
this.MenuItemHelp.Size = new System.Drawing.Size(44, 20);
this.MenuItemHelp.Text = "&Help";
//
// contentsToolStripMenuItem
//
this.contentsToolStripMenuItem.Name = "contentsToolStripMenuItem";
this.contentsToolStripMenuItem.Size = new System.Drawing.Size(122, 22);
this.contentsToolStripMenuItem.Text = "Contents";
//
// indexToolStripMenuItem
//
this.indexToolStripMenuItem.Name = "indexToolStripMenuItem";
this.indexToolStripMenuItem.Size = new System.Drawing.Size(122, 22);
this.indexToolStripMenuItem.Text = "Index";
//
// searchToolStripMenuItem
//
this.searchToolStripMenuItem.Name = "searchToolStripMenuItem";
this.searchToolStripMenuItem.Size = new System.Drawing.Size(122, 22);
this.searchToolStripMenuItem.Text = "Search";
//
// toolStripMenuItem4
//
this.toolStripMenuItem4.Name = "toolStripMenuItem4";
this.toolStripMenuItem4.Size = new System.Drawing.Size(119, 6);
//
// aboutToolStripMenuItem
//
this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
this.aboutToolStripMenuItem.Size = new System.Drawing.Size(122, 22);
this.aboutToolStripMenuItem.Text = "About";
//
// richTextBoxText
//
this.richTextBoxText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBoxText.Location = new System.Drawing.Point(0, 52);
this.richTextBoxText.Name = "richTextBoxText";
this.richTextBoxText.Size = new System.Drawing.Size(454, 212);
this.richTextBoxText.TabIndex = 1;
this.richTextBoxText.Text = "";
this.richTextBoxText.TextChanged += new System.EventHandler(this.richTextBoxText_TextChanged);
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newToolStripButton,
this.openToolStripButton,
this.saveToolStripButton,
this.printToolStripButton,
this.toolStripSeparator,
this.ToolStripButtonBold,
this.ToolStripButtonItalic,
this.ToolStripButtonUnderline,
this.toolStripSeparator1,
this.ToolStripComboBoxFonts,
this.toolStripSeparator2,
this.helpToolStripButton});
this.toolStrip1.Location = new System.Drawing.Point(0, 24);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(454, 25);
this.toolStrip1.TabIndex = 2;
this.toolStrip1.Text = "toolStrip1";
//
// newToolStripButton
//
this.newToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.newToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.newToolStripButton.Name = "newToolStripButton";
this.newToolStripButton.Size = new System.Drawing.Size(23, 22);
this.newToolStripButton.Text = "&New";
this.newToolStripButton.Click += new System.EventHandler(this.MenuItemNew_Click);
//
// openToolStripButton
//
this.openToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.openToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.openToolStripButton.Name = "openToolStripButton";
this.openToolStripButton.Size = new System.Drawing.Size(23, 22);
this.openToolStripButton.Text = "&Open";
this.openToolStripButton.Click += new System.EventHandler(this.MenuItemOpen_Click);
//
// saveToolStripButton
//
this.saveToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.saveToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.saveToolStripButton.Name = "saveToolStripButton";
this.saveToolStripButton.Size = new System.Drawing.Size(23, 22);
this.saveToolStripButton.Text = "&Save";
this.saveToolStripButton.Click += new System.EventHandler(this.MenuItemSave_Click);
//
// printToolStripButton
//
this.printToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.printToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.printToolStripButton.Name = "printToolStripButton";
this.printToolStripButton.Size = new System.Drawing.Size(23, 22);
this.printToolStripButton.Text = "&Print";
//
// toolStripSeparator
//
this.toolStripSeparator.Name = "toolStripSeparator";
this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
//
// ToolStripButtonBold
//
this.ToolStripButtonBold.CheckOnClick = true;
this.ToolStripButtonBold.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.ToolStripButtonBold.ImageTransparentColor = System.Drawing.Color.Magenta;
this.ToolStripButtonBold.Name = "ToolStripButtonBold";
this.ToolStripButtonBold.Size = new System.Drawing.Size(23, 22);
this.ToolStripButtonBold.Text = "toolStripButton1";
this.ToolStripButtonBold.CheckedChanged += new System.EventHandler(this.ToolStripButtonBold_CheckedChanged);
//
// ToolStripButtonItalic
//
this.ToolStripButtonItalic.CheckOnClick = true;
this.ToolStripButtonItalic.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.ToolStripButtonItalic.ImageTransparentColor = System.Drawing.Color.Magenta;
this.ToolStripButtonItalic.Name = "ToolStripButtonItalic";
this.ToolStripButtonItalic.Size = new System.Drawing.Size(23, 22);
this.ToolStripButtonItalic.Text = "toolStripButton2";
this.ToolStripButtonItalic.CheckedChanged += new System.EventHandler(this.ToolStripButtonItalic_CheckedChanged);
//
// ToolStripButtonUnderline
//
this.ToolStripButtonUnderline.CheckOnClick = true;
this.ToolStripButtonUnderline.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.ToolStripButtonUnderline.ImageTransparentColor = System.Drawing.Color.Magenta;
this.ToolStripButtonUnderline.Name = "ToolStripButtonUnderline";
this.ToolStripButtonUnderline.Size = new System.Drawing.Size(23, 22);
this.ToolStripButtonUnderline.Text = "toolStripButton3";
this.ToolStripButtonUnderline.CheckedChanged += new System.EventHandler(this.ToolStripButtonUnderline_CheckedChanged);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
//
// ToolStripComboBoxFonts
//
this.ToolStripComboBoxFonts.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList;
this.ToolStripComboBoxFonts.Items.AddRange(new object[] {
"MS Sans Serif",
"Times New Roman"});
this.ToolStripComboBoxFonts.Name = "ToolStripComboBoxFonts";
this.ToolStripComboBoxFonts.Size = new System.Drawing.Size(121, 25);
this.ToolStripComboBoxFonts.SelectedIndexChanged += new System.EventHandler(this.ToolStripComboBoxFonts_SelectedIndexChanged);
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
//
// helpToolStripButton
//
this.helpToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.helpToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.helpToolStripButton.Name = "helpToolStripButton";
this.helpToolStripButton.Size = new System.Drawing.Size(23, 22);
this.helpToolStripButton.Text = "He&lp";
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabelText,
this.toolStripStatusLabelBold,
this.toolStripStatusLabelItalic,
this.toolStripStatusLabelUnderline});
this.statusStrip1.Location = new System.Drawing.Point(0, 242);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(454, 22);
this.statusStrip1.TabIndex = 3;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabelText
//
this.toolStripStatusLabelText.AutoSize = false;
this.toolStripStatusLabelText.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.toolStripStatusLabelText.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold);
this.toolStripStatusLabelText.Name = "toolStripStatusLabelText";
this.toolStripStatusLabelText.Size = new System.Drawing.Size(259, 17);
this.toolStripStatusLabelText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// toolStripStatusLabelBold
//
this.toolStripStatusLabelBold.AutoSize = false;
this.toolStripStatusLabelBold.DoubleClickEnabled = true;
this.toolStripStatusLabelBold.Enabled = false;
this.toolStripStatusLabelBold.Font = new System.Drawing.Font("Arial", 8.25F);
this.toolStripStatusLabelBold.Name = "toolStripStatusLabelBold";
this.toolStripStatusLabelBold.Size = new System.Drawing.Size(47, 17);
this.toolStripStatusLabelBold.Text = "Bold";
this.toolStripStatusLabelBold.Click += new System.EventHandler(this.toolStripStatusLabelBold_Click);
//
// toolStripStatusLabelItalic
//
this.toolStripStatusLabelItalic.AutoSize = false;
this.toolStripStatusLabelItalic.DoubleClickEnabled = true;
this.toolStripStatusLabelItalic.Enabled = false;
this.toolStripStatusLabelItalic.Font = new System.Drawing.Font("Arial", 8.25F);
this.toolStripStatusLabelItalic.Name = "toolStripStatusLabelItalic";
this.toolStripStatusLabelItalic.Size = new System.Drawing.Size(48, 17);
this.toolStripStatusLabelItalic.Text = "Italic";
this.toolStripStatusLabelItalic.Click += new System.EventHandler(this.toolStripStatusLabelItalic_Click);
//
// toolStripStatusLabelUnderline
//
this.toolStripStatusLabelUnderline.AutoSize = false;
this.toolStripStatusLabelUnderline.DoubleClickEnabled = true;
this.toolStripStatusLabelUnderline.Enabled = false;
this.toolStripStatusLabelUnderline.Font = new System.Drawing.Font("Arial", 8.25F);
this.toolStripStatusLabelUnderline.Name = "toolStripStatusLabelUnderline";
this.toolStripStatusLabelUnderline.Size = new System.Drawing.Size(76, 17);
this.toolStripStatusLabelUnderline.Text = "Underline";
this.toolStripStatusLabelUnderline.Click += new System.EventHandler(this.toolStripStatusLabelUnderline_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(454, 264);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.toolStrip1);
this.Controls.Add(this.richTextBoxText);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.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.ToolStripSeparator toolStripMenuItem2;
private System.Windows.Forms.ToolStripMenuItem printToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem printPreviewToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem3;
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 toolStripMenuItem4;
private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem formatToolStripMenuItem;
private System.Windows.Forms.RichTextBox richTextBoxText;
private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItemBold;
private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItemItalic;
private System.Windows.Forms.ToolStripMenuItem ToolStripMenuItemUnderline;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton newToolStripButton;
private System.Windows.Forms.ToolStripButton openToolStripButton;
private System.Windows.Forms.ToolStripButton saveToolStripButton;
private System.Windows.Forms.ToolStripButton printToolStripButton;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator;
private System.Windows.Forms.ToolStripButton ToolStripButtonBold;
private System.Windows.Forms.ToolStripButton ToolStripButtonItalic;
private System.Windows.Forms.ToolStripButton ToolStripButtonUnderline;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripComboBox ToolStripComboBoxFonts;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripButton helpToolStripButton;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelText;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelBold;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelItalic;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelUnderline;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
RichTextBox: font
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void buttonBold_Click(object sender, EventArgs e) {
Font oldFont;
Font newFont;
oldFont = this.richTextBoxText.SelectionFont;
if (oldFont.Bold)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
}
private void buttonUnderline_Click(object sender, EventArgs e) {
Font oldFont;
Font newFont;
oldFont = this.richTextBoxText.SelectionFont;
if (oldFont.Underline)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
}
private void buttonItalic_Click(object sender, EventArgs e) {
Font oldFont;
Font newFont;
oldFont = this.richTextBoxText.SelectionFont;
if (oldFont.Italic)
newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
else
newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
this.richTextBoxText.SelectionFont = newFont;
this.richTextBoxText.Focus();
}
private void buttonCenter_Click(object sender, EventArgs e) {
if (this.richTextBoxText.SelectionAlignment == HorizontalAlignment.Center)
this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Left;
else
this.richTextBoxText.SelectionAlignment = HorizontalAlignment.Center;
this.richTextBoxText.Focus();
}
private void textBoxSize_KeyPress(object sender, KeyPressEventArgs e) {
if ((e.KeyChar < 48 || e.KeyChar > 57) &&
e.KeyChar != 8 && e.KeyChar != 13) {
e.Handled = true;
} else if (e.KeyChar == 13) {
TextBox txt = (TextBox)sender;
if (txt.Text.Length > 0)
ApplyTextSize(txt.Text);
e.Handled = true;
this.richTextBoxText.Focus();
}
}
private void textBoxSize_Validating(object sender, CancelEventArgs e) {
TextBox txt = (TextBox)sender;
ApplyTextSize(txt.Text);
this.richTextBoxText.Focus();
}
private void ApplyTextSize(string textSize) {
float newSize = Convert.ToSingle(textSize);
FontFamily currentFontFamily;
Font newFont;
currentFontFamily = this.richTextBoxText.SelectionFont.FontFamily;
newFont = new Font(currentFontFamily, newSize);
this.richTextBoxText.SelectionFont = newFont;
}
private void richTextBoxText_LinkClicked(object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}
private void buttonLoad_Click(object sender, EventArgs e) {
try {
richTextBoxText.LoadFile("Test.rtf");
} catch (System.IO.FileNotFoundException) {
MessageBox.Show("No file to load yet");
}
}
private void buttonSave_Click(object sender, EventArgs e) {
try {
richTextBoxText.SaveFile("Test.rtf");
} catch (System.Exception err) {
MessageBox.Show(err.Message);
}
}
private void InitializeComponent() {
this.buttonBold = new System.Windows.Forms.Button();
this.buttonUnderline = new System.Windows.Forms.Button();
this.buttonItalic = new System.Windows.Forms.Button();
this.buttonCenter = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button();
this.buttonLoad = new System.Windows.Forms.Button();
this.labelSize = new System.Windows.Forms.Label();
this.textBoxSize = new System.Windows.Forms.TextBox();
this.richTextBoxText = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
this.buttonBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.buttonBold.Location = new System.Drawing.Point(187, 13);
this.buttonBold.Margin = new System.Windows.Forms.Padding(3, 3, 3, 1);
this.buttonBold.Name = "buttonBold";
this.buttonBold.Size = new System.Drawing.Size(82, 23);
this.buttonBold.TabIndex = 0;
this.buttonBold.Text = "Bold";
this.buttonBold.Click += new System.EventHandler(this.buttonBold_Click);
this.buttonUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.buttonUnderline.Location = new System.Drawing.Point(276, 13);
this.buttonUnderline.Margin = new System.Windows.Forms.Padding(3, 3, 3, 1);
this.buttonUnderline.Name = "buttonUnderline";
this.buttonUnderline.Size = new System.Drawing.Size(82, 23);
this.buttonUnderline.TabIndex = 1;
this.buttonUnderline.Text = "Underline";
this.buttonUnderline.Click += new System.EventHandler(this.buttonUnderline_Click);
this.buttonItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.buttonItalic.Location = new System.Drawing.Point(365, 13);
this.buttonItalic.Name = "buttonItalic";
this.buttonItalic.Size = new System.Drawing.Size(82, 23);
this.buttonItalic.TabIndex = 2;
this.buttonItalic.Text = "Italic";
this.buttonItalic.Click += new System.EventHandler(this.buttonItalic_Click);
this.buttonCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.buttonCenter.Location = new System.Drawing.Point(454, 13);
this.buttonCenter.Margin = new System.Windows.Forms.Padding(3, 0, 3, 3);
this.buttonCenter.Name = "buttonCenter";
this.buttonCenter.Size = new System.Drawing.Size(82, 23);
this.buttonCenter.TabIndex = 3;
this.buttonCenter.Text = "Center";
this.buttonCenter.Click += new System.EventHandler(this.buttonCenter_Click);
this.buttonSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.buttonSave.Location = new System.Drawing.Point(365, 309);
this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 0, 3, 3);
this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(82, 23);
this.buttonSave.TabIndex = 4;
this.buttonSave.Text = "Save";
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
this.buttonLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.buttonLoad.Location = new System.Drawing.Point(276, 309);
this.buttonLoad.Name = "buttonLoad";
this.buttonLoad.Size = new System.Drawing.Size(82, 23);
this.buttonLoad.TabIndex = 5;
this.buttonLoad.Text = "Load";
this.buttonLoad.Click += new System.EventHandler(this.buttonLoad_Click);
this.labelSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.labelSize.AutoSize = true;
this.labelSize.Location = new System.Drawing.Point(295, 46);
this.labelSize.Name = "labelSize";
this.labelSize.Size = new System.Drawing.Size(26, 14);
this.labelSize.TabIndex = 6;
this.labelSize.Text = "Size";
this.textBoxSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.textBoxSize.Location = new System.Drawing.Point(328, 43);
this.textBoxSize.Name = "textBoxSize";
this.textBoxSize.TabIndex = 7;
this.textBoxSize.Text = "10";
this.textBoxSize.Validating += new System.ruponentModel.CancelEventHandler(this.textBoxSize_Validating);
this.textBoxSize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxSize_KeyPress);
this.richTextBoxText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBoxText.Location = new System.Drawing.Point(13, 70);
this.richTextBoxText.Name = "richTextBoxText";
this.richTextBoxText.Size = new System.Drawing.Size(686, 232);
this.richTextBoxText.TabIndex = 8;
this.richTextBoxText.Text = "";
this.richTextBoxText.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBoxText_LinkClicked);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(722, 341);
this.Controls.Add(this.richTextBoxText);
this.Controls.Add(this.textBoxSize);
this.Controls.Add(this.labelSize);
this.Controls.Add(this.buttonLoad);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.buttonCenter);
this.Controls.Add(this.buttonItalic);
this.Controls.Add(this.buttonUnderline);
this.Controls.Add(this.buttonBold);
this.MinimumSize = new System.Drawing.Size(730, 368);
this.Name = "Form1";
this.Text = "RichTextBox Test";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Button buttonBold;
private System.Windows.Forms.Button buttonUnderline;
private System.Windows.Forms.Button buttonItalic;
private System.Windows.Forms.Button buttonCenter;
private System.Windows.Forms.Button buttonSave;
private System.Windows.Forms.Button buttonLoad;
private System.Windows.Forms.Label labelSize;
private System.Windows.Forms.TextBox textBoxSize;
private System.Windows.Forms.RichTextBox richTextBoxText;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}