Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C#

CodeDom Assistant

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
21 Sep 20074 min read 140K   6.6K   82  
Generating CodeDom Code By Parsing C# or VB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ScintillaNet
{
	public partial class IncrementalSearcher : UserControl
	{
		private Scintilla _scintilla;
		public Scintilla Scintilla
		{
			get
			{
				return _scintilla;
			}
			set
			{
				_scintilla = value;
			}
		}

		public IncrementalSearcher()
		{
			InitializeComponent();
		}

		protected override void OnLeave(EventArgs e)
		{
			base.OnLostFocus(e);

			Hide();			
		}

		protected override void OnVisibleChanged(EventArgs e)
		{
			base.OnVisibleChanged(e);

			txtFind.Text = string.Empty;
			txtFind.BackColor = SystemColors.Window;

			moveFormAwayFromSelection();

			if (Visible)
				txtFind.Focus();
			else
				Scintilla.Focus();
		}

		protected override void OnCreateControl()
		{
			base.OnCreateControl();
			moveFormAwayFromSelection();
			txtFind.Focus();
		}


		private void txtFind_TextChanged(object sender, EventArgs e)
		{
			txtFind.BackColor = SystemColors.Window;
			if (txtFind.Text == string.Empty)
				return;

			int pos = Math.Min(Scintilla.Caret.Position, Scintilla.Caret.Anchor);
			Range r = Scintilla.FindReplace.Find(pos, Scintilla.TextLength, txtFind.Text, Scintilla.FindReplace.Window.GetSearchFlags());
			if (r == null)
				r = Scintilla.FindReplace.Find(0, pos, txtFind.Text, Scintilla.FindReplace.Window.GetSearchFlags());

			if (r != null)
				r.Select();
			else
				txtFind.BackColor = Color.Tomato;

			moveFormAwayFromSelection();
		}

		private void btnNext_Click(object sender, EventArgs e)
		{
			findNext();
		}

		private void findNext()
		{
			if (txtFind.Text == string.Empty)
				return;

			Range r = Scintilla.FindReplace.FindNext(txtFind.Text, true, Scintilla.FindReplace.Window.GetSearchFlags());
			if (r != null)
				r.Select();

			moveFormAwayFromSelection();
		}

		private void brnPrevious_Click(object sender, EventArgs e)
		{
			findPrevious();
		}

		private void findPrevious()
		{
			if (txtFind.Text == string.Empty)
				return;

			Range r = Scintilla.FindReplace.FindPrevious(txtFind.Text, true, Scintilla.FindReplace.Window.GetSearchFlags());
			if (r != null)
				r.Select();

			moveFormAwayFromSelection();
		}

		private void txtFind_KeyDown(object sender, KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.Enter:
				case Keys.Down:
					findNext();
					e.Handled = true;
					break;
				case Keys.Up:
					findPrevious();
					e.Handled = true;
					break;
				case Keys.Escape:
					Hide();
					break;
			}
		}

		private void btnHighlightAll_Click(object sender, EventArgs e)
		{
			if (txtFind.Text == string.Empty)
				return;

			Scintilla.FindReplace.HighlightAll(Scintilla.FindReplace.FindAll(txtFind.Text));
		}

		private void btnClearHighlights_Click(object sender, EventArgs e)
		{
			Scintilla.FindReplace.ClearAllHighlights();
		}

		public void moveFormAwayFromSelection()
		{
			if (!Visible)
				return;

			int pos = Scintilla.Caret.Position;
			int x = Scintilla.PointXFromPosition(pos);
			int y = Scintilla.PointYFromPosition(pos);

			Point cursorPoint = new Point(x, y);

			Rectangle r = new Rectangle(Location, Size);
			if (r.Contains(cursorPoint))
			{
				Point newLocation;
				if (cursorPoint.Y < (Screen.PrimaryScreen.Bounds.Height / 2))
				{
					// Top half of the screen
					newLocation = new Point(Location.X, cursorPoint.Y + Scintilla.Lines.Current.Height * 2);
						
				}
				else
				{
					// Bottom half of the screen
					newLocation = new Point(Location.X, cursorPoint.Y - Height - (Scintilla.Lines.Current.Height * 2));
				}
				
				Location = newLocation;
			}
		}

	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions