VB.NET Programming

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

ZerosKD 2020. 8. 31. 19:23
반응형

* VBNET richTextBox 내용 - 문자열 검색

 

메인화면

 

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

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Dim iFindStartIndex As Integer = 0

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        '찾는 문자열 길이
        Dim iFindLength As Integer = textBox1.Text.Length
        iFindStartIndex = FindMyText(textBox1.Text, iFindStartIndex, richTextBox1.Text.Length)
        If iFindStartIndex = -1 Then
            iFindStartIndex = 0
            Return
        End If

        '찾은 문자열 선택해서 붉은색으로 바꾸기
        richTextBox1.SelectionColor = Color.Red
        richTextBox1.Select(iFindStartIndex, iFindLength)

        '다음 찾기를 위해 찾은 문자열 위치 저장
        iFindStartIndex += iFindLength
    End Sub

    Private Function FindMyText(ByVal strSearchText As String, _
                                ByVal iSearchStart As Integer, _
                                ByVal iSearchEnd As Integer) As Integer

        ' Initialize the return value to false by default.
        Dim ReturnValue As Integer = -1

        ' Ensure that a search string and a valid starting point are specified.
        If strSearchText.Length > 0 And iSearchStart >= 0 Then
            ' Ensure that a valid ending value is provided.
            If iSearchEnd > iSearchStart Or iSearchEnd = -1 Then
                ' Obtain the location of the search string in richTextBox1.
                Dim indexToText As Integer = richTextBox1.Find(strSearchText, iSearchStart, iSearchStart, RichTextBoxFinds.MatchCase)
                ' Determine whether the text was found in richTextBox1.
                If indexToText >= 0 Then
                    ' Return the index to the specified search text.
                    ReturnValue = indexToText
                End If
            End If
        End If

        Return ReturnValue

    End Function

End Class

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

 

* 예제 결과

 

 

richTextBox1.SelectionColor = Color.Red; 

richTextBox1.Select(iFindStartIndex, iFindLength);

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


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

 

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

* C# richTextBox 내용 - 문자열 검색 - 사용한 컨트롤: Panel 3개, Label 1개, TextBox 1개, Button 1개, richTextBox 1개 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using Sys..

kdsoft-zeros.tistory.com

 

반응형