Csharp/C Sharp by API/System.Windows.Forms/RichTextBox — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:09, 26 мая 2010

RichTextBox.Focus()

  

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());
    }
}


RichTextBox.Multiline

 
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor1 : Form
{
  public FontConstructor1()
  {
    Size = new Size(350,200);
    Font fnt = Font;
    fnt = new Font(fnt, FontStyle.Bold | FontStyle.Italic);
    Font = fnt;
    RichTextBox rtxt = new RichTextBox();
    rtxt.Text = "first line.\n" +
          "This is a second line of text.";
    rtxt.Text += "\nFont Name:\t" + Font.Name;
    rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
    rtxt.Text += "\nFont Styles:\t" + Font.Style;
    rtxt.Text += "\nFont Size:\t" + Font.Size;
    rtxt.Text += "\nFont Height:\t" + Font.Height;
    rtxt.Text += "\nFont Units:\t" + Font.Unit;
    rtxt.Multiline = true;
    rtxt.Dock = DockStyle.Fill;
    rtxt.Parent = this;
  }
  static void Main() 
  {
    Application.Run(new FontConstructor1());
  }
}


RichTextBox.SelectionChanged

   

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;
public class Form1 : Form
{
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.ToolStrip toolStrip1;
    private System.Windows.Forms.ToolStripButton cmdUnderline;
    private System.Windows.Forms.ToolStripButton cmdBold;
    private System.Windows.Forms.ToolStripButton cmdItalic;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
    private System.Windows.Forms.ToolStripDropDownButton lstColors;
    private System.Windows.Forms.ToolStripDropDownButton lstFonts;
    private System.Windows.Forms.ToolStripDropDownButton lstZoom;
    private System.Windows.Forms.ToolStripDropDownButton lstFontSize;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
    private System.Windows.Forms.Button cmdAddImage;
  public Form1() {
        InitializeComponent();
  }
    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont != null)
        {
            cmdBold.Checked = richTextBox1.SelectionFont.Bold;
            cmdItalic.Checked = richTextBox1.SelectionFont.Italic;
            cmdUnderline.Checked = richTextBox1.SelectionFont.Underline;
        }
    }
    private void cmdBold_Click(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        FontStyle style = richTextBox1.SelectionFont.Style;
        
        if (richTextBox1.SelectionFont.Bold)
        {
            style &= ~FontStyle.Bold;
        }
        else
        {
            style |= FontStyle.Bold;
            
        }
        richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
    }
    private void cmdItalic_Click(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        FontStyle style = richTextBox1.SelectionFont.Style;
        if (richTextBox1.SelectionFont.Italic)
        {
            style &= ~FontStyle.Italic;
        }
        else
        {
            style |= FontStyle.Italic;
        }
        richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
    }
    private void cmdUnderline_Click(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        FontStyle style = richTextBox1.SelectionFont.Style;
        if (richTextBox1.SelectionFont.Underline)
        {
            style &= ~FontStyle.Underline;
        }
        else
        {
            style |= FontStyle.Underline;
        }
        richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        lstColors.DropDown.Items.Add("Red");
        lstColors.DropDown.Items.Add("Blue");
        
        InstalledFontCollection fonts = new InstalledFontCollection();
        foreach (FontFamily family in fonts.Families)
        {
            lstFonts.DropDown.Items.Add(family.Name);
        }
        lstZoom.DropDown.Items.Add("300%");             
        lstZoom.DropDown.Items.Add("200%");             
        lstZoom.DropDown.Items.Add("100%");             
        lstFontSize.DropDown.Items.Add("8");
        lstFontSize.DropDown.Items.Add("10");
        lstFontSize.DropDown.Items.Add("12");
    }
   
    private void lstColors_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        
      KnownColor selectedColor;
      selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), e.ClickedItem.Text);
        richTextBox1.SelectionColor = Color.FromKnownColor(selectedColor);
    }
    private void lstFonts_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            richTextBox1.SelectionFont = new Font(e.ClickedItem.Text, richTextBox1.Font.Size);
        }
        richTextBox1.SelectionFont = new Font(e.ClickedItem.Text, richTextBox1.SelectionFont.Size);
    }
    private void lstZoom_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {            
        float zoomPercent = Convert.ToSingle(e.ClickedItem.Text.Trim("%"));
        richTextBox1.ZoomFactor = zoomPercent / 100;
    }
    private void lstFontSize_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        richTextBox1.SelectionFont =new Font(richTextBox1.SelectionFont.FontFamily,
            Convert.ToInt32(e.ClickedItem.Text),
            richTextBox1.SelectionFont.Style);
    }
     private void cmdAddImage_Click(object sender, EventArgs e)
    {
         Image img = Image.FromFile("winter.jpg");
         Clipboard.SetImage(img);
         
         richTextBox1.SelectionStart = 0;
         richTextBox1.Paste();
         Clipboard.Clear();
    }
    private void InitializeComponent()
    {
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.toolStrip1 = new System.Windows.Forms.ToolStrip();
        this.cmdBold = new System.Windows.Forms.ToolStripButton();
        this.cmdItalic = new System.Windows.Forms.ToolStripButton();
        this.cmdUnderline = new System.Windows.Forms.ToolStripButton();
        this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
        this.lstColors = new System.Windows.Forms.ToolStripDropDownButton();
        this.lstFonts = new System.Windows.Forms.ToolStripDropDownButton();
        this.lstFontSize = new System.Windows.Forms.ToolStripDropDownButton();
        this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
        this.lstZoom = new System.Windows.Forms.ToolStripDropDownButton();
        this.cmdAddImage = new System.Windows.Forms.Button();
        this.toolStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // richTextBox1
        // 
        this.richTextBox1.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.richTextBox1.BulletIndent = 5;
        this.richTextBox1.Location = new System.Drawing.Point(12, 37);
        this.richTextBox1.Margin = new System.Windows.Forms.Padding(5);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(317, 197);
        this.richTextBox1.TabIndex = 0;
        this.richTextBox1.Text = "adfasdf";
        this.richTextBox1.SelectionChanged += new System.EventHandler(this.richTextBox1_SelectionChanged);
        // 
        // toolStrip1
        // 
        this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.cmdBold,
        this.cmdItalic,
        this.cmdUnderline,
        this.toolStripSeparator1,
        this.lstColors,
        this.lstFonts,
        this.lstFontSize,
        this.toolStripSeparator2,
        this.lstZoom});
        this.toolStrip1.Location = new System.Drawing.Point(0, 0);
        this.toolStrip1.Name = "toolStrip1";
        this.toolStrip1.Size = new System.Drawing.Size(341, 25);
        this.toolStrip1.TabIndex = 1;
        this.toolStrip1.Text = "toolStrip1";
        // 
        // cmdBold
        // 
        this.cmdBold.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.cmdBold.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.cmdBold.Name = "cmdBold";
        this.cmdBold.Size = new System.Drawing.Size(31, 22);
        this.cmdBold.Text = "Bold";
        this.cmdBold.Click += new System.EventHandler(this.cmdBold_Click);
        // 
        // cmdItalic
        // 
        this.cmdItalic.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.cmdItalic.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.cmdItalic.Name = "cmdItalic";
        this.cmdItalic.Size = new System.Drawing.Size(34, 22);
        this.cmdItalic.Text = "Italic";
        this.cmdItalic.Click += new System.EventHandler(this.cmdItalic_Click);
        // 
        // cmdUnderline
        // 
        this.cmdUnderline.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.cmdUnderline.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.cmdUnderline.Name = "cmdUnderline";
        this.cmdUnderline.Size = new System.Drawing.Size(56, 22);
        this.cmdUnderline.Text = "Underline";
        this.cmdUnderline.Click += new System.EventHandler(this.cmdUnderline_Click);
        // 
        // toolStripSeparator1
        // 
        this.toolStripSeparator1.Name = "toolStripSeparator1";
        this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
        // 
        // lstColors
        // 
        this.lstColors.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstColors.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstColors.Name = "lstColors";
        this.lstColors.Size = new System.Drawing.Size(45, 22);
        this.lstColors.Text = "Color";
        this.lstColors.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstColors_DropDownItemClicked);
        // 
        // lstFonts
        // 
        this.lstFonts.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstFonts.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstFonts.Name = "lstFonts";
        this.lstFonts.Size = new System.Drawing.Size(42, 22);
        this.lstFonts.Text = "Font";
        this.lstFonts.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstFonts_DropDownItemClicked);
        // 
        // lstFontSize
        // 
        this.lstFontSize.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstFontSize.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstFontSize.Name = "lstFontSize";
        this.lstFontSize.Size = new System.Drawing.Size(39, 22);
        this.lstFontSize.Text = "Size";
        this.lstFontSize.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstFontSize_DropDownItemClicked);
        // 
        // toolStripSeparator2
        // 
        this.toolStripSeparator2.Name = "toolStripSeparator2";
        this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
        // 
        // lstZoom
        // 
        this.lstZoom.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstZoom.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstZoom.Name = "lstZoom";
        this.lstZoom.Size = new System.Drawing.Size(46, 22);
        this.lstZoom.Text = "Zoom";
        this.lstZoom.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstZoom_DropDownItemClicked);
        // 
        // cmdAddImage
        // 
        this.cmdAddImage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
        this.cmdAddImage.Location = new System.Drawing.Point(12, 237);
        this.cmdAddImage.Name = "cmdAddImage";
        this.cmdAddImage.Size = new System.Drawing.Size(154, 23);
        this.cmdAddImage.TabIndex = 2;
        this.cmdAddImage.Text = "Insert Image";
        this.cmdAddImage.UseVisualStyleBackColor = true;
        this.cmdAddImage.Click += new System.EventHandler(this.cmdAddImage_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(341, 266);
        this.Controls.Add(this.cmdAddImage);
        this.Controls.Add(this.toolStrip1);
        this.Controls.Add(this.richTextBox1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "RichTextBox Test";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.toolStrip1.ResumeLayout(false);
        this.toolStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();
    }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


RichTextBox.SelectionFont

   
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;
public class Form1 : Form
{
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.ToolStrip toolStrip1;
    private System.Windows.Forms.ToolStripButton cmdUnderline;
    private System.Windows.Forms.ToolStripButton cmdBold;
    private System.Windows.Forms.ToolStripButton cmdItalic;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
    private System.Windows.Forms.ToolStripDropDownButton lstColors;
    private System.Windows.Forms.ToolStripDropDownButton lstFonts;
    private System.Windows.Forms.ToolStripDropDownButton lstZoom;
    private System.Windows.Forms.ToolStripDropDownButton lstFontSize;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
    private System.Windows.Forms.Button cmdAddImage;
  public Form1() {
        InitializeComponent();
  }
    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont != null)
        {
            cmdBold.Checked = richTextBox1.SelectionFont.Bold;
            cmdItalic.Checked = richTextBox1.SelectionFont.Italic;
            cmdUnderline.Checked = richTextBox1.SelectionFont.Underline;
        }
    }
    private void cmdBold_Click(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        FontStyle style = richTextBox1.SelectionFont.Style;
        
        if (richTextBox1.SelectionFont.Bold)
        {
            style &= ~FontStyle.Bold;
        }
        else
        {
            style |= FontStyle.Bold;
            
        }
        richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
    }
    private void cmdItalic_Click(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        FontStyle style = richTextBox1.SelectionFont.Style;
        if (richTextBox1.SelectionFont.Italic)
        {
            style &= ~FontStyle.Italic;
        }
        else
        {
            style |= FontStyle.Italic;
        }
        richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
    }
    private void cmdUnderline_Click(object sender, EventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        FontStyle style = richTextBox1.SelectionFont.Style;
        if (richTextBox1.SelectionFont.Underline)
        {
            style &= ~FontStyle.Underline;
        }
        else
        {
            style |= FontStyle.Underline;
        }
        richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        lstColors.DropDown.Items.Add("Red");
        lstColors.DropDown.Items.Add("Blue");
        
        InstalledFontCollection fonts = new InstalledFontCollection();
        foreach (FontFamily family in fonts.Families)
        {
            lstFonts.DropDown.Items.Add(family.Name);
        }
        lstZoom.DropDown.Items.Add("300%");             
        lstZoom.DropDown.Items.Add("200%");             
        lstZoom.DropDown.Items.Add("100%");             
        lstFontSize.DropDown.Items.Add("8");
        lstFontSize.DropDown.Items.Add("10");
        lstFontSize.DropDown.Items.Add("12");
    }
   
    private void lstColors_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        
      KnownColor selectedColor;
      selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), e.ClickedItem.Text);
        richTextBox1.SelectionColor = Color.FromKnownColor(selectedColor);
    }
    private void lstFonts_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            richTextBox1.SelectionFont = new Font(e.ClickedItem.Text, richTextBox1.Font.Size);
        }
        richTextBox1.SelectionFont = new Font(e.ClickedItem.Text, richTextBox1.SelectionFont.Size);
    }
    private void lstZoom_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {            
        float zoomPercent = Convert.ToSingle(e.ClickedItem.Text.Trim("%"));
        richTextBox1.ZoomFactor = zoomPercent / 100;
    }
    private void lstFontSize_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (richTextBox1.SelectionFont == null)
        {
            return;
        }
        richTextBox1.SelectionFont =new Font(richTextBox1.SelectionFont.FontFamily,
            Convert.ToInt32(e.ClickedItem.Text),
            richTextBox1.SelectionFont.Style);
    }
     private void cmdAddImage_Click(object sender, EventArgs e)
    {
         Image img = Image.FromFile("winter.jpg");
         Clipboard.SetImage(img);
         
         richTextBox1.SelectionStart = 0;
         richTextBox1.Paste();
         Clipboard.Clear();
    }
    private void InitializeComponent()
    {
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.toolStrip1 = new System.Windows.Forms.ToolStrip();
        this.cmdBold = new System.Windows.Forms.ToolStripButton();
        this.cmdItalic = new System.Windows.Forms.ToolStripButton();
        this.cmdUnderline = new System.Windows.Forms.ToolStripButton();
        this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
        this.lstColors = new System.Windows.Forms.ToolStripDropDownButton();
        this.lstFonts = new System.Windows.Forms.ToolStripDropDownButton();
        this.lstFontSize = new System.Windows.Forms.ToolStripDropDownButton();
        this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
        this.lstZoom = new System.Windows.Forms.ToolStripDropDownButton();
        this.cmdAddImage = new System.Windows.Forms.Button();
        this.toolStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // richTextBox1
        // 
        this.richTextBox1.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.richTextBox1.BulletIndent = 5;
        this.richTextBox1.Location = new System.Drawing.Point(12, 37);
        this.richTextBox1.Margin = new System.Windows.Forms.Padding(5);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(317, 197);
        this.richTextBox1.TabIndex = 0;
        this.richTextBox1.Text = "adfasdf";
        this.richTextBox1.SelectionChanged += new System.EventHandler(this.richTextBox1_SelectionChanged);
        // 
        // toolStrip1
        // 
        this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.cmdBold,
        this.cmdItalic,
        this.cmdUnderline,
        this.toolStripSeparator1,
        this.lstColors,
        this.lstFonts,
        this.lstFontSize,
        this.toolStripSeparator2,
        this.lstZoom});
        this.toolStrip1.Location = new System.Drawing.Point(0, 0);
        this.toolStrip1.Name = "toolStrip1";
        this.toolStrip1.Size = new System.Drawing.Size(341, 25);
        this.toolStrip1.TabIndex = 1;
        this.toolStrip1.Text = "toolStrip1";
        // 
        // cmdBold
        // 
        this.cmdBold.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.cmdBold.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.cmdBold.Name = "cmdBold";
        this.cmdBold.Size = new System.Drawing.Size(31, 22);
        this.cmdBold.Text = "Bold";
        this.cmdBold.Click += new System.EventHandler(this.cmdBold_Click);
        // 
        // cmdItalic
        // 
        this.cmdItalic.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.cmdItalic.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.cmdItalic.Name = "cmdItalic";
        this.cmdItalic.Size = new System.Drawing.Size(34, 22);
        this.cmdItalic.Text = "Italic";
        this.cmdItalic.Click += new System.EventHandler(this.cmdItalic_Click);
        // 
        // cmdUnderline
        // 
        this.cmdUnderline.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.cmdUnderline.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.cmdUnderline.Name = "cmdUnderline";
        this.cmdUnderline.Size = new System.Drawing.Size(56, 22);
        this.cmdUnderline.Text = "Underline";
        this.cmdUnderline.Click += new System.EventHandler(this.cmdUnderline_Click);
        // 
        // toolStripSeparator1
        // 
        this.toolStripSeparator1.Name = "toolStripSeparator1";
        this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
        // 
        // lstColors
        // 
        this.lstColors.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstColors.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstColors.Name = "lstColors";
        this.lstColors.Size = new System.Drawing.Size(45, 22);
        this.lstColors.Text = "Color";
        this.lstColors.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstColors_DropDownItemClicked);
        // 
        // lstFonts
        // 
        this.lstFonts.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstFonts.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstFonts.Name = "lstFonts";
        this.lstFonts.Size = new System.Drawing.Size(42, 22);
        this.lstFonts.Text = "Font";
        this.lstFonts.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstFonts_DropDownItemClicked);
        // 
        // lstFontSize
        // 
        this.lstFontSize.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstFontSize.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstFontSize.Name = "lstFontSize";
        this.lstFontSize.Size = new System.Drawing.Size(39, 22);
        this.lstFontSize.Text = "Size";
        this.lstFontSize.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstFontSize_DropDownItemClicked);
        // 
        // toolStripSeparator2
        // 
        this.toolStripSeparator2.Name = "toolStripSeparator2";
        this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
        // 
        // lstZoom
        // 
        this.lstZoom.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.lstZoom.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.lstZoom.Name = "lstZoom";
        this.lstZoom.Size = new System.Drawing.Size(46, 22);
        this.lstZoom.Text = "Zoom";
        this.lstZoom.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstZoom_DropDownItemClicked);
        // 
        // cmdAddImage
        // 
        this.cmdAddImage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
        this.cmdAddImage.Location = new System.Drawing.Point(12, 237);
        this.cmdAddImage.Name = "cmdAddImage";
        this.cmdAddImage.Size = new System.Drawing.Size(154, 23);
        this.cmdAddImage.TabIndex = 2;
        this.cmdAddImage.Text = "Insert Image";
        this.cmdAddImage.UseVisualStyleBackColor = true;
        this.cmdAddImage.Click += new System.EventHandler(this.cmdAddImage_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(341, 266);
        this.Controls.Add(this.cmdAddImage);
        this.Controls.Add(this.toolStrip1);
        this.Controls.Add(this.richTextBox1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "RichTextBox Test";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.toolStrip1.ResumeLayout(false);
        this.toolStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();
    }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


RichTextBox.SelectionLength

  
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;
  }
}