通过 ms access table 数据循环到 richtextbox vb.net 时格式不正确

getting incorrect formating when looping through ms access table data to richtextbox vb.net

我有一个 table 存储段落位置、段落类型和段落文本的地方

此信息被发送到富文本框。

到目前为止一切顺利

根据存储在 table 中的段落类型对段落文本进行格式化。

这里开始变得混乱

如果段落文本重复,我的代码会更改格式。它会对每个段落类型执行此操作。我不明白为什么会这样。

这里有什么优惠!!!?

提前感谢您抽出时间!

        For i = 1 To ds.Tables("FoundSelection").Rows.Count - 1

        rtbScriptRTF.SelectionStart = rtbScriptRTF.Text.IndexOf(rtbScriptRTF.Lines(i))
        rtbScriptRTF.SelectionLength = rtbScriptRTF.Lines(i).Length
        'MsgBox(rtbScriptRTF.SelectedText.ToString)

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 1 Then 'Action!!
            rtbScriptRTF.SelectionColor = Color.Black

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 2 Then 'Dialogue!!
            rtbScriptRTF.SelectionColor = Color.DarkOliveGreen
            rtbScriptRTF.SelectionIndent = (rtbScriptRTF.Width / 3)
            rtbScriptRTF.SelectionRightIndent = 25

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 3 Then 'Cue!!
            rtbScriptRTF.SelectionColor = Color.Navy
            rtbScriptRTF.SelectionIndent = (rtbScriptRTF.Width / 2)

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 4 Then 'Parenthesis!!
            rtbScriptRTF.SelectionColor = Color.DarkOliveGreen

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 5 Then 'Transition IN!!
            rtbScriptRTF.SelectionColor = Color.Black
        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 6 Then 'Transition OUT!!
            rtbScriptRTF.SelectionColor = Color.Black
            rtbScriptRTF.SelectionAlignment = HorizontalAlignment.Right

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 7 Then 'SLUGLINE!!
            rtbScriptRTF.SelectionColor = Color.DarkRed

        End If

        If (ds.Tables("FoundSelection").Rows(i).Item("Paragraphtype")) = 8 Then 'ACT!!
            rtbScriptRTF.SelectionBackColor = Color.LightGray
            rtbScriptRTF.SelectionColor = Color.Black

        End If
    Next

这是因为您正在使用 IndexOf。这将找到指定文本的第一个实例。不过,您无需查找任何内容的索引。在添加新文本之前和之后获取 TextLength,这是您的起点和终点。

Dim table = ds.Tables("FoundSelection")

For Each row As DataRow In table.Rows
    Dim selectionStart = rtbScriptRTF.TextLength
    Dim selectionColor = rtbScriptRTF.ForeColor
    Dim selectionBackColor = rtbScriptRTF.BackColor
    Dim selectionIndent = 0
    Dim selectionRightIndent = 0
    Dim selectionAlignment = HorizontalAlignment.Left

    Select Case row.Field(Of Integer)("Paragraphtype")
        Case 1, 5
            selectionColor = Color.Black
        Case 2
            selectionColor = Color.DarkOliveGreen
            selectionIndent = rtbScriptRTF.Width \ 3
            selectionRightIndent = 25
        Case 3
            selectionColor = Color.Navy
            selectionIndent = rtbScriptRTF.Width \ 2
        Case 4
            selectionColor = Color.DarkOliveGreen
        Case 6
            selectionColor = Color.Black
            selectionAlignment = HorizontalAlignment.Right
        Case 7
            selectionColor = Color.DarkRed
        Case 8
            selectionColor = Color.Black
            selectionBackColor = Color.LightGray
    End Select

    With rtbScriptRTF
        .AppendText(row.Field(Of String)("Paragraph"))

        Dim selectionLength = .TextLength - selectionStart

        .SelectionStart = selectionStart
        .SelectionLength = selectionLength
        .SelectionColor = selectionColor
        .SelectionBackColor = selectionBackColor
        .SelectionIndent = selectionIndent
        .SelectionRightIndent = selectionRightIndent
        .SelectionAlignment = selectionAlignment

        .AppendText(Environment.NewLine)
    End With
Next

该代码将添加一个段落并为其设置格式,添加下一个段落并为其设置格式,依此类推。