C# Programming

[C#] [Control] richTextBox - 문자열 검색

ZerosKD 2020. 8. 17. 10:03
반응형

* C# richTextBox 내용 - 문자열 검색

 

메인화면

 

- 사용한 컨트롤: Panel 3개, Label 1개, TextBox 1개, Button 1개, richTextBox 1개

 

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSharp_TextBoxSearch
{
    public partial class Form1 : Form
    {
        int iFindStartIndex = 0;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);


            richTextBox1.Text = @"private void button1_Click(object sender, EventArgs e)
                                    {
                                        //찾는 문자열 길이
                                        int iFindStartIndex = 0;
                                        int iFindLength = textBox1.Text.Length;
                                        iFindStartIndex = FindMyText(textBox1.Text, iFindStartIndex, richTextBox1.Text.Length);
                                        if (iFindStartIndex == -1)
                                        {
                                            iFindStartIndex = 0;
                                            return;
                                        }
                                        
                                        richTextBox1.SelectionColor = Color.Red;
                                        richTextBox1.Select(iFindStartIndex, iFindLength);
                                        iFindStartIndex += iFindLength;
                                    }

                                    private int FindMyText(string searchText, int searchStart, int searchEnd)
                                    {
                                        // Initialize the return value to false by default.
                                        int returnValue = -1;

                                        // Ensure that a search string and a valid starting point are specified.
                                        if (searchText.Length > 0 && searchStart >= 0)
                                        {
                                            // Ensure that a valid ending value is provided.
                                            if (searchEnd > searchStart || searchEnd == -1)
                                            {
                                                // Obtain the location of the search string in richTextBox1.
                                                int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
                                                // Determine whether the text was found in richTextBox1.
                                                if (indexToText >= 0)
                                                {
                                                    // Return the index to the specified search text.
                                                    returnValue = indexToText;
                                                }
                                            }
                                        }

                                        return returnValue;
                                    }";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //찾는 문자열 길이
            int iFindLength = textBox1.Text.Length;
            iFindStartIndex = FindMyText(textBox1.Text, iFindStartIndex, richTextBox1.Text.Length);
            if (iFindStartIndex == -1)
            {
                iFindStartIndex = 0;
                return;
            }
            
            //찾은 문자열 선택해서 붉은색으로 바꾸기
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.Select(iFindStartIndex, iFindLength);

            //다음 찾기를 위해 찾은 문자열 위치 저장
            iFindStartIndex += iFindLength;
        }

        private int FindMyText(string searchText, int searchStart, int searchEnd)
        {
            // Initialize the return value to false by default.
            int returnValue = -1;

            // Ensure that a search string and a valid starting point are specified.
            if (searchText.Length > 0 && searchStart >= 0)
            {
                // Ensure that a valid ending value is provided.
                if (searchEnd > searchStart || searchEnd == -1)
                {
                    // Obtain the location of the search string in richTextBox1.
                    int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
                    // Determine whether the text was found in richTextBox1.
                    if (indexToText >= 0)
                    {
                        // Return the index to the specified search text.
                        returnValue = indexToText;
                    }
                }
            }

            return returnValue;
        }
    }
}

 

 

마이크로 소프트 msdn 참조로 richTextBox 내용에 원하는 문자열 검색 구현

 

* 예제 결과

 

 

richTextBox1.SelectionColor = Color.Red; 

richTextBox1.Select(iFindStartIndex, iFindLength);

예제 결과 보듯이 찾은 문자열을 붉은색으로 바꿔 표시 됩니다.

 

https://kdsoft-zeros.tistory.com/221

 

[VBNET] [Control] richTextBox - 문자열 검색

* VBNET richTextBox 내용 - 문자열 검색 메인화면 - 사용한 컨트롤: Panel 3개, Label 1개, TextBox 1개, Button 1개, richTextBox 1개 전체 소스 코드 Form1.vb Public Class Form1 Dim iFindStartIndex As Int..

kdsoft-zeros.tistory.com

 

반응형