RichTextBox Zoom : RichTextBox « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » GUI Windows Form » RichTextBoxScreenshots 
RichTextBox Zoom
RichTextBox Zoom


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.Paste Image to RichTextBoxPaste Image to RichTextBox
2.Set Font size, family for a RichTextBoxSet Font size, family for a RichTextBox
3.RichTextBox Font bold, italicRichTextBox Font bold, italic
4.Editor FormEditor Form
5.RTFBox TestRTFBox Test
6.RichTextBox Zooming
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.