zoukankan      html  css  js  c++  java
  • RichTextBox 关键字 显示颜色

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

  • 相关阅读:
    nginx socket转发设置
    Linux CentOS 7 安装字体库 & 中文字体
    nginx配置location总结及rewrite规则写法
    nginx动静分离小示例
    iptables黑/白名单设置(使用ipset 工具)
    Docker logs 命令
    Docker定制容器镜像(利用Dockerfile文件)
    docker swarn集群笔记
    [国家集训队]数颜色 / 维护队列(带修莫队)
    于是他错误的点名开始了(trie树)
  • 原文地址:https://www.cnblogs.com/DotNet1010/p/2171691.html
Copyright © 2011-2022 走看看