RichTextBox: font : RichTextBox « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » RichTextBox 
23.20.2.RichTextBox: font
using System;
using System.Collections.Generic;
using System.ComponentModel;
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 != && 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(18713);
        this.buttonBold.Margin = new System.Windows.Forms.Padding(3331);
        this.buttonBold.Name = "buttonBold";
        this.buttonBold.Size = new System.Drawing.Size(8223);
        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(27613);
        this.buttonUnderline.Margin = new System.Windows.Forms.Padding(3331);
        this.buttonUnderline.Name = "buttonUnderline";
        this.buttonUnderline.Size = new System.Drawing.Size(8223);
        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(36513);
        this.buttonItalic.Name = "buttonItalic";
        this.buttonItalic.Size = new System.Drawing.Size(8223);
        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(45413);
        this.buttonCenter.Margin = new System.Windows.Forms.Padding(3033);
        this.buttonCenter.Name = "buttonCenter";
        this.buttonCenter.Size = new System.Drawing.Size(8223);
        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(365309);
        this.buttonSave.Margin = new System.Windows.Forms.Padding(3033);
        this.buttonSave.Name = "buttonSave";
        this.buttonSave.Size = new System.Drawing.Size(8223);
        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(276309);
        this.buttonLoad.Name = "buttonLoad";
        this.buttonLoad.Size = new System.Drawing.Size(8223);
        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(29546);
        this.labelSize.Name = "labelSize";
        this.labelSize.Size = new System.Drawing.Size(2614);
        this.labelSize.TabIndex = 6;
        this.labelSize.Text = "Size";

        this.textBoxSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.textBoxSize.Location = new System.Drawing.Point(32843);
        this.textBoxSize.Name = "textBoxSize";
        this.textBoxSize.TabIndex = 7;
        this.textBoxSize.Text = "10";
        this.textBoxSize.Validating += new System.ComponentModel.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(1370);
        this.richTextBoxText.Name = "richTextBoxText";
        this.richTextBoxText.Size = new System.Drawing.Size(686232);
        this.richTextBoxText.TabIndex = 8;
        this.richTextBoxText.Text = "";
        this.richTextBoxText.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBoxText_LinkClicked);

        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(722341);
        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(730368);
        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());
    }

}
23.20.RichTextBox
23.20.1.LoadFile SaveFile
23.20.2.RichTextBox: font
23.20.3.Editor based on RichTextBoxEditor based on RichTextBox
23.20.4.Build Menu for a RichTextBox editorBuild Menu for a RichTextBox editor
23.20.5.RichTextBox based Text Editor
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.