joi, 18 martie 2010

User re-sizable text box control that...

User re-sizable text box control that support multiple fonts/colors


What I want to have:
- a user re-sizable text box
- text box in which I can write texts with different colors/fonts

This is how the control looks like and works:



The control that I am created is derived from a UserControl and has two controls inside:
  • a RichTextBox control: this is used for having different fonts/color in the same text box
  • a PictureBox control: used for resizing


Code:

ResizableRichTextBoxCtrl.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace george.lache.Controls
{
/// <summary>
/// - a user re-sizable text box
/// - text box in which it can be written texts with different colors/fonts
/// </summary>
public partial class ResizableRichTextBoxCtrl : UserControl
{
public event EventHandler EditValueChanged;
public new event KeyEventHandler KeyUp;

private readonly List<KeyValuePair<string, Color>> m_texts = new List<KeyValuePair<string, Color>>();
private bool allowResize;

public ResizableRichTextBoxCtrl()
{
InitializeComponent();
}

/// <summary>
/// Gets or sets the current font used for text
/// </summary>
public Font SelectionFont
{
get { return m_richTextBox.SelectionFont; }
set { m_richTextBox.SelectionFont = value; }
}

/// <summary>
/// Gets or sets the Text of control
/// </summary>
public new string Text
{
get { return m_richTextBox.Text; }
set
{
ClearText();
if (string.IsNullOrEmpty(value)) return;

AppendText(value, Color.Black);
m_richTextBox.SelectionLength = 0;
m_richTextBox.SelectionStart = m_richTextBox.Text.Length;
}
}

/// <summary>
/// Gets or sets the starting point of text selected in the text box.
/// </summary>
public int SelectionStart
{
get { return m_richTextBox.SelectionStart; }
set { m_richTextBox.SelectionStart = value; }
}

/// <summary>
/// Apends new text to the existing one.
/// </summary>
/// <param name="i_text">The new text</param>
/// <param name="i_color">The <see cref="Color"/>of the text</param>
public void AppendText(string i_text, Color i_color)
{
if (m_richTextBox.Text.Length == 0)
{
m_richTextBox.Text = " ";
}

m_texts.Add(new KeyValuePair<string, Color>(i_text, i_color));

m_richTextBox.SelectionFont = i_color != Color.Black
? new Font("Tahoma", 8, FontStyle.Bold)
: new Font("Tahoma", 8);

m_richTextBox.SelectionColor = i_color;
m_richTextBox.SelectedText = i_text;

m_richTextBox.SelectionColor = Color.Black;
}


/// <summary>
/// Removes last added text
/// </summary>
public void RemoveLastAddedText()
{
if (m_texts.Count == 0) return;
m_texts.RemoveAt(m_texts.Count - 1);
m_richTextBox.Text = " ";
foreach (var _pair in m_texts)
{
AppendText(_pair.Key, _pair.Value);
}
}

/// <summary>
/// Cleans the text of the control
/// </summary>
public void ClearText()
{
m_richTextBox.Text = string.Empty;
m_texts.Clear();
}

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
allowResize = true;
}

private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
allowResize = false;
}

private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (allowResize)
{
Height = m_pictureBox.Top + e.Y;
Width = m_pictureBox.Left + e.X;
}
}

private void m_richTextBox_TextChanged(object sender, EventArgs e)
{
EventHandler _eh = EditValueChanged;
if (_eh != null)
{
_eh(sender, e);
}
}

private void m_richTextBox_KeyUp(object sender, KeyEventArgs e)
{
KeyEventHandler _eh = KeyUp;
if (_eh != null)
{
_eh(sender, e);
}
}
}
}


ResizableRichTextBoxCtrl.Designer.cs

namespace aptixia.sv.ClientFormula.Controls
{
partial class ResizableRichTextBoxCtrl
{
/// <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 Component 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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ResizableRichTextBoxCtrl));
this.m_pictureBox = new System.Windows.Forms.PictureBox();
this.m_richTextBox = new System.Windows.Forms.RichTextBox();
((System.ComponentModel.ISupportInitialize)(this.m_pictureBox)).BeginInit();
this.SuspendLayout();
//
// m_pictureBox
//
this.m_pictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.m_pictureBox.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
this.m_pictureBox.Image = ((System.Drawing.Image)(resources.GetObject("m_pictureBox.Image")));
this.m_pictureBox.Location = new System.Drawing.Point(128, 11);
this.m_pictureBox.Name = "m_pictureBox";
this.m_pictureBox.Size = new System.Drawing.Size(16, 16);
this.m_pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.m_pictureBox.TabIndex = 8;
this.m_pictureBox.TabStop = false;
this.m_pictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
this.m_pictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseDown);
this.m_pictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseUp);
//
// m_richTextBox
//
this.m_richTextBox.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.m_richTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.m_richTextBox.Location = new System.Drawing.Point(-1, -1);
this.m_richTextBox.Name = "m_richTextBox";
this.m_richTextBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
this.m_richTextBox.Size = new System.Drawing.Size(143, 27);
this.m_richTextBox.TabIndex = 7;
this.m_richTextBox.Text = "";
this.m_richTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.m_richTextBox_KeyUp);
this.m_richTextBox.TextChanged += new System.EventHandler(this.m_richTextBox_TextChanged);
//
// ResizableRichTextBoxCtrl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.m_pictureBox);
this.Controls.Add(this.m_richTextBox);
this.Name = "ResizableRichTextBoxCtrl";
this.Size = new System.Drawing.Size(142, 26);
((System.ComponentModel.ISupportInitialize)(this.m_pictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.PictureBox m_pictureBox;
private System.Windows.Forms.RichTextBox m_richTextBox;

}
}

Use of this control:

resizableRichTextBoxCtrl_1.AppendText("=", Color.Black);
resizableRichTextBoxCtrl_1.AppendText("A1", Color.Blue);
resizableRichTextBoxCtrl_1.AppendText("+", Color.Black);
resizableRichTextBoxCtrl_1.AppendText("b3", Color.Orange);
resizableRichTextBoxCtrl_1.AppendText("+sum", Color.Black);
resizableRichTextBoxCtrl_1.AppendText("h2:i4", Color.Red);



Niciun comentariu:

Trimiteți un comentariu