SqlLighter Control

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;
}

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!