HTMLタグと通常のテキストを色分けするには?

解決


kmknkmkn  2007-10-02 05:29:14  No: 137674

はじめまして、kmknkmknといいます。
ひとつ質問させてください。
今、HTMLエディタを作成中なのですが、
タグと通常の文字を区別するために色分けをしたいと
思っています。
タグに色を付けるところまではいったのですが、
タグの中身すべてに色が付いてしまいます。
ダブルクオーテーション(")内の文字は別の色にしたいのですが、
そのように記述してもそうなてくれません。
下がそのスクリプトです。

        Dim s As String
        Dim n As Long
        Dim t As Long
        s = RichTextBox1.Text
        RichTextBox1.Text = s
        RichTextBox1.SelectionStart = 0
        RichTextBox1.SelectionLength = Len(s)
        RichTextBox1.SelectionColor = Color.Blue

        For n = 1 To Len(s)
            If Mid(s, n, 1) = "<" Then
                RichTextBox1.SelectionStart = n
                t = n
            ElseIf Mid(s, n, 1) = ">" Then
                RichTextBox1.SelectionLength = n - t - 1
                RichTextBox1.SelectionColor = Color.Red
            End If
        Next n
        For n = 1 To Len(s)
            If Mid(s, n, 1) = """" Then
                RichTextBox1.SelectionStart = n
                t = n
            ElseIf Mid(s, n, 1) = """" Then
                RichTextBox1.SelectionLength = n - t - 1
                RichTextBox1.SelectionColor = Color.Green
            End If
        Next n
        RichTextBox1.SelectionStart = pos
        RichTextBox1.SelectionLength = 0
        RichTextBox2.Text = RichTextBox1.Rtf

素人の質問ですみません。
どうぞよろしくお願いします。


我龍院  2007-10-02 22:06:38  No: 137675

これはいったいVB6以前なのかしら。
VB6以前なら、
        Dim s As String
        Dim n As Long
        Dim t As Long
        s = RichTextBox1.Text
        RichTextBox1.SelStart = 0
        RichTextBox1.SelLength = Len(s)
        RichTextBox1.SelColor = vbBlue

        For n = 1 To Len(s)
            If Mid(s, n, 1) = "<" Then
                RichTextBox1.SelStart = n
                t = n
            ElseIf Mid(s, n, 1) = ">" Then
                RichTextBox1.SelLength = n - t - 1
                RichTextBox1.SelColor = vbRed
            End If
        Next n
        t = 0
        For n = 1 To Len(s)
            If Mid(s, n, 1) = """" And t = 0 Then
                RichTextBox1.SelStart = n
                t = n
            ElseIf Mid(s, n, 1) = """" And t <> 0 Then
                RichTextBox1.SelLength = n - t - 1
                RichTextBox1.SelColor = vbGreen
            End If
        Next n
      
        RichTextBox1.SelLength = 0
        RichTextBox2.Text = RichTextBox1.TextRTF

こうかな。


我龍院  2007-10-02 23:43:09  No: 137676

どう見てもVB.NETかな。
        Dim strText As String = RichTextBox1.Text
        Dim intStart As Integer = 0
        Dim intEnd As Integer = 0
        Dim strStartTag() As String = {"<", """"}
        Dim strEndTag() As String = {">", """"}
        Dim colTag() As Color = {Color.Red, Color.Green}

        RichTextBox1.ForeColor = Color.Blue
        For i As Integer = 0 To strStartTag.Length - 1
            intStart = 0
            intEnd = 0
            Do
                intStart = InStr(intStart + 1, strText, strStartTag(i))
                If intStart <> 0 Then
                    intEnd = InStr(intStart + 1, strText, strEndTag(i))
                    If intEnd <> 0 Then
                        RichTextBox1.SelectionStart = intStart
                        RichTextBox1.SelectionLength = intEnd - intStart - 1
                        RichTextBox1.SelectionColor = colTag(i)
                        intStart = intEnd
                    End If
                End If
            Loop While (intStart <> 0 And intEnd <> 0)
        Next
        RichTextBox1.SelectionStart = 0
        RichTextBox1.SelectionLength = 0
        RichTextBox2.Text = RichTextBox1.Rtf


kmknkmkn  2007-10-03 03:46:36  No: 137677

ありがとうございます。
ちなみに、開発環境はVB2005です。
重要なことを記述し忘れてすみませんでした。
我龍院さん、解決しました。
ありがとうございました。


ひろこ  2007-10-04 09:06:49  No: 137678

>我龍院さん
VB6のコードを実行したら、文章が途中から緑一色になりました。
>               RichTextBox1.SelColor = vbGreen
の後に
               t = 0
が必要だと思われます。


我龍院  2007-10-04 18:32:08  No: 137679

>VB6のコードを実行したら、文章が途中から緑一色になりました。
バグですね。(^^;
いずれにしろ1文づつなめて行くのはじかんがっかるので、 
        Dim strText As String
        Dim intStart As Integer
        Dim intEnd As Integer
        Dim strTag(1, 1) As String
        Dim vbColor(2) As ColorConstants
        
        strText = RichTextBox1.Text
        intStart = 0
        intEnd = 0
        
        strTag(0, 0) = "<"
        strTag(0, 1) = ">"
        strTag(1, 0) = """"
        strTag(1, 1) = """"
        
        vbColor(0) = vbRed
        vbColor(1) = vbGreen
        vbColor(2) = vbBlue
        
        RichTextBox1.SelStart = 0
        RichTextBox1.SelLength = Len(RichTextBox1.Text)
        RichTextBox1.SelColor = vbColor(2)

        Dim i As Integer
        
        For i = 0 To UBound(strTag)
            intStart = 0
            intEnd = 0
            Do
                intStart = InStr(intStart + 1, strText, strTag(i, 0))
                If intStart <> 0 Then
                    intEnd = InStr(intStart + 1, strText, strTag(i, 1))
                    If intEnd <> 0 Then
                        RichTextBox1.SelStart = intStart
                        RichTextBox1.SelLength = intEnd - intStart - 1
                        RichTextBox1.SelColor = vbColor(i)
                        intStart = intEnd
                    End If
                End If
            Loop While (intStart <> 0 And intEnd <> 0)
        Next
        RichTextBox1.SelStart = 0
        RichTextBox1.SelLength = 0
        RichTextBox2.Text = RichTextBox1.TextRTF
これでどうでしょう。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加