RichTextBox.SelectionChanged : RichTextBox « System.Windows.Forms « C# / C Sharp by API

Home
C# / C Sharp by API
1.Microsoft.Win32
2.System
3.System.Collections
4.System.Collections.Generic
5.System.Collections.Specialized
6.System.ComponentModel
7.System.Configuration
8.System.Data
9.System.Data.Common
10.System.Data.Linq
11.System.Data.Odbc
12.System.Data.OleDb
13.System.Data.Sql
14.System.Data.SqlClient
15.System.Diagnostics
16.System.DirectoryServices
17.System.Drawing
18.System.Drawing.Drawing2D
19.System.Drawing.Imaging
20.System.Drawing.Printing
21.System.Drawing.Text
22.System.EnterpriseServices
23.System.Globalization
24.System.IO
25.System.IO.Compression
26.System.IO.IsolatedStorage
27.System.IO.Ports
28.System.Linq
29.System.Management
30.System.Media
31.System.Messaging
32.System.Net
33.System.Net.Mail
34.System.Net.NetworkInformation
35.System.Net.Sockets
36.System.Reflection
37.System.Resources
38.System.Runtime
39.System.Runtime.CompilerServices
40.System.Runtime.InteropServices
41.System.Runtime.Remoting
42.System.Runtime.Remoting.Channels
43.System.Runtime.Remoting.Channels.Http
44.System.Runtime.Remoting.Messaging
45.System.Runtime.Serialization
46.System.Runtime.Serialization.Formatters.Binary
47.System.Runtime.Serialization.Formatters.Soap
48.System.Security
49.System.Security.AccessControl
50.System.Security.Cryptography
51.System.Security.Cryptography.X509Certificates
52.System.Security.Permissions
53.System.Security.Policy
54.System.Security.Principal
55.System.ServiceProcess
56.System.Text
57.System.Text.RegularExpressions
58.System.Threading
59.System.Timers
60.System.Web.Security
61.System.Web.Services
62.System.Windows.Controls
63.System.Windows.Forms
64.System.Xml
65.System.Xml.Linq
66.System.Xml.Schema
67.System.Xml.Serialization
68.System.Xml.XPath
69.System.Xml.Xsl
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp by API » System.Windows.Forms » RichTextBox 
RichTextBox.SelectionChanged
   


using System;
using System.Collections.Generic;
using System.ComponentModel;
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(1237);
        this.richTextBox1.Margin = new System.Windows.Forms.Padding(5);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(317197);
        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(00);
        this.toolStrip1.Name = "toolStrip1";
        this.toolStrip1.Size = new System.Drawing.Size(34125);
        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(3122);
        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(3422);
        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(5622);
        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(625);
        // 
        // 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(4522);
        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(4222);
        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(3922);
        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(625);
        // 
        // 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(4622);
        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(12237);
        this.cmdAddImage.Name = "cmdAddImage";
        this.cmdAddImage.Size = new System.Drawing.Size(15423);
        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(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(341266);
        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());
  }

}

   
    
    
  
Related examples in the same category
1.RichTextBox.Focus()
2.RichTextBox.Multiline
3.RichTextBox.SelectionFont
4.RichTextBox.SelectionLength
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.