RichTextBox : RichTextBox : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » RichTextBox »

 

RichTextBox









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

}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» RichTextBox