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

CodeBox for Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
12 Nov 2009CPOL11 min read 83.3K   2.4K   99  
A RichTextBox for Windows Forms that supports flexible highlighting and background coloring.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinFormsColoredWordPad;
using WinFormsCodeBox.Decorations;
namespace WinFormsColoredWordPad
{
    public partial class TextDocument : Form
    {
        public TextDocument()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
            cmbDecorationScheme.SelectedIndex = 0;
        }

    

        private void cmbDecorationScheme_SelectedIndexChanged(object sender, EventArgs e)
        {
             switch (cmbDecorationScheme.SelectedIndex)
            {

                case 0:
                    codeBox.DecorationScheme = null;
                    break;
                case 1:
                    codeBox.DecorationScheme = WinFormsCodeBox.Decorations.DecorationSchemes.CSharp3;
                    break;

                case 2:
                    codeBox.DecorationScheme = WinFormsCodeBox.Decorations.DecorationSchemes.Xml;
                    break;

            }
        }

        private void hiliteLineToolStripMenuItem_Click(object sender, EventArgs e)
        {   codeBox.Decorations.Clear();
            Dialogs.DiaLineNumbers dia = new  Dialogs.DiaLineNumbers();
            if (dia.ShowDialog() == DialogResult.OK)
            {
                if (dia.LineNumbers.Count > 0)
                {

                    if (dia.LineNumbers.Count == 1)
                    {
                        LineDecoration ld = new LineDecoration()
                        {
                            DecorationType = WinFormsCodeBox.Decorations.EDecorationType.Hilight,
                            Color = Color.Yellow,
                            Line = dia.LineNumbers[0]
                        };
                        codeBox.Decorations.Add(ld);
                    }
                    else
                    {
                        MultiLineDecoration ld = new MultiLineDecoration()
                        {
                            DecorationType = WinFormsCodeBox.Decorations.EDecorationType.Hilight,
                            Color = Color.Yellow,
                            Lines = dia.LineNumbers 
                        };
                        codeBox.Decorations.Add(ld);
                        
                    }

                }
            }

          codeBox.ApplyDecorations(); 
                 
        }

        private void codeBox_SelectionChanged(object sender, EventArgs e)
        {
            lblTextIndex.Text = codeBox.SelectionStart +","  + codeBox.SelectionLength ;
            
        }

    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

Comments and Discussions