导航

RichTextBox 关键字 显示颜色

Posted on 2011-09-08 17:57  DotNet1010  阅读(526)  评论(0编辑  收藏  举报

    List<string> KeyWordList = new List<string> { };

        private void InitKeyWordList()
        {
            KeyWordList.Add("SELECT");
            KeyWordList.Add("TOP");
            KeyWordList.Add("*");
            KeyWordList.Add("FROM");
            KeyWordList.Add("JOIN");
            KeyWordList.Add("ON");
            KeyWordList.Add("WHERE");
            KeyWordList.Add("GROUP");
            KeyWordList.Add("BY");
            KeyWordList.Add("HAVING");
            KeyWordList.Add("ORDER");

            KeyWordList.Add("NULL");
            KeyWordList.Add("DISTINCT");
            KeyWordList.Add("INNER");
            KeyWordList.Add("LEFT");
            KeyWordList.Add("RIGHT");
            KeyWordList.Add("FULL");
            KeyWordList.Add("UNION");
            KeyWordList.Add("ALL");
            KeyWordList.Add("ASC");
            KeyWordList.Add("DESC");
        }

        [DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam); private const int WM_SETREDRAW = 0xB;

        private void tbxSelectText_TextChanged(object sender, EventArgs e)
        {

            RichTextBox richBox = sender as RichTextBox;
            if(richBox.Text==string.Empty)return;

            char[] charArray = new char[] { ' ','\r','\n'};
            List<char> listCharArray = new List<char>();
            listCharArray.AddRange(charArray);

            int currentIndex = richBox.SelectionStart;

            string strSQLText = richBox.Text;
            richBox.SelectionColor = System.Drawing.Color.Black;
        
            char[] sqlChar= strSQLText.ToCharArray();
            int intStart = 0;
            int intEnd = 0;

            SendMessage(base.Handle, WM_SETREDRAW, 0, IntPtr.Zero);

            try
            {
                #region -------

                for (int i = 0; i < sqlChar.Length; i++)
                {
                    if (sqlChar[i] == ' ' || sqlChar[i] == '\n') continue;

                    intStart = i;

                    for (int j = intStart + 1; j < sqlChar.Length; j++)
                    {
                        if (sqlChar[j] != ' ' && sqlChar[j] != '\n') continue;

                        intEnd = j;

                        string strKeyWord = strSQLText.Substring(intStart, intEnd - intStart);

                        if (KeyWordList.Contains(strKeyWord.ToUpper()))
                        {
                            richBox.Select(intStart, intEnd - intStart);
                            richBox.SelectionColor = System.Drawing.Color.Blue;
                            richBox.SelectionFont = new System.Drawing.Font(richBox.SelectionFont, FontStyle.Bold);
                        }
                        else if (strKeyWord.ToLower().StartsWith("dbo."))
                        {
                            richBox.Select(intStart, intEnd - intStart);
                            richBox.SelectionColor = System.Drawing.Color.OrangeRed;
                            richBox.SelectionFont = new System.Drawing.Font(richBox.SelectionFont, FontStyle.Bold);
                        }
                        else
                        {
                            richBox.Select(intStart, intEnd - intStart);
                            richBox.SelectionColor = System.Drawing.Color.Black;
                            richBox.SelectionFont = new System.Drawing.Font(richBox.SelectionFont, FontStyle.Regular);
                        }

                        i = j;

                        break;
                    }
                }

                richBox.SelectionStart = currentIndex;
                richBox.SelectionLength = 0;

                #endregion

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Source);
            }
            finally
            {
                SendMessage(base.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
            }

            richBox.Refresh();
        }