zoukankan      html  css  js  c++  java
  • C# richTextBox中部分關鍵字加亮顯示

    在RichTextBox中部分關鍵字加亮顯示的方法: 
    使用TextChange事件,循環查找RichTextBox中符合條件的值,加亮顯示。
    (如果只加亮顯示一次,只是IndexOf查找,如果有重復出現,且都有加亮顯示,則用LastIndexof查找。)

    請看原代碼。

            //使用TextChange事件,進行加亮顯示(rtbCommand為RichTextBox控件)
            rtbCommand.TextChanged += new EventHandler(rtbCommand_TextChanged);
            
            
            
            
    /// <summary>
            
    /// 加亮顯示
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            void rtbCommand_TextChanged(object sender, EventArgs e)
            {
                
                Font fBeforeFont 
    = rtbCommand.Font;
                Color cBeforeColor 
    = rtbCommand.ForeColor;

                
    //定義命的關鍵字
                List<string> lsCommand = new List<string>();
                lsCommand.Add(
    "ping");
                lsCommand.Add(
    "ipconfig");
                lsCommand.Add(
    "netstat");
                lsCommand.Add(
    "nbtstat");
                lsCommand.Add(
    "ftp");
                lsCommand.Add(
    "tracert");
                lsCommand.Add(
    "pathping");
                lsCommand.Add(
    "nslookup");
                lsCommand.Add(
    "arp");
                lsCommand.Add(
    "net");
                lsCommand.Add(
    "netsh");
                lsCommand.Add(
    "interface");


                
    //定義參數啟始關鍵字
                List<string> lsCmdParameter = new List<string>();
                lsCmdParameter.Add(
    "/");
                lsCmdParameter.Add(
    "-");

                
    //定義啟始索為-1,表示沒有找到
                int iComPraIndex = -1;

                
    //循環所有參數的關鍵,看RichTextBox中是否含有關鍵字
                for (int ICmdPra = 0; ICmdPra < lsCmdParameter.Count; ICmdPra++)
                {
                    
    //索引為最后一個找的字符
                    iComPraIndex = rtbCommand.Text.ToLower().LastIndexOf(lsCmdParameter[ICmdPra]);
                    
    if (iComPraIndex>=0)
                    {
                        
    break;
                    }                
                }
                
    //如果是,就一定不是命令;否則就要判斷是不是命令關鍵字
                if (iComPraIndex >= 0)
                {
                    
    //如果找到,剛后兩位加粗,並變色。
                    rtbCommand.Select(iComPraIndex, 2);
                    rtbCommand.SelectionColor 
    = Color.Green;
                    rtbCommand.SelectionFont 
    = new Font(rtbCommand.Font, rtbCommand.Font.Style | FontStyle.Bold);

                    
    //返回成原樣
                    rtbCommand.Select(rtbCommand.Text.Length, 0);
                    rtbCommand.SelectionFont 
    = fBeforeFont;
                    rtbCommand.SelectionColor 
    = cBeforeColor;
                }
                
    else
                {
                    
    int iIndex = -1;
                    
    int iSelectLenght = 0;
                    
    string sCommmand = rtbCommand.Text;

                    
    //循環所有命令的關鍵,看RichTextBox中是否含有關鍵字
                    for (int ilist = 0; ilist < lsCommand.Count; ilist++)
                    {
                        iIndex 
    = rtbCommand.Text.ToLower().IndexOf(lsCommand[ilist]);
                        
    if (iIndex >= 0)
                        {
                            
    //得到關鍵字的長度
                            iSelectLenght = lsCommand[ilist].Length;

                            
    ////如果找到,命令加粗,並變色。
                            rtbCommand.Select(iIndex, iSelectLenght);
                            rtbCommand.SelectionColor 
    = Color.Blue;
                            rtbCommand.SelectionFont 
    = new Font(rtbCommand.Font, rtbCommand.Font.Style | FontStyle.Bold);
                            
                            
    //返回成原樣
                            rtbCommand.Select(rtbCommand.Text.Length, 0);
                            rtbCommand.SelectionFont 
    = fBeforeFont;
                            rtbCommand.SelectionColor 
    = cBeforeColor;
                            
    break;
                        }
                    }

                }

            }

    注:上邊代碼中。使用Indexof查找時,每輸入一個字符,IndexOf查找出來的值都會重新加亮顯示,此部分需要改進。
  • 相关阅读:
    统计学习方法 | 第1章 统计学习方法概论
    统计学习方法 | 第2章 感知机
    LeetCode | DP专题详解
    论文阅读 | Towards a Robust Deep Neural Network in Text Domain A Survey
    DFS(深度优先搜索)和BFS(广度优先搜索)
    Analysis Methods in Neural Language Processing: A Survey
    Spring AMQP 发送消息到 RabbitMQ 收到 x-queue-type 错误
    Gradle 的项目导入到 IntelliJ 后子项目源代码不能导入
    在 CentOS 7 上安装 RabbitMQ
    IntelliJ IDEA 运行项目的时候提示 Command line is too long 错误
  • 原文地址:https://www.cnblogs.com/scottckt/p/1131783.html
Copyright © 2011-2022 走看看