|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.