Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / Windows Forms

ColorTextBox

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
25 Mar 2007Apache2 min read 149.3K   3.4K   81  
This article describes ColorTextBox, a customizable User Control which was written completely from scratch and is intended to fill the gap between the TextBox and RichTextBox controls found in the .NET 2.0 library.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ColorTextBoxControl;

namespace ColorTextBoxDemo {
    public partial class ColorTextBoxDemo2 : Form {
        public ColorTextBoxDemo2() {
            InitializeComponent();
            colorTextBox1.CaretChanged += new CaretEventHandler(colorTextBox1_CaretChanged);
            colorDialog1.Color = Color.Black;
            this.AcceptButton = button1;
        }

        void colorTextBox1_CaretChanged(object source, CaretEventArgs args) {
            if (args.Type == CaretEventType.Changed) {
                if (colorTextBox1.SelectionLength == 0) {
                    fromLine.Text = toLine.Text = colorTextBox1.GetLineNumber(args.NewPos.ILine).ToString();
                    toPos.Text = fromPos.Text = args.NewPos.LinePos.ToString();
                }
                else {
                    IPosition from = colorTextBox1.SelectionFromPos;
                    IPosition to = colorTextBox1.SelectionToPos;
                    fromLine.Text = colorTextBox1.GetLineNumber(from.ILine).ToString();
                    fromPos.Text = from.LinePos.ToString();
                    toLine.Text = colorTextBox1.GetLineNumber(to.ILine).ToString();
                    toPos.Text = to.LinePos.ToString();
                }
            }
        }

        private void insert_Click(object sender, EventArgs e) {
            colorTextBox1.SelectedText = text.Text;
        }

        private void setColor_Click(object sender, EventArgs e) {
            colorTextBox1.SelectionColor = colorDialog1.Color;
            colorTextBox1.Focus();
        }

        private void setCaretSelection_Click(object sender, EventArgs e) {
            //Position from = new Position(colorTextBox1.GetLineAt(
            Position from = getPos(fromLine, fromPos);
            Position to = getPos(toLine, toPos);
            if (from != null && to != null) {
                if (from.Equals(to)) colorTextBox1.Caret = from;
                else colorTextBox1.Select(from, to);
            }
            else if (from == null) {
                if (to == null) MessageBox.Show("From and To position are invalid!");
                else MessageBox.Show("From position is invalid!");
            }
            else MessageBox.Show("To position is invalid!");
        }

        private void selectColor_Click(object sender, EventArgs e) {
            DialogResult res = colorDialog1.ShowDialog(this);
            if (res == DialogResult.OK || res == DialogResult.Yes) {
                colorTextBox1.SelectionColor = colorDialog1.Color;
                colLabel.BackColor = colorDialog1.Color;
            }
        }

        private Position getPos(ColorTextBox lb, ColorTextBox pb) {
            int l, p;
            if (Int32.TryParse(lb.Text, out l) &&
                Int32.TryParse(pb.Text, out p)) {
                Line line = colorTextBox1.GetLineAt(l);
                if (line != null && p >= 0 && p <= line.LastPos) {
                    return new Position(line, p);
                }
            }
            return null;
        }

        private void delete_Click(object sender, EventArgs e) {
            if (colorTextBox1.SelectionLength == 0) {
                if (colorTextBox1.Caret != null) {
                    colorTextBox1.Delete(colorTextBox1.Caret, -1);
                }
                else MessageBox.Show("There is currently no text selected and no caret set!");
            }
            else {
                colorTextBox1.SelectedText = String.Empty;
            }
        }

        private void insertNewline_Click(object sender, EventArgs e) {
            colorTextBox1.Insert("\r\n");
        }

        private void colLabel_Click(object sender, EventArgs e) {
            DialogResult res = colorDialog1.ShowDialog(this);
            if (res == DialogResult.OK || res == DialogResult.Yes) {
                colorTextBox1.SelectionColor = colorDialog1.Color;
                colLabel.BackColor = colorDialog1.Color;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions