Advertisement
digemall

Rtf Example

Feb 13th, 2012
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.71 KB | None | 0 0
  1. // **** TO TEST:
  2. // **** Create a new WinForms project in VisualStudio and follow the next 2 following steps
  3.  
  4. // ******************************************************************************
  5. // 1) Replace the class Form1 inside "Form1.cs" file with the following code (be careful not to copy the 2nd step):
  6. public partial class Form1 : Form
  7. {
  8.     // Dictionary<TKey,TValue> is preferable to HashTable
  9.     Dictionary<string, string> rtfByRichTextBoxName = new Dictionary<string, string>();
  10.  
  11.     public Form1()
  12.     {
  13.         InitializeComponent();
  14.  
  15.         // subscribe the events
  16.         this.comboBoxSelectRichTextBox.SelectedIndexChanged += new EventHandler(comboBoxSelectRichTextBox_SelectedIndexChanged);
  17.  
  18.         // here we create 3 richTextBox (I initialized them with some rtf text using WordPad)
  19.         this.rtfByRichTextBoxName.Add("RichTextBox A", @"{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
  20.                                                         {\colortbl ;\red255\green0\blue0;}
  21.                                                         {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\lang16\f0\fs22 Hello World \cf0 from A\par}");
  22.         this.rtfByRichTextBoxName.Add("RichTextBox B", @"{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
  23.                                                         {\colortbl ;\red0\green0\blue255;}
  24.                                                         {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\lang16\f0\fs22 Hello World \cf0 from B\par}");
  25.         this.rtfByRichTextBoxName.Add("RichTextBox C", @"{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
  26.                                                         {\colortbl ;\red0\green255\blue0;}
  27.                                                         {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\lang16\f0\fs22 Hello World \cf0 from C\par}");
  28.  
  29.         // fill the combobox with the available richtextboxes
  30.         foreach (var key in this.rtfByRichTextBoxName.Keys)
  31.             this.comboBoxSelectRichTextBox.Items.Add(key);
  32.         // and select the first item to trigger the event
  33.         this.comboBoxSelectRichTextBox.SelectedIndex = 0;
  34.     }
  35.  
  36.     void comboBoxSelectRichTextBox_SelectedIndexChanged(object sender, EventArgs e)
  37.     {
  38.         // N.B. we use the Tag property of the richTextBox to store the current key of the richTextBox
  39.  
  40.         string oldSelectedKey = null;
  41.         if (this.richTextBox1.Tag != null)
  42.             oldSelectedKey = this.richTextBox1.Tag.ToString();
  43.        
  44.         string newSelectedKey = this.comboBoxSelectRichTextBox.SelectedItem.ToString();
  45.        
  46.         // if we are switching to a new richTextBox, update the Rtf to the dictionary
  47.         // (to keep possible user changes)
  48.         if(oldSelectedKey != null && newSelectedKey != oldSelectedKey)
  49.             this.rtfByRichTextBoxName[oldSelectedKey] = this.richTextBox1.Rtf;
  50.  
  51.         // now switch to the new text
  52.         this.richTextBox1.Rtf = this.rtfByRichTextBoxName[newSelectedKey];
  53.  
  54.         this.richTextBox1.Tag = newSelectedKey;
  55.     }
  56.  
  57.  
  58.     private void buttonSetSelectedTextColor_Click(object sender, EventArgs e)
  59.     {
  60.         ColorDialog dlg = new ColorDialog();
  61.         dlg.Color = this.richTextBox1.SelectionColor;
  62.         if (dlg.ShowDialog() == DialogResult.OK)
  63.             this.richTextBox1.SelectionColor = dlg.Color;
  64.     }
  65. }
  66.  
  67. // ******************************************************************************
  68. // 2) Replace the class Form1 inside "Form1.Designer.cs" file with the following code:
  69.  
  70. partial class Form1
  71. {
  72.     /// <summary>
  73.     /// Required designer variable.
  74.     /// </summary>
  75.     private System.ComponentModel.IContainer components = null;
  76.  
  77.     /// <summary>
  78.     /// Clean up any resources being used.
  79.     /// </summary>
  80.     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  81.     protected override void Dispose(bool disposing)
  82.     {
  83.         if (disposing && (components != null))
  84.         {
  85.             components.Dispose();
  86.         }
  87.         base.Dispose(disposing);
  88.     }
  89.  
  90.     #region Windows Form Designer generated code
  91.  
  92.     /// <summary>
  93.     /// Required method for Designer support - do not modify
  94.     /// the contents of this method with the code editor.
  95.     /// </summary>
  96.     private void InitializeComponent()
  97.     {
  98.         this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  99.         this.buttonSetSelectedTextColor = new System.Windows.Forms.Button();
  100.         this.comboBoxSelectRichTextBox = new System.Windows.Forms.ComboBox();
  101.         this.label1 = new System.Windows.Forms.Label();
  102.         this.SuspendLayout();
  103.         //
  104.         // richTextBox1
  105.         //
  106.         this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  107.                     | System.Windows.Forms.AnchorStyles.Left)
  108.                     | System.Windows.Forms.AnchorStyles.Right)));
  109.         this.richTextBox1.Location = new System.Drawing.Point(12, 116);
  110.         this.richTextBox1.Name = "richTextBox1";
  111.         this.richTextBox1.Size = new System.Drawing.Size(508, 281);
  112.         this.richTextBox1.TabIndex = 0;
  113.         this.richTextBox1.Text = "";
  114.         //
  115.         // buttonSetSelectedTextColor
  116.         //
  117.         this.buttonSetSelectedTextColor.Location = new System.Drawing.Point(12, 87);
  118.         this.buttonSetSelectedTextColor.Name = "buttonSetSelectedTextColor";
  119.         this.buttonSetSelectedTextColor.Size = new System.Drawing.Size(137, 23);
  120.         this.buttonSetSelectedTextColor.TabIndex = 2;
  121.         this.buttonSetSelectedTextColor.Text = "Set Selected Text Color...";
  122.         this.buttonSetSelectedTextColor.UseVisualStyleBackColor = true;
  123.         this.buttonSetSelectedTextColor.Click += new System.EventHandler(this.buttonSetSelectedTextColor_Click);
  124.         //
  125.         // comboBoxSelectRichTextBox
  126.         //
  127.         this.comboBoxSelectRichTextBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  128.         this.comboBoxSelectRichTextBox.FormattingEnabled = true;
  129.         this.comboBoxSelectRichTextBox.Location = new System.Drawing.Point(159, 6);
  130.         this.comboBoxSelectRichTextBox.Name = "comboBoxSelectRichTextBox";
  131.         this.comboBoxSelectRichTextBox.Size = new System.Drawing.Size(215, 21);
  132.         this.comboBoxSelectRichTextBox.TabIndex = 3;
  133.         //
  134.         // label1
  135.         //
  136.         this.label1.AutoSize = true;
  137.         this.label1.Location = new System.Drawing.Point(12, 9);
  138.         this.label1.Name = "label1";
  139.         this.label1.Size = new System.Drawing.Size(141, 13);
  140.         this.label1.TabIndex = 4;
  141.         this.label1.Text = "Current Visible RichTextBox:";
  142.         //
  143.         // Form1
  144.         //
  145.         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  146.         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  147.         this.ClientSize = new System.Drawing.Size(532, 409);
  148.         this.Controls.Add(this.label1);
  149.         this.Controls.Add(this.comboBoxSelectRichTextBox);
  150.         this.Controls.Add(this.buttonSetSelectedTextColor);
  151.         this.Controls.Add(this.richTextBox1);
  152.         this.Name = "Form1";
  153.         this.Text = "Form1";
  154.         this.ResumeLayout(false);
  155.         this.PerformLayout();
  156.  
  157.     }
  158.  
  159.     #endregion
  160.  
  161.     private System.Windows.Forms.RichTextBox richTextBox1;
  162.     private System.Windows.Forms.Button buttonSetSelectedTextColor;
  163.     private System.Windows.Forms.ComboBox comboBoxSelectRichTextBox;
  164.     private System.Windows.Forms.Label label1;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement