Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

SqlLighter Control
By Shripad Kulkarni

Soruce Code :Download
Executable : Download
Windows forms provides us with the RichTextBox that provides us with rich editing featues like colors , fonts and other formatting options.
The SqlLighter control is just an extension of using this windows control to beautify a SQL statament that I developed as part of an existing project. This project is implemented as a "Windows Control Library".
Although I have covered most of the keywords, operators , operands that can be highlighted, the control has additional flexibility to add any other keywords that you wish to highlight using public functions like.

 

 

  • AddSpecial(string skey)
  • AddOperators(string skey)
  • AddOperands(string skey)
  • AddKeyWords(string skey)
  • public void ClearOperators()
  • public void ClearOperands()
  • public void ClearKeyWords()
The important event to look for here is the TextChange event sent by the RichTextBox.
		private void RichSQLBox_TextChanged(object sender, System.EventArgs e)
		{
			PROCESS();
		}
Execute the Process function to check for text you entered and process accordingly
		private void PROCESS()
		{
			LockWindowUpdate(RichSQLBox.Handle);

			int selStart = RichSQLBox.SelectionStart ;

			string[] s = RichSQLBox.Text.Split(null);
			int pos = 0 ;
			bool found = false ;
			string special_str = "";
			int special_str_pos = -1;
			for ( int i=0; i < s.Length ; i++)
			{
				string orig = s[i];
				string new_str = s[i].ToUpper();
				int len = new_str.Length ; 
				new_str = new_str.Trim();
				if ( new_str == "" ) continue ;

				pos = RichSQLBox.Text.IndexOf(orig,pos);
				found = true;
				if ( SearchKeyWords(new_str) ) 
				{
					Adjust(pos , new_str.Length , Color.Blue , kFont );
				}
				else
				if ( SearchOperands(new_str) ) 
				{
					Adjust(pos , new_str.Length , Color.Red, oprFont );
				}
				else 
					if ( SearchOperators(new_str))
				{
					Adjust(pos , new_str.Length , 
                                     Color.BlueViolet , oprtFont);
				}
				else
					if ( IsNumber(new_str))
				{
					Adjust(pos , new_str.Length , 
                           Color.DarkGreen, RichSQLBox.Font );
				}
				else
				if ( SearchSpecial(new_str , ref special_str 
                             , ref special_str_pos))
				{
					Adjust(pos + special_str_pos 
                         , special_str.Length , Color.IndianRed, spcFont );
				}
				else
				{
					RichSQLBox.SelectionStart = pos;
					RichSQLBox.SelectionLength = new_str.Length;
					RichSQLBox.SelectionColor = Color.Black;
					RichSQLBox.SelectionFont = RichSQLBox.Font ;
		
				}
				pos += new_str.Length ; 
			}
			if ( found ) 
			{
				string text = RichSQLBox.SelectedText;
				RichSQLBox.SelectionStart = RichSQLBox.Text.Length ; 
				RichSQLBox.SelectionLength = 0;			
			}
			RichSQLBox.SelectionColor = Color.Black;
			RichSQLBox.SelectionFont = RichSQLBox.Font ;
			LockWindowUpdate(IntPtr.Zero);

			RichSQLBox.SelectionStart = selStart;
			RichSQLBox.SelectionLength = 0;			
		}