#region 필드
        /// <summary>
        /// 다음 단어를 찾을 때 Index 위치
        /// </summary>
        int seekIndex = 0;
        #endregion

#region public method
        /// <summary>
        /// 해당 키워드들을 원하는 Color로 지정해줍니다.
        /// </summary>
        /// <param name="words"></param>
        /// <param name="color"></param>
        public void SetColorWords(string []words, System.Drawing.Color color)
        {
            for (int i = 0; i < words.Length; i++)
            {
                string word = words[i];
                while (true)
                {
                    int findIndex = 0;

                    findIndex = NextFind(word);

                    //검색된 것이 없다면
                    if (findIndex == -1)
                        break;

                    int result = this.Find(word, findIndex, findIndex + word.Length, RichTextBoxFinds.MatchCase);
                    if (result == -1)
                        break;

                    this.SelectionColor = color;
                    this.DeselectAll();
                    this.SelectionColor = System.Drawing.Color.Black;
                }
                this.seekIndex = 0;
            }
            this.SelectionLength = 0;
        }
        #endregion
        /// <summary>
        /// seekIndex부터 word에 해당하는 시작되는 Index 반환
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private int NextFind(string word)
        {
            int i = 0;
            for (i = seekIndex; i < this.Text.Length; i++)
            {
                int wordIndex = 0;
                while (true)
                {
                    if (this.Text[i] == word[wordIndex])
                    {
                        i++;
                        wordIndex++;
                        if (wordIndex == word.Length)
                        {
                            this.seekIndex = i;
                            return i - word.Length;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            this.seekIndex = i;
            return -1;
        }

 

 

결과 화면

 

 

 

 

 

TestRichTextBox.zip

 

 

'.Net > Winform' 카테고리의 다른 글

C# Keyboard, Mouse Hooking  (0) 2012.06.28
C# 지정한 폴더 감시하기  (0) 2012.06.27
RichTextBox Keyword 비쥬얼 스튜디오 효과내기  (0) 2012.05.10
C# WndProc이용, USB인식 및 해제 감시  (0) 2011.12.07
C# FileSystemWatcher  (3) 2011.12.07

+ Recent posts