Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // **** TO TEST:
- // **** Create a new WinForms project in VisualStudio and follow the next 2 following steps
- // ******************************************************************************
- // 1) Replace the class Form1 inside "Form1.cs" file with the following code (be careful not to copy the 2nd step):
- public partial class Form1 : Form
- {
- // Dictionary<TKey,TValue> is preferable to HashTable
- Dictionary<string, string> rtfByRichTextBoxName = new Dictionary<string, string>();
- public Form1()
- {
- InitializeComponent();
- // subscribe the events
- this.comboBoxSelectRichTextBox.SelectedIndexChanged += new EventHandler(comboBoxSelectRichTextBox_SelectedIndexChanged);
- // here we create 3 richTextBox (I initialized them with some rtf text using WordPad)
- this.rtfByRichTextBoxName.Add("RichTextBox A", @"{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
- {\colortbl ;\red255\green0\blue0;}
- {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\lang16\f0\fs22 Hello World \cf0 from A\par}");
- this.rtfByRichTextBoxName.Add("RichTextBox B", @"{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
- {\colortbl ;\red0\green0\blue255;}
- {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\lang16\f0\fs22 Hello World \cf0 from B\par}");
- this.rtfByRichTextBoxName.Add("RichTextBox C", @"{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
- {\colortbl ;\red0\green255\blue0;}
- {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\lang16\f0\fs22 Hello World \cf0 from C\par}");
- // fill the combobox with the available richtextboxes
- foreach (var key in this.rtfByRichTextBoxName.Keys)
- this.comboBoxSelectRichTextBox.Items.Add(key);
- // and select the first item to trigger the event
- this.comboBoxSelectRichTextBox.SelectedIndex = 0;
- }
- void comboBoxSelectRichTextBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- // N.B. we use the Tag property of the richTextBox to store the current key of the richTextBox
- string oldSelectedKey = null;
- if (this.richTextBox1.Tag != null)
- oldSelectedKey = this.richTextBox1.Tag.ToString();
- string newSelectedKey = this.comboBoxSelectRichTextBox.SelectedItem.ToString();
- // if we are switching to a new richTextBox, update the Rtf to the dictionary
- // (to keep possible user changes)
- if(oldSelectedKey != null && newSelectedKey != oldSelectedKey)
- this.rtfByRichTextBoxName[oldSelectedKey] = this.richTextBox1.Rtf;
- // now switch to the new text
- this.richTextBox1.Rtf = this.rtfByRichTextBoxName[newSelectedKey];
- this.richTextBox1.Tag = newSelectedKey;
- }
- private void buttonSetSelectedTextColor_Click(object sender, EventArgs e)
- {
- ColorDialog dlg = new ColorDialog();
- dlg.Color = this.richTextBox1.SelectionColor;
- if (dlg.ShowDialog() == DialogResult.OK)
- this.richTextBox1.SelectionColor = dlg.Color;
- }
- }
- // ******************************************************************************
- // 2) Replace the class Form1 inside "Form1.Designer.cs" file with the following code:
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.richTextBox1 = new System.Windows.Forms.RichTextBox();
- this.buttonSetSelectedTextColor = new System.Windows.Forms.Button();
- this.comboBoxSelectRichTextBox = new System.Windows.Forms.ComboBox();
- this.label1 = new System.Windows.Forms.Label();
- 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.Location = new System.Drawing.Point(12, 116);
- this.richTextBox1.Name = "richTextBox1";
- this.richTextBox1.Size = new System.Drawing.Size(508, 281);
- this.richTextBox1.TabIndex = 0;
- this.richTextBox1.Text = "";
- //
- // buttonSetSelectedTextColor
- //
- this.buttonSetSelectedTextColor.Location = new System.Drawing.Point(12, 87);
- this.buttonSetSelectedTextColor.Name = "buttonSetSelectedTextColor";
- this.buttonSetSelectedTextColor.Size = new System.Drawing.Size(137, 23);
- this.buttonSetSelectedTextColor.TabIndex = 2;
- this.buttonSetSelectedTextColor.Text = "Set Selected Text Color...";
- this.buttonSetSelectedTextColor.UseVisualStyleBackColor = true;
- this.buttonSetSelectedTextColor.Click += new System.EventHandler(this.buttonSetSelectedTextColor_Click);
- //
- // comboBoxSelectRichTextBox
- //
- this.comboBoxSelectRichTextBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
- this.comboBoxSelectRichTextBox.FormattingEnabled = true;
- this.comboBoxSelectRichTextBox.Location = new System.Drawing.Point(159, 6);
- this.comboBoxSelectRichTextBox.Name = "comboBoxSelectRichTextBox";
- this.comboBoxSelectRichTextBox.Size = new System.Drawing.Size(215, 21);
- this.comboBoxSelectRichTextBox.TabIndex = 3;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(12, 9);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(141, 13);
- this.label1.TabIndex = 4;
- this.label1.Text = "Current Visible RichTextBox:";
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(532, 409);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.comboBoxSelectRichTextBox);
- this.Controls.Add(this.buttonSetSelectedTextColor);
- this.Controls.Add(this.richTextBox1);
- this.Name = "Form1";
- this.Text = "Form1";
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.RichTextBox richTextBox1;
- private System.Windows.Forms.Button buttonSetSelectedTextColor;
- private System.Windows.Forms.ComboBox comboBoxSelectRichTextBox;
- private System.Windows.Forms.Label label1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement